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..23e37beacb77c7de823d6757157309ca2aa79364 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`, parsing will continue after a non-option argument. */ |
+ 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>[]; |
Bob Nystrom
2013/06/24 20:49:29
Make this final and lose the type annotation.
Andrei Mouravski
2013/06/24 20:54:29
Done.
|
+ |
/** The accumulated parsed options. */ |
final Map results = {}; |
- Parser(this.commandName, this.grammar, this.args, [this.parent]); |
+ Parser(this.commandName, this.grammar, this.args, this.parent, rest, |
+ {this.allowTrailingOptions: false}) { |
+ if (rest != null) this.rest.addAll(rest); |
+ } |
+ |
/** The current argument being parsed. */ |
String get current => args[0]; |
@@ -67,20 +77,29 @@ 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, this, rest, |
+ allowTrailingOptions: allowTrailingOptions); |
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) { |
Bob Nystrom
2013/06/24 20:49:29
This isn't needed any more.
Andrei Mouravski
2013/06/24 20:54:29
Done.
|
+ if (parseSoloOption()) continue; |
+ if (parseAbbreviation(this)) continue; |
+ if (parseLongOption()) continue; |
+ } |
- // 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 +114,7 @@ class Parser { |
}); |
// Add in the leftover arguments we didn't parse to the innermost command. |
- var rest = args.toList(); |
+ rest.addAll(args); |
args.clear(); |
return new ArgResults(results, commandName, commandResults, rest); |
} |
@@ -117,6 +136,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); |
Bob Nystrom
2013/06/24 20:49:29
You can ditch this.
Andrei Mouravski
2013/06/24 20:54:29
Done.
|
+ |
/** |
* 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 |