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 335 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
346 * [FormatException] if [condition] is `false`. | 346 * [FormatException] if [condition] is `false`. |
347 */ | 347 */ |
348 _validate(bool condition, String message) { | 348 _validate(bool condition, String message) { |
349 if (!condition) throw new FormatException(message); | 349 if (!condition) throw new FormatException(message); |
350 } | 350 } |
351 | 351 |
352 /** Validates and stores [value] as the value for [option]. */ | 352 /** Validates and stores [value] as the value for [option]. */ |
353 _setOption(Map results, _Option option, value) { | 353 _setOption(Map results, _Option option, value) { |
354 // See if it's one of the allowed values. | 354 // See if it's one of the allowed values. |
355 if (option.allowed != null) { | 355 if (option.allowed != null) { |
356 _validate(option.allowed.some((allow) => allow == value), | 356 _validate(option.allowed.any((allow) => allow == value), |
357 '"$value" is not an allowed value for option "${option.name}".'); | 357 '"$value" is not an allowed value for option "${option.name}".'); |
358 } | 358 } |
359 | 359 |
360 if (option.allowMultiple) { | 360 if (option.allowMultiple) { |
361 results[option.name].add(value); | 361 results[option.name].add(value); |
362 } else { | 362 } else { |
363 results[option.name] = value; | 363 results[option.name] = value; |
364 } | 364 } |
365 } | 365 } |
366 | 366 |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
537 operator [](String name) { | 537 operator [](String name) { |
538 if (!_options.containsKey(name)) { | 538 if (!_options.containsKey(name)) { |
539 throw new ArgumentError( | 539 throw new ArgumentError( |
540 'Could not find an option named "$name".'); | 540 'Could not find an option named "$name".'); |
541 } | 541 } |
542 | 542 |
543 return _options[name]; | 543 return _options[name]; |
544 } | 544 } |
545 | 545 |
546 /** Get the names of the options as a [Collection]. */ | 546 /** Get the names of the options as a [Collection]. */ |
547 Collection<String> get options => _options.keys; | 547 Collection<String> get options => _options.keys.toList(); |
548 } | 548 } |
549 | 549 |
550 class _Option { | 550 class _Option { |
551 final String name; | 551 final String name; |
552 final String abbreviation; | 552 final String abbreviation; |
553 final List allowed; | 553 final List allowed; |
554 final defaultValue; | 554 final defaultValue; |
555 final Function callback; | 555 final Function callback; |
556 final String help; | 556 final String help; |
557 final Map<String, String> allowedHelp; | 557 final Map<String, String> allowedHelp; |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
623 calculateColumnWidths(); | 623 calculateColumnWidths(); |
624 | 624 |
625 for (var name in args._optionNames) { | 625 for (var name in args._optionNames) { |
626 var option = args._options[name]; | 626 var option = args._options[name]; |
627 write(0, getAbbreviation(option)); | 627 write(0, getAbbreviation(option)); |
628 write(1, getLongOption(option)); | 628 write(1, getLongOption(option)); |
629 | 629 |
630 if (option.help != null) write(2, option.help); | 630 if (option.help != null) write(2, option.help); |
631 | 631 |
632 if (option.allowedHelp != null) { | 632 if (option.allowedHelp != null) { |
633 var allowedNames = option.allowedHelp.keys; | 633 var allowedNames = option.allowedHelp.keys.toList(); |
634 allowedNames.sort((a, b) => a.compareTo(b)); | 634 allowedNames.sort(); |
635 newline(); | 635 newline(); |
636 for (var name in allowedNames) { | 636 for (var name in allowedNames) { |
637 write(1, getAllowedTitle(name)); | 637 write(1, getAllowedTitle(name)); |
638 write(2, option.allowedHelp[name]); | 638 write(2, option.allowedHelp[name]); |
639 } | 639 } |
640 newline(); | 640 newline(); |
641 } else if (option.allowed != null) { | 641 } else if (option.allowed != null) { |
642 write(2, buildAllowedList(option)); | 642 write(2, buildAllowedList(option)); |
643 } else if (option.defaultValue != null) { | 643 } else if (option.defaultValue != null) { |
644 if (option.isFlag && option.defaultValue == true) { | 644 if (option.isFlag && option.defaultValue == true) { |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
776 allowedBuffer.add(allowed); | 776 allowedBuffer.add(allowed); |
777 if (allowed == option.defaultValue) { | 777 if (allowed == option.defaultValue) { |
778 allowedBuffer.add(' (default)'); | 778 allowedBuffer.add(' (default)'); |
779 } | 779 } |
780 first = false; | 780 first = false; |
781 } | 781 } |
782 allowedBuffer.add(']'); | 782 allowedBuffer.add(']'); |
783 return allowedBuffer.toString(); | 783 return allowedBuffer.toString(); |
784 } | 784 } |
785 } | 785 } |
OLD | NEW |