Index: pkg/args/lib/src/options.dart |
diff --git a/pkg/args/lib/src/options.dart b/pkg/args/lib/src/options.dart |
index 34dc8d30f39624e4988085e0eedb949023295ba4..c5e1e84003402f943cbd88a61dd99ea5a415f888 100644 |
--- a/pkg/args/lib/src/options.dart |
+++ b/pkg/args/lib/src/options.dart |
@@ -12,15 +12,22 @@ class Option { |
final String help; |
final String valueHelp; |
final Map<String, String> allowedHelp; |
- final bool isFlag; |
+ final OptionType type; |
final bool negatable; |
- final bool allowMultiple; |
final bool hide; |
+ /// Whether the option is boolean-valued flag. |
+ bool get isFlag => type == OptionType.FLAG; |
+ |
+ /// Whether the option takes a single value. |
+ bool get isSingle => type == OptionType.SINGLE; |
+ |
+ /// Whether the option allows multiple values. |
+ bool get isMultiple => type == OptionType.MULTIPLE; |
+ |
Option(this.name, this.abbreviation, this.help, this.valueHelp, |
List<String> allowed, Map<String, String> allowedHelp, this.defaultValue, |
- this.callback, {this.isFlag, this.negatable, this.allowMultiple: false, |
- this.hide: false}) : |
+ this.callback, this.type, {this.negatable, this.hide: false}) : |
this.allowed = allowed == null ? |
null : new UnmodifiableListView(allowed), |
this.allowedHelp = allowedHelp == null ? |
@@ -50,5 +57,52 @@ class Option { |
} |
} |
+ /// Returns [value] if non-`null`, otherwise returns the default value for |
+ /// this option. |
+ /// |
+ /// For single-valued options, it will be [defaultValue] if set or `null` |
+ /// otherwise. For multiple-valued options, it will be an empty list or a |
+ /// list containing [defaultValue] if set. |
+ dynamic getOrDefault(value) { |
+ if (value != null) return value; |
+ |
+ if (!isMultiple) return defaultValue; |
+ if (defaultValue != null) return [defaultValue]; |
+ return []; |
+ } |
+ |
static final _invalidChars = new RegExp(r'''[ \t\r\n"'\\/]'''); |
} |
+ |
+/// What kinds of values an option accepts. |
+class OptionType { |
+ /// An option that can only be `true` or `false`. |
+ /// |
+ /// The presence of the option name itself in the argument list means `true`. |
+ static const FLAG = const OptionType("OptionType.FLAG"); |
+ |
+ /// An option that takes a single value. |
+ /// |
+ /// Examples: |
+ /// |
+ /// --mode debug |
+ /// -mdebug |
+ /// --mode=debug |
+ /// |
+ /// If the option is passed more than once, the last one wins. |
+ static const SINGLE = const OptionType("OptionType.SINGLE"); |
+ |
+ /// An option that allows multiple values. |
+ /// |
+ /// Example: |
+ /// |
+ /// --output text --output xml |
+ /// |
+ /// In the parsed [ArgResults], a multiple-valued option will always return |
+ /// a list, even if one or no values were passed. |
+ static const MULTIPLE = const OptionType("OptionType.MULTIPLE"); |
+ |
+ const OptionType(this.name); |
nweiz
2014/07/14 21:22:34
This should be private.
Bob Nystrom
2014/07/15 21:05:23
Done.
|
+ |
+ final String name; |
nweiz
2014/07/14 21:22:34
Nit: fields go above constructors.
Bob Nystrom
2014/07/15 21:05:23
Done.
|
+} |