Chromium Code Reviews| 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. | |
|
Bob Nystrom
2012/08/16 21:26:58
This method is useful in its own right, but if it'
gram
2012/08/16 21:55:53
ArgResults does not have that information. We coul
| |
| 496 */ | |
| 497 | |
| 498 getDefault(String option) => _options.containsKey(option) ? | |
| 499 _options[option].defaultValue : null; | |
|
Bob Nystrom
2012/08/16 21:26:58
If the option name is invalid, this should throw a
gram
2012/08/16 21:55:53
Done. I assume you mean IllegalArgument...
| |
| 492 } | 500 } |
| 493 | 501 |
| 494 /** | 502 /** |
| 495 * The results of parsing a series of command line arguments using | 503 * The results of parsing a series of command line arguments using |
| 496 * [ArgParser.parse()]. Includes the parsed options and any remaining unparsed | 504 * [ArgParser.parse()]. Includes the parsed options and any remaining unparsed |
| 497 * command line arguments. | 505 * command line arguments. |
| 498 */ | 506 */ |
| 499 class ArgResults { | 507 class ArgResults { |
| 500 final Map _options; | 508 final Map _options; |
| 501 | 509 |
| 502 /** | 510 /** |
| 503 * The remaining command-line arguments that were not parsed as options or | 511 * The remaining command-line arguments that were not parsed as options or |
| 504 * flags. If `--` was used to separate the options from the remaining | 512 * flags. If `--` was used to separate the options from the remaining |
| 505 * arguments, it will not be included in this list. | 513 * arguments, it will not be included in this list. |
| 506 */ | 514 */ |
| 507 final List<String> rest; | 515 final List<String> rest; |
| 508 | 516 |
| 509 /** Creates a new [ArgResults]. */ | 517 /** Creates a new [ArgResults]. */ |
| 510 ArgResults(this._options, this.rest); | 518 ArgResults(this._options, this.rest); |
| 511 | 519 |
| 512 /** Gets the parsed command-line option named [name]. */ | 520 /** Gets the parsed command-line option named [name]. */ |
| 513 operator [](String name) { | 521 operator [](String name) { |
| 514 if (!_options.containsKey(name)) { | 522 if (!_options.containsKey(name)) { |
| 515 throw new IllegalArgumentException( | 523 throw new IllegalArgumentException( |
| 516 'Could not find an option named "$name".'); | 524 'Could not find an option named "$name".'); |
| 517 } | 525 } |
| 518 | 526 |
| 519 return _options[name]; | 527 return _options[name]; |
| 520 } | 528 } |
| 529 | |
| 530 /** Get the names of the options as a [Collection]. */ | |
| 531 Collection<String> get options => _options.getKeys(); | |
|
Bob Nystrom
2012/08/16 21:26:58
Instead of this, how about making ArgResults imple
gram
2012/08/16 21:55:53
That seems like a lot of extra effort. All I want
| |
| 521 } | 532 } |
| 522 | 533 |
| 523 class _Option { | 534 class _Option { |
| 524 final String name; | 535 final String name; |
| 525 final String abbreviation; | 536 final String abbreviation; |
| 526 final List allowed; | 537 final List allowed; |
| 527 final defaultValue; | 538 final defaultValue; |
| 528 final Function callback; | 539 final Function callback; |
| 529 final String help; | 540 final String help; |
| 530 final Map<String, String> allowedHelp; | 541 final Map<String, String> allowedHelp; |
| (...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 749 allowedBuffer.add(allowed); | 760 allowedBuffer.add(allowed); |
| 750 if (allowed == option.defaultValue) { | 761 if (allowed == option.defaultValue) { |
| 751 allowedBuffer.add(' (default)'); | 762 allowedBuffer.add(' (default)'); |
| 752 } | 763 } |
| 753 first = false; | 764 first = false; |
| 754 } | 765 } |
| 755 allowedBuffer.add(']'); | 766 allowedBuffer.add(']'); |
| 756 return allowedBuffer.toString(); | 767 return allowedBuffer.toString(); |
| 757 } | 768 } |
| 758 } | 769 } |
| OLD | NEW |