| Index: pkg/args/lib/args.dart
|
| diff --git a/pkg/args/lib/args.dart b/pkg/args/lib/args.dart
|
| index ec40958991bc5a1a497cd26a652a0ead28016051..e14c9d37d4b52b3ea5aedcdd5c898e7d86fe576d 100644
|
| --- a/pkg/args/lib/args.dart
|
| +++ b/pkg/args/lib/args.dart
|
| @@ -69,7 +69,7 @@ class ArgParser {
|
| void addFlag(String name, {String abbr, String help, bool defaultsTo: false,
|
| bool negatable: true, void callback(bool value), bool hide: false}) {
|
| _addOption(name, abbr, help, null, null, null, defaultsTo, callback,
|
| - isFlag: true, negatable: negatable, hide: hide);
|
| + OptionType.FLAG, negatable: negatable, hide: hide);
|
| }
|
|
|
| /// Defines a value-taking option. Throws an [ArgumentError] if:
|
| @@ -80,14 +80,14 @@ class ArgParser {
|
| List<String> allowed, Map<String, String> allowedHelp, String defaultsTo,
|
| void callback(value), bool allowMultiple: false, bool hide: false}) {
|
| _addOption(name, abbr, help, valueHelp, allowed, allowedHelp, defaultsTo,
|
| - callback, isFlag: false, allowMultiple: allowMultiple,
|
| + callback, allowMultiple ? OptionType.MULTIPLE : OptionType.SINGLE,
|
| hide: hide);
|
| }
|
|
|
| void _addOption(String name, String abbr, String help, String valueHelp,
|
| List<String> allowed, Map<String, String> allowedHelp, defaultsTo,
|
| - void callback(value), {bool isFlag, bool negatable: false,
|
| - bool allowMultiple: false, bool hide: false}) {
|
| + void callback(value), OptionType type, {bool negatable: false,
|
| + bool hide: false}) {
|
| // Make sure the name isn't in use.
|
| if (_options.containsKey(name)) {
|
| throw new ArgumentError('Duplicate option "$name".');
|
| @@ -103,8 +103,8 @@ class ArgParser {
|
| }
|
|
|
| _options[name] = new Option(name, abbr, help, valueHelp, allowed,
|
| - allowedHelp, defaultsTo, callback, isFlag: isFlag, negatable: negatable,
|
| - allowMultiple: allowMultiple, hide: hide);
|
| + allowedHelp, defaultsTo, callback, type, negatable: negatable,
|
| + hide: hide);
|
| }
|
|
|
| /// Parses [args], a list of command-line arguments, matches them against the
|
| @@ -140,7 +140,11 @@ class ArgParser {
|
| /// Includes the parsed options and any remaining unparsed command line
|
| /// arguments.
|
| class ArgResults {
|
| - final Map<String, dynamic> _options;
|
| + /// The [ArgParser] whose options were parsed for these results.
|
| + final ArgParser _parser;
|
| +
|
| + /// The option values that were parsed from arguments.
|
| + final Map<String, dynamic> _parsed;
|
|
|
| /// If these are the results for parsing a command's options, this will be the
|
| /// name of the command. For top-level results, this returns `null`.
|
| @@ -160,20 +164,46 @@ class ArgResults {
|
| final List<String> rest;
|
|
|
| /// Creates a new [ArgResults].
|
| - ArgResults(this._options, this.name, this.command, List<String> rest)
|
| - : this.rest = new UnmodifiableListView(rest);
|
| + ArgResults(this._parser, this._parsed, this.name, this.command,
|
| + List<String> rest)
|
| + : this.rest = new UnmodifiableListView(rest);
|
|
|
| /// Gets the parsed command-line option named [name].
|
| operator [](String name) {
|
| - if (!_options.containsKey(name)) {
|
| - throw new ArgumentError(
|
| - 'Could not find an option named "$name".');
|
| + if (!_parser.options.containsKey(name)) {
|
| + throw new ArgumentError('Could not find an option named "$name".');
|
| }
|
|
|
| - return _options[name];
|
| + return _parser.options[name].getOrDefault(_parsed[name]);
|
| }
|
|
|
| - /// Get the names of the options as an [Iterable].
|
| - Iterable<String> get options => _options.keys;
|
| + /// Get the names of the available options as an [Iterable].
|
| + ///
|
| + /// This includes the options whose values were parsed or that have defaults.
|
| + /// Options that weren't present and have no default will be omitted.
|
| + Iterable<String> get options {
|
| + var result = new Set.from(_parsed.keys);
|
| +
|
| + // Include the options that have defaults.
|
| + _parser.options.forEach((name, option) {
|
| + if (option.defaultValue != null) result.add(name);
|
| + });
|
| +
|
| + return result;
|
| + }
|
| +
|
| + /// Returns `true` if the option with [name] was parsed from an actual
|
| + /// argument.
|
| + ///
|
| + /// Returns `false` if it wasn't provided and the default value or no default
|
| + /// value would be used instead.
|
| + bool wasParsed(String name) {
|
| + var option = _parser.options[name];
|
| + if (option == null) {
|
| + throw new ArgumentError('Could not find an option named "$name".');
|
| + }
|
| +
|
| + return _parsed.containsKey(name);
|
| + }
|
| }
|
|
|
|
|