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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | pkg/args/test/command_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/args/lib/args.dart
diff --git a/pkg/args/lib/args.dart b/pkg/args/lib/args.dart
index be1a6efa4d819550aee0c9a8819d7c361ac5c2a3..3898b9fd1aafdbb0b778b8d1f9b6be8f20170b74 100644
--- a/pkg/args/lib/args.dart
+++ b/pkg/args/lib/args.dart
@@ -169,17 +169,46 @@
* option passed to the command. You can add a command like so:
*
* var parser = new ArgParser();
- * var command = parser.addCommand("commit");
+ * var command = parser.addCommand('commit');
+ *
+ * It returns another [ArgParser] which you can then use to define options
+ * specific to that command. If you already have an [ArgParser] for the
+ * command's options, you can pass it to [addCommand]:
+ *
+ * var parser = new ArgParser();
+ * var command = new ArgParser();
+ * parser.addCommand('commit', command);
+ *
+ * The [ArgParser] for a command can then define whatever options or flags:
+ *
* command.addFlag('all', abbr: 'a');
*
- * It returns another [ArgParser] which you can use to define options and
- * subcommands on that command. When an argument list is parsed, you can then
- * determine which command was entered and what options were provided for it.
+ * You can add multiple commands to the same parser so that a user can select
+ * one from a range of possible commands. When an argument list is parsed,
+ * you can then determine which command was entered and what options were
+ * provided for it.
*
* var results = parser.parse(['commit', '-a']);
* print(results.command.name); // "commit"
* print(results.command['a']); // true
*
+ * Options for a command must appear after the command in the argument list.
+ * For example, given the above parser, "git -a commit" is *not* valid. The
+ * parser will try to find the right-most command that accepts an option. For
+ * example:
+ *
+ * var parser = new ArgParser();
+ * parser.addFlag('all', abbr: 'a');
+ * var command = new ArgParser().addCommand('commit');
+ * parser.addFlag('all', abbr: 'a');
+ * var results = parser.parse(['commit', '-a']);
+ * print(results.command['a']); // true
+ *
+ * Here, both the top-level parser and the "commit" command can accept a "-a"
+ * (which is probably a bad command line interface, admittedly). In that case,
+ * when "-a" appears after "commit", it will be applied to that command. If it
+ * appears to the left of "commit", it will be given to the top-level parser.
+ *
* ## Displaying usage ##
*
* This library can also be used to automatically generate nice usage help
@@ -248,19 +277,21 @@ class ArgParser {
ArgParser();
/**
- * Defines a command. A command is a named argument which may in turn
- * define its own options and subcommands. Returns an [ArgParser] that can
- * be used to define the command's options.
+ * Defines a command.
+ *
+ * A command is a named argument which may in turn define its own options and
+ * subcommands using the given parser. If [parser] is omitted, implicitly
+ * creates a new one. Returns the parser for the command.
*/
- ArgParser addCommand(String name) {
+ ArgParser addCommand(String name, [ArgParser parser]) {
// Make sure the name isn't in use.
if (commands.containsKey(name)) {
throw new ArgumentError('Duplicate command "$name".');
}
- var command = new ArgParser();
- commands[name] = command;
- return command;
+ if (parser == null) parser = new ArgParser();
+ commands[name] = parser;
+ return parser;
}
/**
« 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