| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 471 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 482 * Finds the option whose abbreviation is [abbr], or `null` if no option has | 482 * Finds the option whose abbreviation is [abbr], or `null` if no option has |
| 483 * that abbreviation. | 483 * that abbreviation. |
| 484 */ | 484 */ |
| 485 _Option _findByAbbr(String abbr) { | 485 _Option _findByAbbr(String abbr) { |
| 486 for (var option in _options.getValues()) { | 486 for (var option in _options.getValues()) { |
| 487 if (option.abbreviation == abbr) return option; | 487 if (option.abbreviation == abbr) return option; |
| 488 } | 488 } |
| 489 | 489 |
| 490 return null; | 490 return null; |
| 491 } | 491 } |
| 492 |
| 493 /** |
| 494 * Get the default value for an option. Useful after parsing to test |
| 495 * if the user specified something other than the default. |
| 496 */ |
| 497 getDefault(String option) { |
| 498 if (!_options.containsKey(option)) { |
| 499 throw new IllegalArgumentException('No option named $option'); |
| 500 } |
| 501 return _options[option].defaultValue; |
| 502 } |
| 492 } | 503 } |
| 493 | 504 |
| 494 /** | 505 /** |
| 495 * The results of parsing a series of command line arguments using | 506 * The results of parsing a series of command line arguments using |
| 496 * [ArgParser.parse()]. Includes the parsed options and any remaining unparsed | 507 * [ArgParser.parse()]. Includes the parsed options and any remaining unparsed |
| 497 * command line arguments. | 508 * command line arguments. |
| 498 */ | 509 */ |
| 499 class ArgResults { | 510 class ArgResults { |
| 500 final Map _options; | 511 final Map _options; |
| 501 | 512 |
| 502 /** | 513 /** |
| 503 * The remaining command-line arguments that were not parsed as options or | 514 * The remaining command-line arguments that were not parsed as options or |
| 504 * flags. If `--` was used to separate the options from the remaining | 515 * flags. If `--` was used to separate the options from the remaining |
| 505 * arguments, it will not be included in this list. | 516 * arguments, it will not be included in this list. |
| 506 */ | 517 */ |
| 507 final List<String> rest; | 518 final List<String> rest; |
| 508 | 519 |
| 509 /** Creates a new [ArgResults]. */ | 520 /** Creates a new [ArgResults]. */ |
| 510 ArgResults(this._options, this.rest); | 521 ArgResults(this._options, this.rest); |
| 511 | 522 |
| 512 /** Gets the parsed command-line option named [name]. */ | 523 /** Gets the parsed command-line option named [name]. */ |
| 513 operator [](String name) { | 524 operator [](String name) { |
| 514 if (!_options.containsKey(name)) { | 525 if (!_options.containsKey(name)) { |
| 515 throw new IllegalArgumentException( | 526 throw new IllegalArgumentException( |
| 516 'Could not find an option named "$name".'); | 527 'Could not find an option named "$name".'); |
| 517 } | 528 } |
| 518 | 529 |
| 519 return _options[name]; | 530 return _options[name]; |
| 520 } | 531 } |
| 532 |
| 533 /** Get the names of the options as a [Collection]. */ |
| 534 Collection<String> get options => _options.getKeys(); |
| 521 } | 535 } |
| 522 | 536 |
| 523 class _Option { | 537 class _Option { |
| 524 final String name; | 538 final String name; |
| 525 final String abbreviation; | 539 final String abbreviation; |
| 526 final List allowed; | 540 final List allowed; |
| 527 final defaultValue; | 541 final defaultValue; |
| 528 final Function callback; | 542 final Function callback; |
| 529 final String help; | 543 final String help; |
| 530 final Map<String, String> allowedHelp; | 544 final Map<String, String> allowedHelp; |
| (...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 749 allowedBuffer.add(allowed); | 763 allowedBuffer.add(allowed); |
| 750 if (allowed == option.defaultValue) { | 764 if (allowed == option.defaultValue) { |
| 751 allowedBuffer.add(' (default)'); | 765 allowedBuffer.add(' (default)'); |
| 752 } | 766 } |
| 753 first = false; | 767 first = false; |
| 754 } | 768 } |
| 755 allowedBuffer.add(']'); | 769 allowedBuffer.add(']'); |
| 756 return allowedBuffer.toString(); | 770 return allowedBuffer.toString(); |
| 757 } | 771 } |
| 758 } | 772 } |
| OLD | NEW |