| 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 /** | 5 /** |
| 6 * This library lets you define parsers for parsing raw command-line arguments | 6 * This library lets you define parsers for parsing raw command-line arguments |
| 7 * into a set of options and values using [GNU][] and [POSIX][] style options. | 7 * into a set of options and values using [GNU][] and [POSIX][] style options. |
| 8 * | 8 * |
| 9 * ## Defining options ## | 9 * ## Defining options ## |
| 10 * | 10 * |
| (...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 270 * | 270 * |
| 271 * * There is already an option with name [name]. | 271 * * There is already an option with name [name]. |
| 272 * * There is already an option using abbreviation [abbr]. | 272 * * There is already an option using abbreviation [abbr]. |
| 273 */ | 273 */ |
| 274 void addOption(String name, | 274 void addOption(String name, |
| 275 {String abbr, | 275 {String abbr, |
| 276 String help, | 276 String help, |
| 277 List<String> allowed, | 277 List<String> allowed, |
| 278 Map<String, String> allowedHelp, | 278 Map<String, String> allowedHelp, |
| 279 String defaultsTo, | 279 String defaultsTo, |
| 280 void callback(value), | 280 void callback(dynamic value), |
| 281 bool allowMultiple: false}) { | 281 bool allowMultiple: false}) { |
| 282 _addOption(name, abbr, help, allowed, allowedHelp, defaultsTo, callback, | 282 _addOption(name, abbr, help, allowed, allowedHelp, defaultsTo, callback, |
| 283 isFlag: false, allowMultiple: allowMultiple); | 283 isFlag: false, allowMultiple: allowMultiple); |
| 284 } | 284 } |
| 285 | 285 |
| 286 void _addOption(String name, String abbr, String help, List<String> allowed, | 286 void _addOption( |
| 287 Map<String, String> allowedHelp, defaultsTo, void callback(value), | 287 String name, |
| 288 {bool isFlag, bool negatable: false, bool allowMultiple: false}) { | 288 String abbr, |
| 289 String help, |
| 290 List<String> allowed, |
| 291 Map<String, String> allowedHelp, |
| 292 dynamic defaultsTo, |
| 293 void callback(dynamic value), |
| 294 {bool isFlag, |
| 295 bool negatable: false, |
| 296 bool allowMultiple: false}) { |
| 289 // Make sure the name isn't in use. | 297 // Make sure the name isn't in use. |
| 290 if (options.containsKey(name)) { | 298 if (options.containsKey(name)) { |
| 291 throw new ArgumentError('Duplicate option "$name".'); | 299 throw new ArgumentError('Duplicate option "$name".'); |
| 292 } | 300 } |
| 293 | 301 |
| 294 // Make sure the abbreviation isn't too long or in use. | 302 // Make sure the abbreviation isn't too long or in use. |
| 295 if (abbr != null) { | 303 if (abbr != null) { |
| 296 if (abbr.length > 1) { | 304 if (abbr.length > 1) { |
| 297 throw new ArgumentError( | 305 throw new ArgumentError( |
| 298 'Abbreviation "$abbr" is longer than one character.'); | 306 'Abbreviation "$abbr" is longer than one character.'); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 320 /** | 328 /** |
| 321 * Generates a string displaying usage information for the defined options. | 329 * Generates a string displaying usage information for the defined options. |
| 322 * This is basically the help text shown on the command line. | 330 * This is basically the help text shown on the command line. |
| 323 */ | 331 */ |
| 324 String getUsage() => new Usage(this).generate(); | 332 String getUsage() => new Usage(this).generate(); |
| 325 | 333 |
| 326 /** | 334 /** |
| 327 * Get the default value for an option. Useful after parsing to test | 335 * Get the default value for an option. Useful after parsing to test |
| 328 * if the user specified something other than the default. | 336 * if the user specified something other than the default. |
| 329 */ | 337 */ |
| 330 getDefault(String option) { | 338 dynamic getDefault(String option) { |
| 331 if (!options.containsKey(option)) { | 339 if (!options.containsKey(option)) { |
| 332 throw new ArgumentError('No option named $option'); | 340 throw new ArgumentError('No option named $option'); |
| 333 } | 341 } |
| 334 return options[option].defaultValue; | 342 return options[option].defaultValue; |
| 335 } | 343 } |
| 336 | 344 |
| 337 /** | 345 /** |
| 338 * Finds the option whose abbreviation is [abbr], or `null` if no option has | 346 * Finds the option whose abbreviation is [abbr], or `null` if no option has |
| 339 * that abbreviation. | 347 * that abbreviation. |
| 340 */ | 348 */ |
| 341 Option findByAbbreviation(String abbr) { | 349 Option findByAbbreviation(String abbr) { |
| 342 return options.values.firstWhere((option) => option.abbreviation == abbr, | 350 return options.values.firstWhere((option) => option.abbreviation == abbr, |
| 343 orElse: () => null); | 351 orElse: () => null); |
| 344 } | 352 } |
| 345 } | 353 } |
| 346 | 354 |
| 347 /** | 355 /** |
| 348 * A command-line option. Includes both flags and options which take a value. | 356 * A command-line option. Includes both flags and options which take a value. |
| 349 */ | 357 */ |
| 350 class Option { | 358 class Option { |
| 351 final String name; | 359 final String name; |
| 352 final String abbreviation; | 360 final String abbreviation; |
| 353 final List allowed; | 361 final List allowed; |
| 354 final defaultValue; | 362 final dynamic defaultValue; |
| 355 final Function callback; | 363 final Function callback; |
| 356 final String help; | 364 final String help; |
| 357 final Map<String, String> allowedHelp; | 365 final Map<String, String> allowedHelp; |
| 358 final bool isFlag; | 366 final bool isFlag; |
| 359 final bool negatable; | 367 final bool negatable; |
| 360 final bool allowMultiple; | 368 final bool allowMultiple; |
| 361 | 369 |
| 362 Option(this.name, this.abbreviation, this.help, this.allowed, | 370 Option(this.name, this.abbreviation, this.help, this.allowed, |
| 363 this.allowedHelp, this.defaultValue, this.callback, | 371 this.allowedHelp, this.defaultValue, this.callback, |
| 364 {this.isFlag, this.negatable, this.allowMultiple: false}); | 372 {this.isFlag, this.negatable, this.allowMultiple: false}); |
| 365 } | 373 } |
| 366 | 374 |
| 367 /** | 375 /** |
| 368 * The results of parsing a series of command line arguments using | 376 * The results of parsing a series of command line arguments using |
| 369 * [ArgParser.parse()]. Includes the parsed options and any remaining unparsed | 377 * [ArgParser.parse()]. Includes the parsed options and any remaining unparsed |
| 370 * command line arguments. | 378 * command line arguments. |
| 371 */ | 379 */ |
| 372 class ArgResults { | 380 class ArgResults { |
| 373 final Map _options; | 381 final Map<String, dynamic> _options; |
| 374 | 382 |
| 375 /** | 383 /** |
| 376 * If these are the results for parsing a command's options, this will be | 384 * If these are the results for parsing a command's options, this will be |
| 377 * the name of the command. For top-level results, this returns `null`. | 385 * the name of the command. For top-level results, this returns `null`. |
| 378 */ | 386 */ |
| 379 final String name; | 387 final String name; |
| 380 | 388 |
| 381 /** | 389 /** |
| 382 * The command that was selected, or `null` if none was. This will contain | 390 * The command that was selected, or `null` if none was. This will contain |
| 383 * the options that were selected for that command. | 391 * the options that were selected for that command. |
| 384 */ | 392 */ |
| 385 final ArgResults command; | 393 final ArgResults command; |
| 386 | 394 |
| 387 /** | 395 /** |
| 388 * The remaining command-line arguments that were not parsed as options or | 396 * The remaining command-line arguments that were not parsed as options or |
| 389 * flags. If `--` was used to separate the options from the remaining | 397 * flags. If `--` was used to separate the options from the remaining |
| 390 * arguments, it will not be included in this list. | 398 * arguments, it will not be included in this list. |
| 391 */ | 399 */ |
| 392 final List<String> rest; | 400 final List<String> rest; |
| 393 | 401 |
| 394 /** Creates a new [ArgResults]. */ | 402 /** Creates a new [ArgResults]. */ |
| 395 ArgResults(this._options, this.name, this.command, this.rest); | 403 ArgResults(this._options, this.name, this.command, this.rest); |
| 396 | 404 |
| 397 /** Gets the parsed command-line option named [name]. */ | 405 /** Gets the parsed command-line option named [name]. */ |
| 398 operator [](String name) { | 406 dynamic operator [](String name) { |
| 399 if (!_options.containsKey(name)) { | 407 if (!_options.containsKey(name)) { |
| 400 throw new ArgumentError('Could not find an option named "$name".'); | 408 throw new ArgumentError('Could not find an option named "$name".'); |
| 401 } | 409 } |
| 402 | 410 |
| 403 return _options[name]; | 411 return _options[name]; |
| 404 } | 412 } |
| 405 | 413 |
| 406 /** Get the names of the options as a [Collection]. */ | 414 /** Get the names of the options as a [Collection]. */ |
| 407 List<String> get options => _options.keys.toList(); | 415 List<String> get options => _options.keys.toList(); |
| 408 } | 416 } |
| OLD | NEW |