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

Side by Side Diff: pkg/args/README.md

Issue 797473002: Add a CommandRunner class for dispatching commands to args. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Code review changes Created 6 years 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
OLDNEW
1 Parses raw command-line arguments into a set of options and values. 1 Parses raw command-line arguments into a set of options and values.
2 2
3 This library supports [GNU][] and [POSIX][] style options, and it works 3 This library supports [GNU][] and [POSIX][] style options, and it works
4 in both server-side and client-side apps. 4 in both server-side and client-side apps.
5 5
6 ## Defining options 6 ## Defining options
7 7
8 First create an [ArgParser][]: 8 First create an [ArgParser][]:
9 9
10 var parser = new ArgParser(); 10 var parser = new ArgParser();
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after
190 command.addFlag('all', abbr: 'a'); 190 command.addFlag('all', abbr: 'a');
191 191
192 var results = parser.parse(['commit', '-a']); 192 var results = parser.parse(['commit', '-a']);
193 print(results.command['all']); // true 193 print(results.command['all']); // true
194 194
195 Here, both the top-level parser and the `"commit"` command can accept a `"-a"` 195 Here, both the top-level parser and the `"commit"` command can accept a `"-a"`
196 (which is probably a bad command line interface, admittedly). In that case, when 196 (which is probably a bad command line interface, admittedly). In that case, when
197 `"-a"` appears after `"commit"`, it is applied to that command. If it appears to 197 `"-a"` appears after `"commit"`, it is applied to that command. If it appears to
198 the left of `"commit"`, it is given to the top-level parser. 198 the left of `"commit"`, it is given to the top-level parser.
199 199
200 ## Dispatching Commands
201
202 If you're writing a command-based application, you can use the [CommandRunner][]
203 and [Command][] classes to help structure it. [CommandRunner][] has built-in
204 support for dispatching to [Command][]s based on command-line arguments, as well
205 as handling `--help` flags and invalid arguments. For example:
206
207 var runner = new CommandRunner("git", "Distributed version control.");
208 runner.addCommand(new CommitCommand());
209 runner.addCommand(new StashCommand());
210 runner.run(['commit', '-a']); // Calls [CommitCommand.run()]
211
212 Custom commands are defined by extending the [Command][] class. For example:
213
214 class CommitCommand extends Command {
215 // The [name] and [description] properties must be defined by every
216 // subclass.
217 final name = "commit";
218 final description = "Record changes to the repository.";
219
220 CommitCommand() {
221 // [argParser] is automatically created by the parent class.
222 argParser.addFlag('all', abbr: 'a');
223 }
224
225 // [run] may also return a Future.
226 void run() {
227 // [options] is set before [run()] is called and contains the options
228 // passed to this command.
229 print(options['all']);
230 }
231 }
232
233 Commands can also have subcommands, which are added with [addSubcommand][]. A
234 command with subcommands can't run its own code, so [run][] doesn't need to be
235 implemented. For example:
236
237 class StashCommand extends Command {
238 final String name = "stash";
239 final String description = "Stash changes in the working directory.";
240
241 StashCommand() {
242 addSubcommand(new StashSaveCommand());
243 addSubcommand(new StashListCommand());
244 }
245 }
246
247 [CommandRunner][] automatically adds a `help` command that displays usage
248 information for commands, as well as support for the `--help` flag for all
249 commands. If it encounters an error parsing the arguments or processing a
250 command, it throws a [UsageError][]; your `main()` method should catch these and
251 print them appropriately. For example:
252
253 runner.run(arguments).catchError((error) {
254 if (error is! UsageError) throw error;
255 print(error);
256 exit(64); // Exit code 64 indicates a usage error.
257 });
258
200 ## Displaying usage 259 ## Displaying usage
201 260
202 You can automatically generate nice help text, suitable for use as the output of 261 You can automatically generate nice help text, suitable for use as the output of
203 `--help`. To display good usage information, you should provide some help text 262 `--help`. To display good usage information, you should provide some help text
204 when you create your options. 263 when you create your options.
205 264
206 To define help text for an entire option, use the `help:` parameter: 265 To define help text for an entire option, use the `help:` parameter:
207 266
208 parser.addOption('mode', help: 'The compiler configuration', 267 parser.addOption('mode', help: 'The compiler configuration',
209 allowed: ['debug', 'release']); 268 allowed: ['debug', 'release']);
(...skipping 23 matching lines...) Expand all
233 [debug, release] 292 [debug, release]
234 293
235 --out=<path> The output path 294 --out=<path> The output path
236 --[no-]verbose Show additional diagnostic info 295 --[no-]verbose Show additional diagnostic info
237 --arch The architecture to compile for 296 --arch The architecture to compile for
238 [arm] ARM Holding 32-bit chip 297 [arm] ARM Holding 32-bit chip
239 [ia32] Intel x86 298 [ia32] Intel x86
240 299
241 [posix]: http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap12.html #tag_12_02 300 [posix]: http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap12.html #tag_12_02
242 [gnu]: http://www.gnu.org/prep/standards/standards.html#Command_002dLine-Interfa ces 301 [gnu]: http://www.gnu.org/prep/standards/standards.html#Command_002dLine-Interfa ces
243 [ArgParser]: https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/arg s/args.ArgParser 302 [ArgParser]: http://www.dartdocs.org/documentation/args/latest/index.html#args/a rgs.ArgParser
244 [ArgResults]: https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/ar gs/args.ArgResults 303 [ArgResults]: http://www.dartdocs.org/documentation/args/latest/index.html#args/ args.ArgResults
245 [addOption]: https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/arg s/args.ArgParser#id_addOption 304 [CommandRunner]: http://www.dartdocs.org/documentation/args/latest/index.html#ar gs/args.CommandRunner
246 [addFlag]: https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/args/ args.ArgParser#id_addFlag 305 [Command]: http://www.dartdocs.org/documentation/args/latest/index.html#args/arg s.Command
247 [parse]: https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/args/ar gs.ArgParser#id_parse 306 [UsageError]: http://www.dartdocs.org/documentation/args/latest/index.html#args/ args.UsageError
248 [rest]: https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/args/arg s.ArgResults#id_rest 307 [addOption]: http://www.dartdocs.org/documentation/args/latest/index.html#args/a rgs.ArgParser@id_addOption
249 [addCommand]: https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/ar gs/args.ArgParser#id_addCommand 308 [addFlag]: http://www.dartdocs.org/documentation/args/latest/index.html#args/arg s.ArgParser@id_addFlag
250 [getUsage]: https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/args /args.ArgParser#id_getUsage 309 [parse]: http://www.dartdocs.org/documentation/args/latest/index.html#args/args. ArgParser@id_parse
310 [rest]: http://www.dartdocs.org/documentation/args/latest/index.html#args/args.A rgResults@id_rest
311 [addCommand]: http://www.dartdocs.org/documentation/args/latest/index.html#args/ args.ArgParser@id_addCommand
312 [getUsage]: http://www.dartdocs.org/documentation/args/latest/index.html#args/ar gs.ArgParser@id_getUsage
313 [addSubcommand]: http://www.dartdocs.org/documentation/args/latest/index.html#ar gs/args.Command@id_addSubcommand
314 [run]: http://www.dartdocs.org/documentation/args/latest/index.html#args/args.Co mmand@id_run
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698