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

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: 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..182ec0a8be3fcd638f3d0446dded1c542db6923f 100644
--- a/pkg/args/lib/args.dart
+++ b/pkg/args/lib/args.dart
@@ -173,8 +173,16 @@
* 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.
+ * subcommands on 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();
+ * command.addFlag('all', abbr: 'a');
+ * parser.addCommand("commit", command);
+ *
+ * When an argument list is parsed, you can then determine which command was
+ * entered and what options were provided for it.
gram 2013/05/21 23:11:07 Can you elaborate more here? Can an argparser have
Bob Nystrom 2013/05/21 23:20:15 There's lots of tests for these cases, but little
*
* var results = parser.parse(['commit', '-a']);
* print(results.command.name); // "commit"
@@ -248,19 +256,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