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

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] fields must be defined by every subclass.
216 final name = "commit";
217 final description = "Record changes to the repository.";
218
219 CommitCommand() {
220 // [argParser] is automatically created by the parent class.
221 argParser.addFlag('all', abbr: 'a');
222 }
223
224 // [run] may also return a Future.
225 void run() {
226 // [options] is set before [run()] is called and contains the options
227 // passed to this command.
228 print(options['all']);
229 }
230 }
231
232 Commands can also have subcommands, which are added with [addSubcommand][]. A
233 command with subcommands can't run its own code, so [run][] doesn't need to be
234 implemented. For example:
235
236 class StashCommand extends Command {
237 final String name = "stash";
238 final String description = "Stash changes in the working directory.";
239
240 StashCommand() {
241 addSubcommand(new StashSaveCommand());
242 addSubcommand(new StashListCommand());
243 }
244 }
245
246 [CommandRunner][] automatically adds a `help` command that displays usage
247 information for commands, as well as support for the `--help` flag for all
248 commands. If it encounters an error parsing the arguments or processing a
249 command, it throws a [UsageError][]; your `main()` method should catch these and
250 print them appropriately. For example:
251
252 runner.run(arguments).catchError((error) {
253 if (error is! UsageError) throw error;
254 print(error);
255 exit(64); // Exit code 64 indicates a usage error.
256 });
257
200 ## Displaying usage 258 ## Displaying usage
201 259
202 You can automatically generate nice help text, suitable for use as the output of 260 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 261 `--help`. To display good usage information, you should provide some help text
204 when you create your options. 262 when you create your options.
205 263
206 To define help text for an entire option, use the `help:` parameter: 264 To define help text for an entire option, use the `help:` parameter:
207 265
208 parser.addOption('mode', help: 'The compiler configuration', 266 parser.addOption('mode', help: 'The compiler configuration',
209 allowed: ['debug', 'release']); 267 allowed: ['debug', 'release']);
(...skipping 23 matching lines...) Expand all
233 [debug, release] 291 [debug, release]
234 292
235 --out=<path> The output path 293 --out=<path> The output path
236 --[no-]verbose Show additional diagnostic info 294 --[no-]verbose Show additional diagnostic info
237 --arch The architecture to compile for 295 --arch The architecture to compile for
238 [arm] ARM Holding 32-bit chip 296 [arm] ARM Holding 32-bit chip
239 [ia32] Intel x86 297 [ia32] Intel x86
240 298
241 [posix]: http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap12.html #tag_12_02 299 [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 300 [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 301 [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 302 [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 303 [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 304 [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 305 [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 306 [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 307 [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 308 [parse]: http://www.dartdocs.org/documentation/args/latest/index.html#args/args. ArgParser@id_parse
309 [rest]: http://www.dartdocs.org/documentation/args/latest/index.html#args/args.A rgResults@id_rest
310 [addCommand]: http://www.dartdocs.org/documentation/args/latest/index.html#args/ args.ArgParser@id_addCommand
311 [getUsage]: http://www.dartdocs.org/documentation/args/latest/index.html#args/ar gs.ArgParser@id_getUsage
312 [addSubcommand]: http://www.dartdocs.org/documentation/args/latest/index.html#ar gs/args.Command@id_addSubcommand
313 [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