Chromium Code Reviews| Index: pkg/args/lib/src/parser.dart |
| diff --git a/pkg/args/lib/src/parser.dart b/pkg/args/lib/src/parser.dart |
| index d35f9ea4aff1bb67a1c20034b17574168d0c6502..7eba8e3dc866bfd9ff689be77c5fb4757329203c 100644 |
| --- a/pkg/args/lib/src/parser.dart |
| +++ b/pkg/args/lib/src/parser.dart |
| @@ -28,16 +28,26 @@ class Parser { |
| */ |
| final Parser parent; |
| + /** If `true`, parser will continue after it sees a non-option argument. */ |
|
Bob Nystrom
2013/06/24 15:53:20
"parser" -> "parsing"
"it sees" -> ""
Andrei Mouravski
2013/06/24 20:41:04
Done.
|
| + final bool allowTrailingOptions; |
| + |
| /** The grammar being parsed. */ |
| final ArgParser grammar; |
| /** The arguments being parsed. */ |
| final List<String> args; |
| + /** The remaining non-option, non-command arguments. */ |
| + List<String> rest = <String>[]; |
| + |
| /** The accumulated parsed options. */ |
| final Map results = {}; |
| - Parser(this.commandName, this.grammar, this.args, [this.parent]); |
| + Parser(this.commandName, this.grammar, this.args, |
| + [this.allowTrailingOptions = false, this.parent, rest]) { |
|
Bob Nystrom
2013/06/24 15:53:20
Ugh. Boolean parameters should be named. Since you
Andrei Mouravski
2013/06/24 20:41:04
Done.
|
| + if (rest != null) this.rest.addAll(rest); |
| + } |
| + |
| /** The current argument being parsed. */ |
| String get current => args[0]; |
| @@ -67,20 +77,31 @@ class Parser { |
| // options so that commands can have option-like names. |
| var command = grammar.commands[current]; |
| if (command != null) { |
| + validate(rest.isEmpty, 'Cannot specify arguments before a command.'); |
| var commandName = args.removeAt(0); |
| - var commandParser = new Parser(commandName, command, args, this); |
| + var commandParser = new Parser(commandName, command, args, |
| + allowTrailingOptions, this, rest.toList()); |
|
Bob Nystrom
2013/06/24 15:53:20
.toList() isn't needed.
Andrei Mouravski
2013/06/24 20:41:04
Done.
|
| commandResults = commandParser.parse(); |
| - continue; |
| + |
| + // All remaining arguments were passed to command so clear them here. |
| + rest.clear(); |
| + break; |
| } |
| // Try to parse the current argument as an option. Note that the order |
| // here matters. |
| - if (parseSoloOption()) continue; |
| - if (parseAbbreviation(this)) continue; |
| - if (parseLongOption()) continue; |
| + if (isCurrentArgAnOption) { |
| + if (parseSoloOption()) continue; |
| + if (parseAbbreviation(this)) continue; |
| + if (parseLongOption()) continue; |
| + throw new FormatException( |
| + 'Could not find an option or flag "${args[0]}".'); |
| + } |
| - // If we got here, the argument doesn't look like an option, so stop. |
| - break; |
| + // This argument is neither option nor command, so stop parsing unless |
| + // the [allowTrailingOptions] option is set. |
| + if (!allowTrailingOptions) break; |
| + rest.add(args.removeAt(0)); |
| } |
| // Set unspecified multivalued arguments to their default value, |
| @@ -95,7 +116,7 @@ class Parser { |
| }); |
| // Add in the leftover arguments we didn't parse to the innermost command. |
| - var rest = args.toList(); |
| + rest.addAll(args.toList()); |
|
Bob Nystrom
2013/06/24 15:53:20
Don't need .toList() here.
Andrei Mouravski
2013/06/24 20:41:04
Done.
|
| args.clear(); |
| return new ArgResults(results, commandName, commandResults, rest); |
| } |
| @@ -117,6 +138,10 @@ class Parser { |
| args.removeAt(0); |
| } |
| + /** Returns `true` if the current argument looks like an option. */ |
| + bool get isCurrentArgAnOption => [_SOLO_OPT, _ABBR_OPT, _LONG_OPT].any( |
| + (re) => re.firstMatch(current) != null); |
| + |
| /** |
| * Tries to parse the current argument as a "solo" option, which is a single |
| * hyphen followed by a single letter. We treat this differently than |
| @@ -130,9 +155,8 @@ class Parser { |
| var option = grammar.findByAbbreviation(soloOpt[1]); |
| if (option == null) { |
| // Walk up to the parent command if possible. |
| - validate(parent != null, |
| + return tryParseOnParent((p) => p.parseSoloOption(), |
| 'Could not find an option or flag "-${soloOpt[1]}".'); |
| - return parent.parseSoloOption(); |
| } |
| args.removeAt(0); |
| @@ -161,9 +185,8 @@ class Parser { |
| var first = grammar.findByAbbreviation(c); |
| if (first == null) { |
| // Walk up to the parent command if possible. |
| - validate(parent != null, |
| + return tryParseOnParent((p) => p.parseAbbreviation(innermostCommand), |
| 'Could not find an option with short name "-$c".'); |
| - return parent.parseAbbreviation(innermostCommand); |
| } else if (!first.isFlag) { |
| // The first character is a non-flag option, so the rest must be the |
| // value. |
| @@ -194,9 +217,8 @@ class Parser { |
| var option = grammar.findByAbbreviation(c); |
| if (option == null) { |
| // Walk up to the parent command if possible. |
| - validate(parent != null, |
| + tryParseOnParent((p) => p.parseShortFlag(c), |
| 'Could not find an option with short name "-$c".'); |
| - parent.parseShortFlag(c); |
| return; |
| } |
| @@ -238,8 +260,8 @@ class Parser { |
| option = grammar.options[name]; |
| if (option == null) { |
| // Walk up to the parent command if possible. |
| - validate(parent != null, 'Could not find an option named "$name".'); |
| - return parent.parseLongOption(); |
| + return tryParseOnParent((p) => p.parseLongOption(), |
| + 'Could not find an option named "$name".'); |
| } |
| args.removeAt(0); |
| @@ -249,8 +271,8 @@ class Parser { |
| setOption(results, option, false); |
| } else { |
| // Walk up to the parent command if possible. |
| - validate(parent != null, 'Could not find an option named "$name".'); |
| - return parent.parseLongOption(); |
| + return tryParseOnParent((p) => p.parseLongOption(), |
| + 'Could not find an option named "$name".'); |
| } |
| return true; |
| @@ -264,6 +286,27 @@ class Parser { |
| if (!condition) throw new FormatException(message); |
| } |
| + /** |
| + * Tries to run parseFunc recursively on this parser's parent parser. |
| + * |
| + * Returns `true` if the parse succeeded on any ancestor of this parser and |
| + * means that the current argument was accepted. |
| + * Returns `false` if no [Parser] accepted the [parseFunc] and |
| + * [allowTrailingOptions] is true, which allows the current argument to be |
| + * accepted elsewhere. |
| + * Throws a [FormatException] exception otherwise because the current argument |
| + * is not accepted by anything. |
| + */ |
| + bool tryParseOnParent(bool parseFunc(Parser p), String message) { |
| + if (parent != null) { |
| + return parseFunc(parent); |
| + } else if (allowTrailingOptions) { |
| + return false; |
| + } else { |
| + throw new FormatException(message); |
| + } |
| + } |
| + |
| /** Validates and stores [value] as the value for [option]. */ |
| setOption(Map results, Option option, value) { |
| // See if it's one of the allowed values. |