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 'arg_parser.dart'; | 7 import 'arg_parser.dart'; |
8 import 'arg_results.dart'; | 8 import 'arg_results.dart'; |
9 import 'option.dart'; | 9 import 'option.dart'; |
10 | 10 |
(...skipping 28 matching lines...) Expand all Loading... |
39 | 39 |
40 Parser(this.commandName, this.grammar, this.args, this.parent, rest) { | 40 Parser(this.commandName, this.grammar, this.args, this.parent, rest) { |
41 if (rest != null) this.rest.addAll(rest); | 41 if (rest != null) this.rest.addAll(rest); |
42 } | 42 } |
43 | 43 |
44 /// The current argument being parsed. | 44 /// The current argument being parsed. |
45 String get current => args[0]; | 45 String get current => args[0]; |
46 | 46 |
47 /// Parses the arguments. This can only be called once. | 47 /// Parses the arguments. This can only be called once. |
48 ArgResults parse() { | 48 ArgResults parse() { |
| 49 var arguments = args.toList(); |
49 var commandResults = null; | 50 var commandResults = null; |
50 | 51 |
51 // Parse the args. | 52 // Parse the args. |
52 while (args.length > 0) { | 53 while (args.length > 0) { |
53 if (current == '--') { | 54 if (current == '--') { |
54 // Reached the argument terminator, so stop here. | 55 // Reached the argument terminator, so stop here. |
55 args.removeAt(0); | 56 args.removeAt(0); |
56 break; | 57 break; |
57 } | 58 } |
58 | 59 |
(...skipping 25 matching lines...) Expand all Loading... |
84 | 85 |
85 // Invoke the callbacks. | 86 // Invoke the callbacks. |
86 grammar.options.forEach((name, option) { | 87 grammar.options.forEach((name, option) { |
87 if (option.callback == null) return; | 88 if (option.callback == null) return; |
88 option.callback(option.getOrDefault(results[name])); | 89 option.callback(option.getOrDefault(results[name])); |
89 }); | 90 }); |
90 | 91 |
91 // Add in the leftover arguments we didn't parse to the innermost command. | 92 // Add in the leftover arguments we didn't parse to the innermost command. |
92 rest.addAll(args); | 93 rest.addAll(args); |
93 args.clear(); | 94 args.clear(); |
94 return newArgResults(grammar, results, commandName, commandResults, rest); | 95 return newArgResults(grammar, results, commandName, commandResults, rest, |
| 96 arguments); |
95 } | 97 } |
96 | 98 |
97 /// Pulls the value for [option] from the second argument in [args]. | 99 /// Pulls the value for [option] from the second argument in [args]. |
98 /// | 100 /// |
99 /// Validates that there is a valid value there. | 101 /// Validates that there is a valid value there. |
100 void readNextArgAsValue(Option option) { | 102 void readNextArgAsValue(Option option) { |
101 // Take the option argument from the next command line arg. | 103 // Take the option argument from the next command line arg. |
102 validate(args.length > 0, | 104 validate(args.length > 0, |
103 'Missing argument for "${option.name}".'); | 105 'Missing argument for "${option.name}".'); |
104 | 106 |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 } | 262 } |
261 | 263 |
262 if (option.isMultiple) { | 264 if (option.isMultiple) { |
263 var list = results.putIfAbsent(option.name, () => []); | 265 var list = results.putIfAbsent(option.name, () => []); |
264 list.add(value); | 266 list.add(value); |
265 } else { | 267 } else { |
266 results[option.name] = value; | 268 results[option.name] = value; |
267 } | 269 } |
268 } | 270 } |
269 } | 271 } |
OLD | NEW |