OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 library options; | 5 library options; |
6 | 6 |
7 import 'dart:io'; | 7 import 'dart:io'; |
8 | 8 |
9 import 'package:args/args.dart'; | 9 import 'package:args/args.dart'; |
10 | 10 |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 | 67 |
68 /** Whether to show both cold and hot performance statistics */ | 68 /** Whether to show both cold and hot performance statistics */ |
69 final bool warmPerf; | 69 final bool warmPerf; |
70 | 70 |
71 /** Whether to treat warnings as fatal */ | 71 /** Whether to treat warnings as fatal */ |
72 final bool warningsAreFatal; | 72 final bool warningsAreFatal; |
73 | 73 |
74 /** | 74 /** |
75 * Initialize options from the given parsed [args]. | 75 * Initialize options from the given parsed [args]. |
76 */ | 76 */ |
77 CommandLineOptions._fromArgs(ArgResults args, Map<String, String> definedVaria
bles) | 77 CommandLineOptions._fromArgs(ArgResults args, Map<String, |
78 : dartSdkPath = args['dart-sdk'], | 78 String> definedVariables) |
79 this.definedVariables = definedVariables, | 79 : dartSdkPath = args['dart-sdk'], |
80 disableHints = args['no-hints'], | 80 this.definedVariables = definedVariables, |
81 displayVersion = args['version'], | 81 disableHints = args['no-hints'], |
82 enableAsync = args['enable-async'], | 82 displayVersion = args['version'], |
83 enableEnum = args['enable-enum'], | 83 enableAsync = args['enable-async'], |
84 enableTypeChecks = args['enable_type_checks'], | 84 enableEnum = args['enable-enum'], |
85 ignoreUnrecognizedFlags = args['ignore-unrecognized-flags'], | 85 enableTypeChecks = args['enable_type_checks'], |
86 log = args['log'], | 86 ignoreUnrecognizedFlags = args['ignore-unrecognized-flags'], |
87 machineFormat = args['machine'] || args['format'] == 'machine', | 87 log = args['log'], |
88 packageRootPath = args['package-root'], | 88 machineFormat = args['machine'] || args['format'] == 'machine', |
89 perf = args['perf'], | 89 packageRootPath = args['package-root'], |
90 shouldBatch = args['batch'], | 90 perf = args['perf'], |
91 showPackageWarnings = args['show-package-warnings'] || args['package-warni
ngs'], | 91 shouldBatch = args['batch'], |
92 showSdkWarnings = args['show-sdk-warnings'] || args['warnings'], | 92 showPackageWarnings = args['show-package-warnings'] || |
93 sourceFiles = args.rest, | 93 args['package-warnings'], |
94 warmPerf = args['warm-perf'], | 94 showSdkWarnings = args['show-sdk-warnings'] || args['warnings'], |
95 warningsAreFatal = args['fatal-warnings']; | 95 sourceFiles = args.rest, |
| 96 warmPerf = args['warm-perf'], |
| 97 warningsAreFatal = args['fatal-warnings']; |
96 | 98 |
97 /** | 99 /** |
98 * Parse [args] into [CommandLineOptions] describing the specified | 100 * Parse [args] into [CommandLineOptions] describing the specified |
99 * analyzer options. In case of a format error, prints error and exists. | 101 * analyzer options. In case of a format error, prints error and exists. |
100 */ | 102 */ |
101 static CommandLineOptions parse(List<String> args) { | 103 static CommandLineOptions parse(List<String> args) { |
102 CommandLineOptions options = _parse(args); | 104 CommandLineOptions options = _parse(args); |
103 // check SDK | 105 // check SDK |
104 { | 106 { |
105 var sdkPath = options.dartSdkPath; | 107 var sdkPath = options.dartSdkPath; |
106 // check that SDK is specified | 108 // check that SDK is specified |
107 if (sdkPath == null) { | 109 if (sdkPath == null) { |
108 print('Usage: $_BINARY_NAME: no Dart SDK found.'); | 110 print('Usage: $_BINARY_NAME: no Dart SDK found.'); |
109 exit(15); | 111 exit(15); |
110 } | 112 } |
111 // check that SDK is existing directory | 113 // check that SDK is existing directory |
112 if (!(new Directory(sdkPath)).existsSync()) { | 114 if (!(new Directory(sdkPath)).existsSync()) { |
113 print('Usage: $_BINARY_NAME: invalid Dart SDK path: $sdkPath'); | 115 print('Usage: $_BINARY_NAME: invalid Dart SDK path: $sdkPath'); |
114 exit(15); | 116 exit(15); |
115 } | 117 } |
116 } | 118 } |
117 // OK | 119 // OK |
118 return options; | 120 return options; |
119 } | 121 } |
120 | 122 |
| 123 static String _getVersion() { |
| 124 try { |
| 125 // This is relative to bin/snapshot, so ../.. |
| 126 String versionPath = |
| 127 Platform.script.resolve('../../version').toFilePath(); |
| 128 File versionFile = new File(versionPath); |
| 129 return versionFile.readAsStringSync().trim(); |
| 130 } catch (_) { |
| 131 // This happens when the script is not running in the context of an SDK. |
| 132 return "<unknown>"; |
| 133 } |
| 134 } |
| 135 |
121 static CommandLineOptions _parse(List<String> args) { | 136 static CommandLineOptions _parse(List<String> args) { |
122 args = args.expand((String arg) => arg.split('=')).toList(); | 137 args = args.expand((String arg) => arg.split('=')).toList(); |
123 var parser = new _CommandLineParser() | 138 var parser = new _CommandLineParser() |
124 ..addFlag('batch', abbr: 'b', help: 'Run in batch mode', | 139 ..addFlag( |
125 defaultsTo: false, negatable: false) | 140 'batch', |
126 ..addOption('dart-sdk', help: 'The path to the Dart SDK') | 141 abbr: 'b', |
127 ..addOption('package-root', abbr: 'p', | 142 help: 'Run in batch mode', |
128 help: 'The path to the package root. The flag package-root is deprecat
ed. Remove to use package information computed by pub.') | 143 defaultsTo: false, |
129 ..addOption('format', | 144 negatable: false) |
130 help: 'Specifies the format in which errors are displayed') | 145 ..addOption('dart-sdk', help: 'The path to the Dart SDK') |
131 ..addFlag('machine', | 146 ..addOption( |
132 help: 'Print errors in a format suitable for parsing (deprecated)', | 147 'package-root', |
133 defaultsTo: false, negatable: false) | 148 abbr: 'p', |
134 ..addFlag('version', help: 'Print the analyzer version', | 149 help: |
135 defaultsTo: false, negatable: false) | 150 'The path to the package root. The flag package-root is deprecat
ed. Remove to use package information computed by pub.') |
136 ..addFlag('no-hints', help: 'Do not show hint results', | 151 ..addOption( |
137 defaultsTo: false, negatable: false) | 152 'format', |
138 ..addFlag('ignore-unrecognized-flags', | 153 help: 'Specifies the format in which errors are displayed') |
139 help: 'Ignore unrecognized command line flags', | 154 ..addFlag( |
140 defaultsTo: false, negatable: false) | 155 'machine', |
141 ..addFlag('fatal-warnings', help: 'Treat non-type warnings as fatal', | 156 help: 'Print errors in a format suitable for parsing (deprecated)', |
142 defaultsTo: false, negatable: false) | 157 defaultsTo: false, |
143 ..addFlag('package-warnings', | 158 negatable: false) |
144 help: 'Show warnings from package: imports', | 159 ..addFlag( |
145 defaultsTo: false, negatable: false) | 160 'version', |
146 ..addFlag('show-package-warnings', | 161 help: 'Print the analyzer version', |
147 help: 'Show warnings from package: imports (deprecated)', | 162 defaultsTo: false, |
148 defaultsTo: false, negatable: false) | 163 negatable: false) |
149 ..addFlag('perf', | 164 ..addFlag( |
150 help: 'Show performance statistics', | 165 'no-hints', |
151 defaultsTo: false, negatable: false) | 166 help: 'Do not show hint results', |
152 ..addFlag('warnings', help: 'Show warnings from SDK imports', | 167 defaultsTo: false, |
153 defaultsTo: false, negatable: false) | 168 negatable: false) |
154 ..addFlag('show-sdk-warnings', help: 'Show warnings from SDK imports (depr
ecated)', | 169 ..addFlag( |
155 defaultsTo: false, negatable: false) | 170 'ignore-unrecognized-flags', |
156 ..addFlag('help', abbr: 'h', help: 'Display this help message', | 171 help: 'Ignore unrecognized command line flags', |
157 defaultsTo: false, negatable: false) | 172 defaultsTo: false, |
158 // | 173 negatable: false) |
159 // Hidden flags. | 174 ..addFlag( |
160 // | 175 'fatal-warnings', |
161 ..addFlag('enable-async', | 176 help: 'Treat non-type warnings as fatal', |
162 help: 'Enable support for the proposed async feature', | 177 defaultsTo: false, |
163 defaultsTo: false, negatable: false, hide: true) | 178 negatable: false) |
164 ..addFlag('enable-enum', | 179 ..addFlag( |
165 help: 'Enable support for the proposed enum feature', | 180 'package-warnings', |
166 defaultsTo: false, negatable: false, hide: true) | 181 help: 'Show warnings from package: imports', |
167 ..addFlag('log', help: 'Log additional messages and exceptions', | 182 defaultsTo: false, |
168 defaultsTo: false, negatable: false, hide: true) | 183 negatable: false) |
169 ..addFlag('warm-perf', | 184 ..addFlag( |
170 help: 'Show both cold and warm performance statistics', | 185 'show-package-warnings', |
171 defaultsTo: false, negatable: false, hide: true) | 186 help: 'Show warnings from package: imports (deprecated)', |
172 ..addFlag('enable_type_checks', | 187 defaultsTo: false, |
173 help: 'Check types in constant evaluation', | 188 negatable: false) |
174 defaultsTo: false, negatable: false, hide: true); | 189 ..addFlag( |
| 190 'perf', |
| 191 help: 'Show performance statistics', |
| 192 defaultsTo: false, |
| 193 negatable: false) |
| 194 ..addFlag( |
| 195 'warnings', |
| 196 help: 'Show warnings from SDK imports', |
| 197 defaultsTo: false, |
| 198 negatable: false) |
| 199 ..addFlag( |
| 200 'show-sdk-warnings', |
| 201 help: 'Show warnings from SDK imports (deprecated)', |
| 202 defaultsTo: false, |
| 203 negatable: false) |
| 204 ..addFlag( |
| 205 'help', |
| 206 abbr: 'h', |
| 207 help: 'Display this help message', |
| 208 defaultsTo: false, |
| 209 negatable: false) |
| 210 // |
| 211 // Hidden flags. |
| 212 // |
| 213 ..addFlag( |
| 214 'enable-async', |
| 215 help: 'Enable support for the proposed async feature', |
| 216 defaultsTo: false, |
| 217 negatable: false, |
| 218 hide: true) |
| 219 ..addFlag( |
| 220 'enable-enum', |
| 221 help: 'Enable support for the proposed enum feature', |
| 222 defaultsTo: false, |
| 223 negatable: false, |
| 224 hide: true) |
| 225 ..addFlag( |
| 226 'log', |
| 227 help: 'Log additional messages and exceptions', |
| 228 defaultsTo: false, |
| 229 negatable: false, |
| 230 hide: true) |
| 231 ..addFlag( |
| 232 'warm-perf', |
| 233 help: 'Show both cold and warm performance statistics', |
| 234 defaultsTo: false, |
| 235 negatable: false, |
| 236 hide: true) |
| 237 ..addFlag( |
| 238 'enable_type_checks', |
| 239 help: 'Check types in constant evaluation', |
| 240 defaultsTo: false, |
| 241 negatable: false, |
| 242 hide: true); |
175 | 243 |
176 try { | 244 try { |
177 // TODO(scheglov) https://code.google.com/p/dart/issues/detail?id=11061 | 245 // TODO(scheglov) https://code.google.com/p/dart/issues/detail?id=11061 |
178 args = args.map((String arg) => arg == '-batch' ? '--batch' : arg).toList(
); | 246 args = |
| 247 args.map((String arg) => arg == '-batch' ? '--batch' : arg).toList(); |
179 Map<String, String> definedVariables = <String, String>{}; | 248 Map<String, String> definedVariables = <String, String>{}; |
180 var results = parser.parse(args, definedVariables); | 249 var results = parser.parse(args, definedVariables); |
181 // help requests | 250 // help requests |
182 if (results['help']) { | 251 if (results['help']) { |
183 _showUsage(parser); | 252 _showUsage(parser); |
184 exit(0); | 253 exit(0); |
185 } | 254 } |
186 // batch mode and input files | 255 // batch mode and input files |
187 if (results['batch']) { | 256 if (results['batch']) { |
188 if (results.rest.isNotEmpty) { | 257 if (results.rest.isNotEmpty) { |
(...skipping 18 matching lines...) Expand all Loading... |
207 } | 276 } |
208 | 277 |
209 } | 278 } |
210 | 279 |
211 static _showUsage(parser) { | 280 static _showUsage(parser) { |
212 print('Usage: $_BINARY_NAME [options...] <libraries to analyze...>'); | 281 print('Usage: $_BINARY_NAME [options...] <libraries to analyze...>'); |
213 print(parser.getUsage()); | 282 print(parser.getUsage()); |
214 print(''); | 283 print(''); |
215 print('For more information, see http://www.dartlang.org/tools/analyzer.'); | 284 print('For more information, see http://www.dartlang.org/tools/analyzer.'); |
216 } | 285 } |
217 | |
218 static String _getVersion() { | |
219 try { | |
220 // This is relative to bin/snapshot, so ../.. | |
221 String versionPath = | |
222 Platform.script.resolve('../../version').toFilePath(); | |
223 File versionFile = new File(versionPath); | |
224 return versionFile.readAsStringSync().trim(); | |
225 } catch (_) { | |
226 // This happens when the script is not running in the context of an SDK. | |
227 return "<unknown>"; | |
228 } | |
229 } | |
230 } | 286 } |
231 | 287 |
232 /** | 288 /** |
233 * Commandline argument parser. | 289 * Commandline argument parser. |
234 * | 290 * |
235 * TODO(pquitslund): when the args package supports ignoring unrecognized | 291 * TODO(pquitslund): when the args package supports ignoring unrecognized |
236 * options/flags, this class can be replaced with a simple [ArgParser] instance. | 292 * options/flags, this class can be replaced with a simple [ArgParser] instance. |
237 */ | 293 */ |
238 class _CommandLineParser { | 294 class _CommandLineParser { |
239 | 295 |
240 final List<String> _knownFlags; | 296 final List<String> _knownFlags; |
241 final ArgParser _parser; | 297 final ArgParser _parser; |
242 | 298 |
243 /** Creates a new command line parser */ | 299 /** Creates a new command line parser */ |
244 _CommandLineParser() | 300 _CommandLineParser() |
245 : _knownFlags = <String>[], | 301 : _knownFlags = <String>[], |
246 _parser = new ArgParser(allowTrailingOptions: true); | 302 _parser = new ArgParser(allowTrailingOptions: true); |
247 | 303 |
248 | 304 |
249 /** | 305 /** |
250 * Defines a flag. | 306 * Defines a flag. |
251 * | 307 * |
252 * See [ArgParser.addFlag()]. | 308 * See [ArgParser.addFlag()]. |
253 */ | 309 */ |
254 void addFlag(String name, {String abbr, String help, bool defaultsTo: false, | 310 void addFlag(String name, {String abbr, String help, bool defaultsTo: false, |
255 bool negatable: true, void callback(bool value), bool hide: false}) { | 311 bool negatable: true, void callback(bool value), bool hide: false}) { |
256 _knownFlags.add(name); | 312 _knownFlags.add(name); |
257 _parser.addFlag(name, abbr: abbr, help: help, defaultsTo: defaultsTo, | 313 _parser.addFlag( |
258 negatable: negatable, callback: callback, hide: hide); | 314 name, |
| 315 abbr: abbr, |
| 316 help: help, |
| 317 defaultsTo: defaultsTo, |
| 318 negatable: negatable, |
| 319 callback: callback, |
| 320 hide: hide); |
259 } | 321 } |
260 | 322 |
261 /** | 323 /** |
262 * Defines a value-taking option. | 324 * Defines a value-taking option. |
263 * | 325 * |
264 * See [ArgParser.addOption()]. | 326 * See [ArgParser.addOption()]. |
265 */ | 327 */ |
266 void addOption(String name, {String abbr, String help, List<String> allowed, | 328 void addOption(String name, {String abbr, String help, List<String> allowed, |
267 Map<String, String> allowedHelp, String defaultsTo, | 329 Map<String, String> allowedHelp, String defaultsTo, void callback(value), |
268 void callback(value), bool allowMultiple: false}) { | 330 bool allowMultiple: false}) { |
269 _knownFlags.add(name); | 331 _knownFlags.add(name); |
270 _parser.addOption(name, abbr: abbr, help: help, allowed: allowed, | 332 _parser.addOption( |
271 allowedHelp: allowedHelp, defaultsTo: defaultsTo, callback: callback, | 333 name, |
| 334 abbr: abbr, |
| 335 help: help, |
| 336 allowed: allowed, |
| 337 allowedHelp: allowedHelp, |
| 338 defaultsTo: defaultsTo, |
| 339 callback: callback, |
272 allowMultiple: allowMultiple); | 340 allowMultiple: allowMultiple); |
273 } | 341 } |
274 | 342 |
275 | 343 |
276 /** | 344 /** |
277 * Generates a string displaying usage information for the defined options. | 345 * Generates a string displaying usage information for the defined options. |
278 * | 346 * |
279 * See [ArgParser.usage]. | 347 * See [ArgParser.usage]. |
280 */ | 348 */ |
281 String getUsage() => _parser.usage; | 349 String getUsage() => _parser.usage; |
282 | 350 |
283 /** | 351 /** |
284 * Parses [args], a list of command-line arguments, matches them against the | 352 * Parses [args], a list of command-line arguments, matches them against the |
285 * flags and options defined by this parser, and returns the result. The | 353 * flags and options defined by this parser, and returns the result. The |
286 * values of any defined variables are captured in the given map. | 354 * values of any defined variables are captured in the given map. |
287 * | 355 * |
288 * See [ArgParser]. | 356 * See [ArgParser]. |
289 */ | 357 */ |
290 ArgResults parse(List<String> args, Map<String, String> definedVariables) => _
parser.parse(_filterUnknowns(parseDefinedVariables(args, definedVariables))); | 358 ArgResults parse(List<String> args, Map<String, String> definedVariables) => |
| 359 _parser.parse(_filterUnknowns(parseDefinedVariables(args, definedVariables
))); |
291 | 360 |
292 List<String> parseDefinedVariables(List<String> args, Map<String, String> defi
nedVariables) { | 361 List<String> parseDefinedVariables(List<String> args, Map<String, |
| 362 String> definedVariables) { |
293 int count = args.length; | 363 int count = args.length; |
294 List<String> remainingArgs = <String>[]; | 364 List<String> remainingArgs = <String>[]; |
295 for (int i = 0; i < count; i++) { | 365 for (int i = 0; i < count; i++) { |
296 String arg = args[i]; | 366 String arg = args[i]; |
297 if (arg == '--') { | 367 if (arg == '--') { |
298 while (i < count) { | 368 while (i < count) { |
299 remainingArgs.add(args[i++]); | 369 remainingArgs.add(args[i++]); |
300 } | 370 } |
301 } else if (arg.startsWith("-D")) { | 371 } else if (arg.startsWith("-D")) { |
302 definedVariables[arg.substring(2)] = args[++i]; | 372 definedVariables[arg.substring(2)] = args[++i]; |
303 } else { | 373 } else { |
304 remainingArgs.add(arg); | 374 remainingArgs.add(arg); |
305 } | 375 } |
306 } | 376 } |
307 return remainingArgs; | 377 return remainingArgs; |
308 } | 378 } |
309 | 379 |
310 List<String> _filterUnknowns(args) { | 380 List<String> _filterUnknowns(args) { |
311 | 381 |
312 // Only filter args if the ignore flag is specified. | 382 // Only filter args if the ignore flag is specified. |
313 if (!args.contains('--ignore-unrecognized-flags')) { | 383 if (!args.contains('--ignore-unrecognized-flags')) { |
314 return args; | 384 return args; |
315 } | 385 } |
316 | 386 //TODO(pquitslund): replace w/ the following once library skew issues are |
317 //TODO(pquitslund): replace w/ the following once library skew issues are so
rted out | 387 // sorted out |
318 //return args.where((arg) => !arg.startsWith('--') || | 388 //return args.where((arg) => !arg.startsWith('--') || |
319 // _knownFlags.contains(arg.substring(2))); | 389 // _knownFlags.contains(arg.substring(2))); |
320 | 390 |
321 // Filter all unrecognized flags and options. | 391 // Filter all unrecognized flags and options. |
322 var filtered = <String>[]; | 392 var filtered = <String>[]; |
323 for (var i=0; i < args.length; ++i) { | 393 for (var i = 0; i < args.length; ++i) { |
324 var arg = args[i]; | 394 var arg = args[i]; |
325 if (arg.startsWith('--') && arg.length > 2) { | 395 if (arg.startsWith('--') && arg.length > 2) { |
326 if (!_knownFlags.contains(arg.substring(2))) { | 396 if (!_knownFlags.contains(arg.substring(2))) { |
327 print('remove: $arg'); | 397 print('remove: $arg'); |
328 //"eat" params by advancing to the next flag/option | 398 //"eat" params by advancing to the next flag/option |
329 i = _getNextFlagIndex(args, i); | 399 i = _getNextFlagIndex(args, i); |
330 } else { | 400 } else { |
331 filtered.add(arg); | 401 filtered.add(arg); |
332 } | 402 } |
333 } else { | 403 } else { |
334 filtered.add(arg); | 404 filtered.add(arg); |
335 } | 405 } |
336 } | 406 } |
337 | 407 |
338 return filtered; | 408 return filtered; |
339 } | 409 } |
340 | 410 |
341 _getNextFlagIndex(args, i) { | 411 _getNextFlagIndex(args, i) { |
342 for ( ; i < args.length; ++i) { | 412 for ( ; i < args.length; ++i) { |
343 if (args[i].startsWith('--')) { | 413 if (args[i].startsWith('--')) { |
344 return i; | 414 return i; |
345 } | 415 } |
346 } | 416 } |
347 return i; | 417 return i; |
348 } | 418 } |
349 } | 419 } |
OLD | NEW |