Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(254)

Side by Side Diff: pkg/args/lib/args.dart

Issue 15621002: Allow passing in an existing ArgParser for a command. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: More docs. Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | pkg/args/test/command_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
162 * 162 *
163 * In addition to *options*, you can also define *commands*. A command is a 163 * In addition to *options*, you can also define *commands*. A command is a
164 * named argument that has its own set of options. For example, when you run: 164 * named argument that has its own set of options. For example, when you run:
165 * 165 *
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 *
174 * It returns another [ArgParser] which you can then use to define options
175 * specific to that command. If you already have an [ArgParser] for the
176 * command's options, you can pass it to [addCommand]:
177 *
178 * var parser = new ArgParser();
179 * var command = new ArgParser();
180 * parser.addCommand('commit', command);
181 *
182 * The [ArgParser] for a command can then define whatever options or flags:
183 *
173 * command.addFlag('all', abbr: 'a'); 184 * command.addFlag('all', abbr: 'a');
174 * 185 *
175 * It returns another [ArgParser] which you can use to define options and 186 * You can add multiple commands to the same parser so that a user can select
176 * subcommands on that command. When an argument list is parsed, you can then 187 * one from a range of possible commands. When an argument list is parsed,
177 * determine which command was entered and what options were provided for it. 188 * you can then determine which command was entered and what options were
189 * provided for it.
178 * 190 *
179 * var results = parser.parse(['commit', '-a']); 191 * var results = parser.parse(['commit', '-a']);
180 * print(results.command.name); // "commit" 192 * print(results.command.name); // "commit"
181 * print(results.command['a']); // true 193 * print(results.command['a']); // true
182 * 194 *
195 * Options for a command must appear after the command in the argument list.
196 * For example, given the above parser, "git -a commit" is *not* valid. The
197 * parser will try to find the right-most command that accepts an option. For
198 * example:
199 *
200 * var parser = new ArgParser();
201 * parser.addFlag('all', abbr: 'a');
202 * var command = new ArgParser().addCommand('commit');
203 * parser.addFlag('all', abbr: 'a');
204 * var results = parser.parse(['commit', '-a']);
205 * print(results.command['a']); // true
206 *
207 * Here, both the top-level parser and the "commit" command can accept a "-a"
208 * (which is probably a bad command line interface, admittedly). In that case,
209 * when "-a" appears after "commit", it will be applied to that command. If it
210 * appears to the left of "commit", it will be given to the top-level parser.
211 *
183 * ## Displaying usage ## 212 * ## Displaying usage ##
184 * 213 *
185 * This library can also be used to automatically generate nice usage help 214 * 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 215 * 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 216 * will also want to provide some help text when you create your options. To
188 * define help text for the entire option, do: 217 * define help text for the entire option, do:
189 * 218 *
190 * parser.addOption('mode', help: 'The compiler configuration', 219 * parser.addOption('mode', help: 'The compiler configuration',
191 * allowed: ['debug', 'release']); 220 * allowed: ['debug', 'release']);
192 * parser.addFlag('verbose', help: 'Show additional diagnostic info'); 221 * parser.addFlag('verbose', help: 'Show additional diagnostic info');
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
241 270
242 /** 271 /**
243 * The commands that have been defined for this parser. 272 * The commands that have been defined for this parser.
244 */ 273 */
245 final Map<String, ArgParser> commands = <String, ArgParser>{}; 274 final Map<String, ArgParser> commands = <String, ArgParser>{};
246 275
247 /** Creates a new ArgParser. */ 276 /** Creates a new ArgParser. */
248 ArgParser(); 277 ArgParser();
249 278
250 /** 279 /**
251 * Defines a command. A command is a named argument which may in turn 280 * Defines a command.
252 * define its own options and subcommands. Returns an [ArgParser] that can 281 *
253 * be used to define the command's options. 282 * A command is a named argument which may in turn define its own options and
283 * subcommands using the given parser. If [parser] is omitted, implicitly
284 * creates a new one. Returns the parser for the command.
254 */ 285 */
255 ArgParser addCommand(String name) { 286 ArgParser addCommand(String name, [ArgParser parser]) {
256 // Make sure the name isn't in use. 287 // Make sure the name isn't in use.
257 if (commands.containsKey(name)) { 288 if (commands.containsKey(name)) {
258 throw new ArgumentError('Duplicate command "$name".'); 289 throw new ArgumentError('Duplicate command "$name".');
259 } 290 }
260 291
261 var command = new ArgParser(); 292 if (parser == null) parser = new ArgParser();
262 commands[name] = command; 293 commands[name] = parser;
263 return command; 294 return parser;
264 } 295 }
265 296
266 /** 297 /**
267 * Defines a flag. Throws an [ArgumentError] if: 298 * Defines a flag. Throws an [ArgumentError] if:
268 * 299 *
269 * * There is already an option named [name]. 300 * * There is already an option named [name].
270 * * There is already an option using abbreviation [abbr]. 301 * * There is already an option using abbreviation [abbr].
271 */ 302 */
272 void addFlag(String name, {String abbr, String help, bool defaultsTo: false, 303 void addFlag(String name, {String abbr, String help, bool defaultsTo: false,
273 bool negatable: true, void callback(bool value)}) { 304 bool negatable: true, void callback(bool value)}) {
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
428 'Could not find an option named "$name".'); 459 'Could not find an option named "$name".');
429 } 460 }
430 461
431 return _options[name]; 462 return _options[name];
432 } 463 }
433 464
434 /** Get the names of the options as an [Iterable]. */ 465 /** Get the names of the options as an [Iterable]. */
435 Iterable<String> get options => _options.keys; 466 Iterable<String> get options => _options.keys;
436 } 467 }
437 468
OLDNEW
« no previous file with comments | « no previous file | pkg/args/test/command_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698