Chromium Code Reviews| Index: lib/args/args.dart |
| =================================================================== |
| --- lib/args/args.dart (revision 10395) |
| +++ lib/args/args.dart (working copy) |
| @@ -123,6 +123,11 @@ |
| * |
| * -vfi |
| * |
| + * By default, an option has only a single value, with later option values |
| + * overriding earlier ones, unless you set the [multiValued] flag. In that |
| + * case the option can occur multiple times and when parsing arguments a |
| + * List of values will be returned. |
|
Bob Nystrom
2012/08/09 23:08:40
You should show an example of this.
gram
2012/08/09 23:47:00
Done.
|
| + * |
| * ## Usage ## |
| * |
| * This library can also be used to automatically generate nice usage help |
| @@ -214,14 +219,15 @@ |
| */ |
| void addOption(String name, [String abbr, String help, List<String> allowed, |
| Map<String, String> allowedHelp, String defaultsTo, |
| - void callback(bool value)]) { |
| + void callback(bool value), bool multiValued]) { |
|
Bob Nystrom
2012/08/09 23:08:40
How about:
"multiValued" -> "allowMultiple"
gram
2012/08/09 23:47:00
Done.
|
| _addOption(name, abbr, help, allowed, allowedHelp, defaultsTo, |
| - callback, isFlag: false); |
| + callback, isFlag: false, multiValued: multiValued); |
| } |
| void _addOption(String name, String abbr, String help, List<String> allowed, |
| Map<String, String> allowedHelp, defaultsTo, |
| - void callback(bool value), [bool isFlag, bool negatable = false]) { |
| + void callback(bool value), [bool isFlag, bool negatable = false, |
| + bool multiValued = false]) { |
| // Make sure the name isn't in use. |
| if (_options.containsKey(name)) { |
| throw new IllegalArgumentException('Duplicate option "$name".'); |
| @@ -242,7 +248,8 @@ |
| } |
| _options[name] = new _Option(name, abbr, help, allowed, allowedHelp, |
| - defaultsTo, callback, isFlag: isFlag, negatable: negatable); |
| + defaultsTo, callback, isFlag: isFlag, negatable: negatable, |
| + multiValued: multiValued); |
| _optionNames.add(name); |
| } |
| @@ -257,7 +264,11 @@ |
| // Initialize flags to their defaults. |
| _options.forEach((name, option) { |
| - results[name] = option.defaultValue; |
| + if (option.multiValued) { |
| + results[name] = new List(); |
|
Bob Nystrom
2012/08/09 23:08:40
'new List()' => '[]'
gram
2012/08/09 23:47:00
Done.
|
| + } else { |
| + results[name] = option.defaultValue; |
| + } |
| }); |
| // Parse the args. |
| @@ -280,9 +291,15 @@ |
| break; |
| } |
| - // Invoke the callbacks. |
| + // Set unspecified multivalued arguments to their default value, |
| + // if any, and invoke the callbacks. |
| for (var name in _optionNames) { |
| var option = _options[name]; |
| + if (option.multiValued && |
| + results[name].length == 0 && |
| + option.defaultValue != null) { |
| + results[name].add(option.defaultValue); |
| + } |
| if (option.callback != null) option.callback(results[name]); |
| } |
| @@ -314,8 +331,11 @@ |
| _validate(option.allowed.some((allow) => allow == value), |
| '"$value" is not an allowed value for option "${option.name}".'); |
| } |
| - |
| - results[option.name] = value; |
| + if (option.multiValued) { |
|
Bob Nystrom
2012/08/09 23:08:40
Nit, but can you add a blank line above this? I te
gram
2012/08/09 23:47:00
Done.
|
| + results[option.name].add(value); |
| + } else { |
| + results[option.name] = value; |
| + } |
| } |
| /** |
| @@ -497,10 +517,11 @@ |
| final Map<String, String> allowedHelp; |
| final bool isFlag; |
| final bool negatable; |
| + final bool multiValued; |
| _Option(this.name, this.abbreviation, this.help, this.allowed, |
| this.allowedHelp, this.defaultValue, this.callback, [this.isFlag, |
| - this.negatable]); |
| + this.negatable, this.multiValued]); |
| } |
| /** |