OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 import 'dart:convert'; | 5 import 'dart:convert'; |
6 import 'dart:io'; | 6 import 'dart:io'; |
7 | 7 |
8 import 'package:args/args.dart'; | 8 import 'package:args/args.dart'; |
9 import 'package:dart_style/src/dart_formatter.dart'; | 9 import 'package:dart_style/src/dart_formatter.dart'; |
10 import 'package:dart_style/src/formatter_exception.dart'; | 10 import 'package:dart_style/src/formatter_exception.dart'; |
11 import 'package:dart_style/src/formatter_options.dart'; | 11 import 'package:dart_style/src/formatter_options.dart'; |
12 import 'package:dart_style/src/io.dart'; | 12 import 'package:dart_style/src/io.dart'; |
13 import 'package:dart_style/src/source_code.dart'; | 13 import 'package:dart_style/src/source_code.dart'; |
14 | 14 |
15 void main(List<String> args) { | 15 void main(List<String> args) { |
16 var parser = new ArgParser(allowTrailingOptions: true); | 16 var parser = new ArgParser(allowTrailingOptions: true); |
17 | 17 |
18 parser.addFlag("help", abbr: "h", negatable: false, | 18 parser.addFlag("help", |
19 help: "Shows usage information."); | 19 abbr: "h", negatable: false, help: "Shows usage information."); |
20 parser.addOption("line-length", abbr: "l", | 20 parser.addOption("line-length", |
21 help: "Wrap lines longer than this.", | 21 abbr: "l", help: "Wrap lines longer than this.", defaultsTo: "80"); |
22 defaultsTo: "80"); | |
23 parser.addOption("preserve", | 22 parser.addOption("preserve", |
24 help: 'Selection to preserve, formatted as "start:length".'); | 23 help: 'Selection to preserve, formatted as "start:length".'); |
25 parser.addFlag("dry-run", abbr: "n", negatable: false, | 24 parser.addFlag("dry-run", |
| 25 abbr: "n", |
| 26 negatable: false, |
26 help: "Show which files would be modified but make no changes."); | 27 help: "Show which files would be modified but make no changes."); |
27 parser.addFlag("overwrite", abbr: "w", negatable: false, | 28 parser.addFlag("overwrite", |
| 29 abbr: "w", |
| 30 negatable: false, |
28 help: "Overwrite input files with formatted output."); | 31 help: "Overwrite input files with formatted output."); |
29 parser.addFlag("machine", abbr: "m", negatable: false, | 32 parser.addFlag("machine", |
| 33 abbr: "m", |
| 34 negatable: false, |
30 help: "Produce machine-readable JSON output."); | 35 help: "Produce machine-readable JSON output."); |
31 parser.addFlag("follow-links", negatable: false, | 36 parser.addFlag("follow-links", |
| 37 negatable: false, |
32 help: "Follow links to files and directories.\n" | 38 help: "Follow links to files and directories.\n" |
33 "If unset, links will be ignored."); | 39 "If unset, links will be ignored."); |
34 parser.addFlag("transform", abbr: "t", negatable: false, | 40 parser.addFlag("transform", |
| 41 abbr: "t", |
| 42 negatable: false, |
35 help: "Unused flag for compability with the old formatter."); | 43 help: "Unused flag for compability with the old formatter."); |
36 | 44 |
37 var argResults; | 45 var argResults; |
38 try { | 46 try { |
39 argResults = parser.parse(args); | 47 argResults = parser.parse(args); |
40 } on FormatException catch (err) { | 48 } on FormatException catch (err) { |
41 usageError(parser, err.message); | 49 usageError(parser, err.message); |
42 } | 50 } |
43 | 51 |
44 if (argResults["help"]) { | 52 if (argResults["help"]) { |
45 printUsage(parser); | 53 printUsage(parser); |
46 return; | 54 return; |
47 } | 55 } |
48 | 56 |
49 // Can only preserve a selection when parsing from stdin. | 57 // Can only preserve a selection when parsing from stdin. |
50 var selection; | 58 var selection; |
51 | 59 |
52 if (argResults["preserve"] != null && argResults.rest.isNotEmpty) { | 60 if (argResults["preserve"] != null && argResults.rest.isNotEmpty) { |
53 usageError(parser, "Can only use --preserve when reading from stdin."); | 61 usageError(parser, "Can only use --preserve when reading from stdin."); |
54 } | 62 } |
55 | 63 |
56 try { | 64 try { |
57 selection = parseSelection(argResults["preserve"]); | 65 selection = parseSelection(argResults["preserve"]); |
58 } on FormatException catch (_) { | 66 } on FormatException catch (_) { |
59 usageError(parser, | 67 usageError( |
| 68 parser, |
60 '--preserve must be a colon-separated pair of integers, was ' | 69 '--preserve must be a colon-separated pair of integers, was ' |
61 '"${argResults['preserve']}".'); | 70 '"${argResults['preserve']}".'); |
62 } | 71 } |
63 | 72 |
64 if (argResults["dry-run"] && argResults["overwrite"]) { | 73 if (argResults["dry-run"] && argResults["overwrite"]) { |
65 usageError(parser, | 74 usageError( |
66 "Cannot use --dry-run and --overwrite at the same time."); | 75 parser, "Cannot use --dry-run and --overwrite at the same time."); |
67 } | 76 } |
68 | 77 |
69 checkForReporterCollision(String chosen, String other) { | 78 checkForReporterCollision(String chosen, String other) { |
70 if (!argResults[other]) return; | 79 if (!argResults[other]) return; |
71 | 80 |
72 usageError(parser, | 81 usageError(parser, "Cannot use --$chosen and --$other at the same time."); |
73 "Cannot use --$chosen and --$other at the same time."); | |
74 } | 82 } |
75 | 83 |
76 var reporter = OutputReporter.print; | 84 var reporter = OutputReporter.print; |
77 if (argResults["dry-run"]) { | 85 if (argResults["dry-run"]) { |
78 checkForReporterCollision("dry-run", "overwrite"); | 86 checkForReporterCollision("dry-run", "overwrite"); |
79 checkForReporterCollision("dry-run", "machine"); | 87 checkForReporterCollision("dry-run", "machine"); |
80 | 88 |
81 reporter = OutputReporter.dryRun; | 89 reporter = OutputReporter.dryRun; |
82 } else if (argResults["overwrite"]) { | 90 } else if (argResults["overwrite"]) { |
83 checkForReporterCollision("overwrite", "machine"); | 91 checkForReporterCollision("overwrite", "machine"); |
84 | 92 |
85 if (argResults.rest.isEmpty) { | 93 if (argResults.rest.isEmpty) { |
86 usageError(parser, | 94 usageError(parser, |
87 "Cannot use --overwrite without providing any paths to format."); | 95 "Cannot use --overwrite without providing any paths to format."); |
88 } | 96 } |
89 | 97 |
90 reporter = OutputReporter.overwrite; | 98 reporter = OutputReporter.overwrite; |
91 } else if (argResults["machine"]) { | 99 } else if (argResults["machine"]) { |
92 reporter = OutputReporter.printJson; | 100 reporter = OutputReporter.printJson; |
93 } | 101 } |
94 | 102 |
95 var pageWidth; | 103 var pageWidth; |
96 | 104 |
97 try { | 105 try { |
98 pageWidth = int.parse(argResults["line-length"]); | 106 pageWidth = int.parse(argResults["line-length"]); |
99 } on FormatException catch (_) { | 107 } on FormatException catch (_) { |
100 usageError(parser, '--line-length must be an integer, was ' | 108 usageError( |
101 '"${argResults['line-length']}".'); | 109 parser, |
| 110 '--line-length must be an integer, was ' |
| 111 '"${argResults['line-length']}".'); |
102 } | 112 } |
103 | 113 |
104 var followLinks = argResults["follow-links"]; | 114 var followLinks = argResults["follow-links"]; |
105 | 115 |
106 var options = new FormatterOptions(reporter, | 116 var options = new FormatterOptions(reporter, |
107 pageWidth: pageWidth, followLinks: followLinks); | 117 pageWidth: pageWidth, followLinks: followLinks); |
108 | 118 |
109 if (argResults.rest.isEmpty) { | 119 if (argResults.rest.isEmpty) { |
110 formatStdin(options, selection); | 120 formatStdin(options, selection); |
111 } else { | 121 } else { |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 output = stdout; | 207 output = stdout; |
198 } | 208 } |
199 | 209 |
200 output.write("""$message | 210 output.write("""$message |
201 | 211 |
202 Usage: dartfmt [-n|-w] [files or directories...] | 212 Usage: dartfmt [-n|-w] [files or directories...] |
203 | 213 |
204 ${parser.usage} | 214 ${parser.usage} |
205 """); | 215 """); |
206 } | 216 } |
OLD | NEW |