OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library args.src.parser; | 5 library args.src.parser; |
6 | 6 |
7 import '../args.dart'; | 7 import '../args.dart'; |
8 | 8 |
9 final _SOLO_OPT = new RegExp(r'^-([a-zA-Z0-9])$'); | 9 final _SOLO_OPT = new RegExp(r'^-([a-zA-Z0-9])$'); |
10 final _ABBR_OPT = new RegExp(r'^-([a-zA-Z0-9]+)(.*)$'); | 10 final _ABBR_OPT = new RegExp(r'^-([a-zA-Z0-9]+)(.*)$'); |
(...skipping 10 matching lines...) Expand all Loading... |
21 * command. For top-level results, this returns `null`. | 21 * command. For top-level results, this returns `null`. |
22 */ | 22 */ |
23 final String commandName; | 23 final String commandName; |
24 | 24 |
25 /** | 25 /** |
26 * The parser for the supercommand of this command parser, or `null` if this | 26 * The parser for the supercommand of this command parser, or `null` if this |
27 * is the top-level parser. | 27 * is the top-level parser. |
28 */ | 28 */ |
29 final Parser parent; | 29 final Parser parent; |
30 | 30 |
| 31 /** If `true`, parsing will continue after a non-option argument. */ |
| 32 final bool allowTrailingOptions; |
| 33 |
31 /** The grammar being parsed. */ | 34 /** The grammar being parsed. */ |
32 final ArgParser grammar; | 35 final ArgParser grammar; |
33 | 36 |
34 /** The arguments being parsed. */ | 37 /** The arguments being parsed. */ |
35 final List<String> args; | 38 final List<String> args; |
36 | 39 |
| 40 /** The remaining non-option, non-command arguments. */ |
| 41 final rest = <String>[]; |
| 42 |
37 /** The accumulated parsed options. */ | 43 /** The accumulated parsed options. */ |
38 final Map results = {}; | 44 final Map results = {}; |
39 | 45 |
40 Parser(this.commandName, this.grammar, this.args, [this.parent]); | 46 Parser(this.commandName, this.grammar, this.args, this.parent, rest, |
| 47 {this.allowTrailingOptions: false}) { |
| 48 if (rest != null) this.rest.addAll(rest); |
| 49 } |
| 50 |
41 | 51 |
42 /** The current argument being parsed. */ | 52 /** The current argument being parsed. */ |
43 String get current => args[0]; | 53 String get current => args[0]; |
44 | 54 |
45 /** Parses the arguments. This can only be called once. */ | 55 /** Parses the arguments. This can only be called once. */ |
46 ArgResults parse() { | 56 ArgResults parse() { |
47 var commandResults = null; | 57 var commandResults = null; |
48 | 58 |
49 // Initialize flags to their defaults. | 59 // Initialize flags to their defaults. |
50 grammar.options.forEach((name, option) { | 60 grammar.options.forEach((name, option) { |
51 if (option.allowMultiple) { | 61 if (option.allowMultiple) { |
52 results[name] = []; | 62 results[name] = []; |
53 } else { | 63 } else { |
54 results[name] = option.defaultValue; | 64 results[name] = option.defaultValue; |
55 } | 65 } |
56 }); | 66 }); |
57 | 67 |
58 // Parse the args. | 68 // Parse the args. |
59 while (args.length > 0) { | 69 while (args.length > 0) { |
60 if (current == '--') { | 70 if (current == '--') { |
61 // Reached the argument terminator, so stop here. | 71 // Reached the argument terminator, so stop here. |
62 args.removeAt(0); | 72 args.removeAt(0); |
63 break; | 73 break; |
64 } | 74 } |
65 | 75 |
66 // Try to parse the current argument as a command. This happens before | 76 // Try to parse the current argument as a command. This happens before |
67 // options so that commands can have option-like names. | 77 // options so that commands can have option-like names. |
68 var command = grammar.commands[current]; | 78 var command = grammar.commands[current]; |
69 if (command != null) { | 79 if (command != null) { |
| 80 validate(rest.isEmpty, 'Cannot specify arguments before a command.'); |
70 var commandName = args.removeAt(0); | 81 var commandName = args.removeAt(0); |
71 var commandParser = new Parser(commandName, command, args, this); | 82 var commandParser = new Parser(commandName, command, args, this, rest, |
| 83 allowTrailingOptions: allowTrailingOptions); |
72 commandResults = commandParser.parse(); | 84 commandResults = commandParser.parse(); |
73 continue; | 85 |
| 86 // All remaining arguments were passed to command so clear them here. |
| 87 rest.clear(); |
| 88 break; |
74 } | 89 } |
75 | 90 |
76 // Try to parse the current argument as an option. Note that the order | 91 // Try to parse the current argument as an option. Note that the order |
77 // here matters. | 92 // here matters. |
78 if (parseSoloOption()) continue; | 93 if (parseSoloOption()) continue; |
79 if (parseAbbreviation(this)) continue; | 94 if (parseAbbreviation(this)) continue; |
80 if (parseLongOption()) continue; | 95 if (parseLongOption()) continue; |
81 | 96 |
82 // If we got here, the argument doesn't look like an option, so stop. | 97 // This argument is neither option nor command, so stop parsing unless |
83 break; | 98 // the [allowTrailingOptions] option is set. |
| 99 if (!allowTrailingOptions) break; |
| 100 rest.add(args.removeAt(0)); |
84 } | 101 } |
85 | 102 |
86 // Set unspecified multivalued arguments to their default value, | 103 // Set unspecified multivalued arguments to their default value, |
87 // if any, and invoke the callbacks. | 104 // if any, and invoke the callbacks. |
88 grammar.options.forEach((name, option) { | 105 grammar.options.forEach((name, option) { |
89 if (option.allowMultiple && | 106 if (option.allowMultiple && |
90 results[name].length == 0 && | 107 results[name].length == 0 && |
91 option.defaultValue != null) { | 108 option.defaultValue != null) { |
92 results[name].add(option.defaultValue); | 109 results[name].add(option.defaultValue); |
93 } | 110 } |
94 if (option.callback != null) option.callback(results[name]); | 111 if (option.callback != null) option.callback(results[name]); |
95 }); | 112 }); |
96 | 113 |
97 // Add in the leftover arguments we didn't parse to the innermost command. | 114 // Add in the leftover arguments we didn't parse to the innermost command. |
98 var rest = args.toList(); | 115 rest.addAll(args); |
99 args.clear(); | 116 args.clear(); |
100 return new ArgResults(results, commandName, commandResults, rest); | 117 return new ArgResults(results, commandName, commandResults, rest); |
101 } | 118 } |
102 | 119 |
103 /** | 120 /** |
104 * Pulls the value for [option] from the second argument in [args]. Validates | 121 * Pulls the value for [option] from the second argument in [args]. Validates |
105 * that there is a valid value there. | 122 * that there is a valid value there. |
106 */ | 123 */ |
107 void readNextArgAsValue(Option option) { | 124 void readNextArgAsValue(Option option) { |
108 // Take the option argument from the next command line arg. | 125 // Take the option argument from the next command line arg. |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
272 '"$value" is not an allowed value for option "${option.name}".'); | 289 '"$value" is not an allowed value for option "${option.name}".'); |
273 } | 290 } |
274 | 291 |
275 if (option.allowMultiple) { | 292 if (option.allowMultiple) { |
276 results[option.name].add(value); | 293 results[option.name].add(value); |
277 } else { | 294 } else { |
278 results[option.name] = value; | 295 results[option.name] = value; |
279 } | 296 } |
280 } | 297 } |
281 } | 298 } |
OLD | NEW |