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