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

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: 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 set by every subclass.
Bob Nystrom 2014/12/11 20:25:30 This is confusing. You aren't setting fields here,
nweiz 2014/12/11 23:55:24 Changed "set" to "defined". I still find defining
Bob Nystrom 2014/12/12 18:13:22 That still isn't correct. Subclasses don't have to
nweiz 2014/12/16 02:07:44 Changed to "property" which matches dartdoc.
Bob Nystrom 2014/12/16 16:50:41 Property is perfect, thanks. I just don't want use
216 final String name = "commit";
217 final String 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 void run() {
Bob Nystrom 2014/12/11 20:25:30 It would be good to mention how asynchrony is hand
nweiz 2014/12/11 23:55:24 Done.
225 // [options] is set before [run()] is called and contains the options
226 // passed to this command.
227 print(options['all']);
228 }
229 }
230
231 Commands can also have subcommands, which are added with [addSubcommand][]. A
232 command with subcommands can't run its own code, so [run][] doesn't need to be
Bob Nystrom 2014/12/11 20:25:30 Why can't parent commands also be run?
nweiz 2014/12/11 23:55:24 Because the pub command runner didn't support it a
Bob Nystrom 2014/12/12 18:13:22 This CL isn't urgent, though, is it? I could be w
nweiz 2014/12/16 02:07:43 Well, it's blocking further work on the developmen
Bob Nystrom 2014/12/16 16:50:41 I was thinking it might make it more natural for C
233 implemented. For example:
234
235 class StashCommand extends Command {
236 final String name = "stash";
237 final String description = "Stash changes in the working directory.";
238
239 StashCommand() {
240 addSubcommand(new StashSaveCommand());
241 addSubcommand(new StashListCommand());
242 }
243 }
244
245 [CommandRunner][] will automatically add a `help` command that displays usage
Bob Nystrom 2014/12/11 20:25:30 Present tense.
nweiz 2014/12/11 23:55:23 Done.
246 information for commands, as well as support for the `--help` flag for all
247 commands. If it encounters an error parsing the arguments or processing a
248 command, it will throw a [UsageError][]; the `main()` method should catch these
Bob Nystrom 2014/12/11 20:25:30 "the" -> "your".
nweiz 2014/12/11 23:55:24 Done.
249 and print them appropriately. For example:
250
251 runner.run(arguments).catchError((error) {
252 if (error is! UsageError) throw error;
253 print(error);
254 exit(64); // Exit code 64 indicates a usage error.
255 });
256
200 ## Displaying usage 257 ## Displaying usage
201 258
202 You can automatically generate nice help text, suitable for use as the output of 259 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 260 `--help`. To display good usage information, you should provide some help text
204 when you create your options. 261 when you create your options.
205 262
206 To define help text for an entire option, use the `help:` parameter: 263 To define help text for an entire option, use the `help:` parameter:
207 264
208 parser.addOption('mode', help: 'The compiler configuration', 265 parser.addOption('mode', help: 'The compiler configuration',
209 allowed: ['debug', 'release']); 266 allowed: ['debug', 'release']);
(...skipping 23 matching lines...) Expand all
233 [debug, release] 290 [debug, release]
234 291
235 --out=<path> The output path 292 --out=<path> The output path
236 --[no-]verbose Show additional diagnostic info 293 --[no-]verbose Show additional diagnostic info
237 --arch The architecture to compile for 294 --arch The architecture to compile for
238 [arm] ARM Holding 32-bit chip 295 [arm] ARM Holding 32-bit chip
239 [ia32] Intel x86 296 [ia32] Intel x86
240 297
241 [posix]: http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap12.html #tag_12_02 298 [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 299 [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 300 [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 301 [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 302 [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 303 [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 304 [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 305 [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 306 [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 307 [parse]: http://www.dartdocs.org/documentation/args/latest/index.html#args/args. ArgParser@id_parse
308 [rest]: http://www.dartdocs.org/documentation/args/latest/index.html#args/args.A rgResults@id_rest
309 [addCommand]: http://www.dartdocs.org/documentation/args/latest/index.html#args/ args.ArgParser@id_addCommand
310 [getUsage]: http://www.dartdocs.org/documentation/args/latest/index.html#args/ar gs.ArgParser@id_getUsage
311 [addSubcommand]: http://www.dartdocs.org/documentation/args/latest/index.html#ar gs/args.Command@id_addSubcommand
312 [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