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

Unified Diff: lib/args/args.dart

Issue 10855084: Added support for multi-valued options (options that can occur more than once). (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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | tests/lib/args/args_test.dart » ('j') | tests/lib/args/args_test.dart » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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]);
}
/**
« 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