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 /** | 5 /** |
6 * This library lets you define parsers for parsing raw command-line arguments | 6 * This library lets you define parsers for parsing raw command-line arguments |
7 * into a set of options and values using [GNU][] and [POSIX][] style options. | 7 * into a set of options and values using [GNU][] and [POSIX][] style options. |
8 * | 8 * |
9 * ## Installing ## | 9 * ## Installing ## |
10 * | 10 * |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
166 * $ git commit -a | 166 * $ git commit -a |
167 * | 167 * |
168 * The executable is `git`, the command is `commit`, and the `-a` option is an | 168 * The executable is `git`, the command is `commit`, and the `-a` option is an |
169 * option passed to the command. You can add a command like so: | 169 * option passed to the command. You can add a command like so: |
170 * | 170 * |
171 * var parser = new ArgParser(); | 171 * var parser = new ArgParser(); |
172 * var command = parser.addCommand("commit"); | 172 * var command = parser.addCommand("commit"); |
173 * command.addFlag('all', abbr: 'a'); | 173 * command.addFlag('all', abbr: 'a'); |
174 * | 174 * |
175 * It returns another [ArgParser] which you can use to define options and | 175 * It returns another [ArgParser] which you can use to define options and |
176 * subcommands on that command. When an argument list is parsed, you can then | 176 * subcommands on that command. If you already have an [ArgParser] for the |
177 * determine which command was entered and what options were provided for it. | 177 * command's options, you can pass it to [addCommand]: |
178 * | |
179 * var parser = new ArgParser(); | |
180 * var command = new ArgParser(); | |
181 * command.addFlag('all', abbr: 'a'); | |
182 * parser.addCommand("commit", command); | |
183 * | |
184 * When an argument list is parsed, you can then determine which command was | |
185 * entered and what options were provided for it. | |
gram
2013/05/21 23:11:07
Can you elaborate more here? Can an argparser have
Bob Nystrom
2013/05/21 23:20:15
There's lots of tests for these cases, but little
| |
178 * | 186 * |
179 * var results = parser.parse(['commit', '-a']); | 187 * var results = parser.parse(['commit', '-a']); |
180 * print(results.command.name); // "commit" | 188 * print(results.command.name); // "commit" |
181 * print(results.command['a']); // true | 189 * print(results.command['a']); // true |
182 * | 190 * |
183 * ## Displaying usage ## | 191 * ## Displaying usage ## |
184 * | 192 * |
185 * This library can also be used to automatically generate nice usage help | 193 * This library can also be used to automatically generate nice usage help |
186 * text like you get when you run a program with `--help`. To use this, you | 194 * text like you get when you run a program with `--help`. To use this, you |
187 * will also want to provide some help text when you create your options. To | 195 * will also want to provide some help text when you create your options. To |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
241 | 249 |
242 /** | 250 /** |
243 * The commands that have been defined for this parser. | 251 * The commands that have been defined for this parser. |
244 */ | 252 */ |
245 final Map<String, ArgParser> commands = <String, ArgParser>{}; | 253 final Map<String, ArgParser> commands = <String, ArgParser>{}; |
246 | 254 |
247 /** Creates a new ArgParser. */ | 255 /** Creates a new ArgParser. */ |
248 ArgParser(); | 256 ArgParser(); |
249 | 257 |
250 /** | 258 /** |
251 * Defines a command. A command is a named argument which may in turn | 259 * Defines a command. |
252 * define its own options and subcommands. Returns an [ArgParser] that can | 260 * |
253 * be used to define the command's options. | 261 * A command is a named argument which may in turn define its own options and |
262 * subcommands using the given parser. If [parser] is omitted, implicitly | |
263 * creates a new one. Returns the parser for the command. | |
254 */ | 264 */ |
255 ArgParser addCommand(String name) { | 265 ArgParser addCommand(String name, [ArgParser parser]) { |
256 // Make sure the name isn't in use. | 266 // Make sure the name isn't in use. |
257 if (commands.containsKey(name)) { | 267 if (commands.containsKey(name)) { |
258 throw new ArgumentError('Duplicate command "$name".'); | 268 throw new ArgumentError('Duplicate command "$name".'); |
259 } | 269 } |
260 | 270 |
261 var command = new ArgParser(); | 271 if (parser == null) parser = new ArgParser(); |
262 commands[name] = command; | 272 commands[name] = parser; |
263 return command; | 273 return parser; |
264 } | 274 } |
265 | 275 |
266 /** | 276 /** |
267 * Defines a flag. Throws an [ArgumentError] if: | 277 * Defines a flag. Throws an [ArgumentError] if: |
268 * | 278 * |
269 * * There is already an option named [name]. | 279 * * There is already an option named [name]. |
270 * * There is already an option using abbreviation [abbr]. | 280 * * There is already an option using abbreviation [abbr]. |
271 */ | 281 */ |
272 void addFlag(String name, {String abbr, String help, bool defaultsTo: false, | 282 void addFlag(String name, {String abbr, String help, bool defaultsTo: false, |
273 bool negatable: true, void callback(bool value)}) { | 283 bool negatable: true, void callback(bool value)}) { |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
428 'Could not find an option named "$name".'); | 438 'Could not find an option named "$name".'); |
429 } | 439 } |
430 | 440 |
431 return _options[name]; | 441 return _options[name]; |
432 } | 442 } |
433 | 443 |
434 /** Get the names of the options as an [Iterable]. */ | 444 /** Get the names of the options as an [Iterable]. */ |
435 Iterable<String> get options => _options.keys; | 445 Iterable<String> get options => _options.keys; |
436 } | 446 } |
437 | 447 |
OLD | NEW |