Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1145)

Side by Side Diff: lib/args/args.dart

Issue 10828351: Added the ability to get the names of the options in a set of parsed arguments, and the default val… (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | tests/lib/args/args_test.dart » ('j') | tests/lib/args/args_test.dart » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
498 getDefault(String option) {
Bob Nystrom 2012/08/16 22:22:10 Delete blank line above this.
499 if (_options.containsKey(option)) {
500 return _options[option].defaultValue;
501 } else {
502 throw new IllegalArgumentException('No option named $option');
Bob Nystrom 2012/08/16 22:22:10 Nit, but when possible, I prefer validating the ar
gram 2012/08/16 23:20:17 Done.
503 }
504 }
492 } 505 }
493 506
494 /** 507 /**
495 * The results of parsing a series of command line arguments using 508 * The results of parsing a series of command line arguments using
496 * [ArgParser.parse()]. Includes the parsed options and any remaining unparsed 509 * [ArgParser.parse()]. Includes the parsed options and any remaining unparsed
497 * command line arguments. 510 * command line arguments.
498 */ 511 */
499 class ArgResults { 512 class ArgResults {
500 final Map _options; 513 final Map _options;
501 514
502 /** 515 /**
503 * The remaining command-line arguments that were not parsed as options or 516 * The remaining command-line arguments that were not parsed as options or
504 * flags. If `--` was used to separate the options from the remaining 517 * flags. If `--` was used to separate the options from the remaining
505 * arguments, it will not be included in this list. 518 * arguments, it will not be included in this list.
506 */ 519 */
507 final List<String> rest; 520 final List<String> rest;
508 521
509 /** Creates a new [ArgResults]. */ 522 /** Creates a new [ArgResults]. */
510 ArgResults(this._options, this.rest); 523 ArgResults(this._options, this.rest);
511 524
512 /** Gets the parsed command-line option named [name]. */ 525 /** Gets the parsed command-line option named [name]. */
513 operator [](String name) { 526 operator [](String name) {
514 if (!_options.containsKey(name)) { 527 if (!_options.containsKey(name)) {
515 throw new IllegalArgumentException( 528 throw new IllegalArgumentException(
516 'Could not find an option named "$name".'); 529 'Could not find an option named "$name".');
517 } 530 }
518 531
519 return _options[name]; 532 return _options[name];
520 } 533 }
534
535 /** Get the names of the options as a [Collection]. */
536 Collection<String> get options => _options.getKeys();
521 } 537 }
522 538
523 class _Option { 539 class _Option {
524 final String name; 540 final String name;
525 final String abbreviation; 541 final String abbreviation;
526 final List allowed; 542 final List allowed;
527 final defaultValue; 543 final defaultValue;
528 final Function callback; 544 final Function callback;
529 final String help; 545 final String help;
530 final Map<String, String> allowedHelp; 546 final Map<String, String> allowedHelp;
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after
749 allowedBuffer.add(allowed); 765 allowedBuffer.add(allowed);
750 if (allowed == option.defaultValue) { 766 if (allowed == option.defaultValue) {
751 allowedBuffer.add(' (default)'); 767 allowedBuffer.add(' (default)');
752 } 768 }
753 first = false; 769 first = false;
754 } 770 }
755 allowedBuffer.add(']'); 771 allowedBuffer.add(']');
756 return allowedBuffer.toString(); 772 return allowedBuffer.toString();
757 } 773 }
758 } 774 }
OLDNEW
« no previous file with comments | « no previous file | tests/lib/args/args_test.dart » ('j') | tests/lib/args/args_test.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698