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

Side by Side Diff: pkg/args/lib/args.dart

Issue 23603002: Copyedit pass over args library description; fixed some example code (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: tweak Created 7 years, 3 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 /** 5 /**
6 * This library lets you define parsers for parsing raw command-line arguments 6 * Parser support for transforming raw command-line arguments
7 * into a set of options and values using [GNU][] and [POSIX][] style options. 7 * into a set of options and values.
8 * 8 *
9 * ## Installing ## 9 * This library supports
10 * [GNU][] and [POSIX][] style options,
11 * and it works in both server-side and client-side apps.
10 * 12 *
11 * Use [pub][] to install this package. Add the following to your `pubspec.yaml` 13 * For information on installing and importing this library, see the
mem 2013/08/29 14:58:10 Given the detail of the rest of this library descr
Kathy Walrath 2013/09/09 23:26:26 Done.
12 * file. 14 * [args package on pub.dartlang.org]
15 * (http://pub.dartlang.org/packages/args).
13 * 16 *
14 * dependencies: 17 * ## Defining options
15 * args: any
16 * 18 *
17 * Then run `pub install`. 19 * To use this library, first create an [ArgParser]:
18 *
19 * For more information, see the
20 * [args package on pub.dartlang.org](http://pub.dartlang.org/packages/args).
21 *
22 * ## Defining options ##
23 *
24 * To use this library, you create an [ArgParser] object which will contain
25 * the set of options you support:
26 * 20 *
27 * var parser = new ArgParser(); 21 * var parser = new ArgParser();
28 * 22 *
29 * Then you define a set of options on that parser using [addOption()] and 23 * Then define a set of options on that parser using [addOption()] and
30 * [addFlag()]. The minimal way to create an option is: 24 * [addFlag()]. Here's the minimal way to create an option named "name":
31 * 25 *
32 * parser.addOption('name'); 26 * parser.addOption('name');
33 * 27 *
34 * This creates an option named "name". Options must be given a value on the 28 * When an option can't take a string value,
35 * command line. If you have a simple on/off flag, you can instead use: 29 * and instead is either set or unset,
mem 2013/08/29 14:58:10 maybe "When an option has only two values, either
Kathy Walrath 2013/09/09 23:26:26 Done.
30 * then use a flag:
36 * 31 *
37 * parser.addFlag('name'); 32 * parser.addFlag('name');
38 * 33 *
39 * Flag options will, by default, accept a 'no-' prefix to negate the option. 34 * Flag options, by default, accept a 'no-' prefix to negate the option.
40 * This can be disabled like so: 35 * You can disable the 'no-' prefix using the `negatable` parameter:
41 * 36 *
42 * parser.addFlag('name', negatable: false); 37 * parser.addFlag('name', negatable: false);
43 * 38 *
44 * (From here on out "option" will refer to both "regular" options and flags. 39 * **Terminology note:**
45 * In cases where the distinction matters, we'll use "non-flag option".) 40 * From here on out, the term _option_ refers to
41 * both regular options and flags.
42 * In cases where the distinction matters,
43 * this documentation uses the term _non-flag option._
46 * 44 *
47 * Options may have an optional single-character abbreviation: 45 * Options can have an optional single-character abbreviation,
46 * specified with the `abbr` parameter:
48 * 47 *
49 * parser.addOption('mode', abbr: 'm'); 48 * parser.addOption('mode', abbr: 'm');
50 * parser.addFlag('verbose', abbr: 'v'); 49 * parser.addFlag('verbose', abbr: 'v');
51 * 50 *
52 * They may also specify a default value. The default value will be used if the 51 * Options can also have a default value,
53 * option isn't provided: 52 * specified with the `defaultsTo` parameter.
53 * The default value is used when
54 * arguments don't specify the option.
54 * 55 *
55 * parser.addOption('mode', defaultsTo: 'debug'); 56 * parser.addOption('mode', defaultsTo: 'debug');
56 * parser.addFlag('verbose', defaultsTo: false); 57 * parser.addFlag('verbose', defaultsTo: false);
57 * 58 *
58 * The default value for non-flag options can be any [String]. For flags, it 59 * The default value for non-flag options can be any [String]. For flags, it
59 * must be a [bool]. 60 * must be a [bool].
60 * 61 *
61 * To validate non-flag options, you may provide an allowed set of values. When 62 * To validate a non-flag option,
62 * you do, it will throw a [FormatException] when you parse the arguments if 63 * you can use the `allowed` parameter
63 * the value for an option is not in the allowed set: 64 * to provide an allowed set of values. When
65 * you do, the parser throws a [FormatException] if
66 * the value for an option is not in the allowed set.
67 * Here's an example of specifying allowed values:
64 * 68 *
65 * parser.addOption('mode', allowed: ['debug', 'release']); 69 * parser.addOption('mode', allowed: ['debug', 'release']);
66 * 70 *
67 * You can provide a callback when you define an option. When you later parse 71 * You can use the `callback` parameter
68 * a set of arguments, the callback for that option will be invoked with the 72 * to associate a function with an option. Later, when
69 * value provided for it: 73 * parsing occurs, the callback function is invoked with the
74 * value of the option:
70 * 75 *
71 * parser.addOption('mode', callback: (mode) => print('Got mode $mode)); 76 * parser.addOption('mode', callback: (mode) => print('Got mode $mode));
72 * parser.addFlag('verbose', callback: (verbose) { 77 * parser.addFlag('verbose', callback: (verbose) {
73 * if (verbose) print('Verbose'); 78 * if (verbose) print('Verbose');
74 * }); 79 * });
75 * 80 *
76 * The callback for each option will *always* be called when you parse a set of 81 * The callbacks for all options are called whenever a set of arguments
77 * arguments. If the option isn't provided in the args, the callback will be 82 * is parsed. If an option isn't provided in the args, its callback is
78 * passed the default value, or `null` if there is none set. 83 * passed the default value, or `null` if no default value is set.
79 * 84 *
80 * ## Parsing arguments ## 85 * ## Parsing arguments
81 * 86 *
82 * Once you have an [ArgParser] set up with some options and flags, you use it 87 * Once you have an [ArgParser] set up with some options and flags, you use it
83 * by calling [ArgParser.parse()] with a set of arguments: 88 * by calling [ArgParser.parse()] with a set of arguments:
84 * 89 *
85 * var results = parser.parse(['some', 'command', 'line', 'args']); 90 * var results = parser.parse(['some', 'command', 'line', 'args']);
86 * 91 *
87 * These will usually come from `new Options().arguments`, but you can pass in 92 * These arguments usually come from dart:io's Options class
88 * any list of strings. It returns an instance of [ArgResults]. This is a 93 * (`new Options().arguments`),
89 * map-like object that will return the value of any parsed option. 94 * but you can pass in any list of strings.
95 * The parse() method returns an instance of [ArgResults],
96 * a map-like object that contains the values of the parsed options.
90 * 97 *
91 * var parser = new ArgParser(); 98 * var parser = new ArgParser();
92 * parser.addOption('mode'); 99 * parser.addOption('mode');
93 * parser.addFlag('verbose', defaultsTo: true); 100 * parser.addFlag('verbose', defaultsTo: true);
94 * var results = parser.parse('['--mode', 'debug', 'something', 'else']); 101 * var results = parser.parse(['--mode', 'debug', 'something', 'else']);
95 * 102 *
96 * print(results['mode']); // debug 103 * print(results['mode']); // debug
97 * print(results['verbose']); // true 104 * print(results['verbose']); // true
98 * 105 *
99 * The [parse()] method will stop as soon as it reaches `--` or anything that 106 * By default, the parse() method stops as soon as it reaches `--` by itself
100 * it doesn't recognize as an option, flag, or option value. If there are still 107 * or anything that the parser doesn't recognize
101 * arguments left, they will be provided to you in 108 * as an option, flag, or option value.
109 * If arguments still remain, they go into
102 * [ArgResults.rest]. 110 * [ArgResults.rest].
103 * 111 *
104 * print(results.rest); // ['something', 'else'] 112 * print(results.rest); // ['something', 'else']
105 * 113 *
106 * ## Specifying options ## 114 * To continue to parse options found after non-option arguments,
115 * call parse() with `allowTrailingOptions: true`.
116 *
117 * ## Specifying options
107 * 118 *
108 * To actually pass in options and flags on the command line, use GNU or POSIX 119 * To actually pass in options and flags on the command line, use GNU or POSIX
109 * style. If you define an option like: 120 * style. Consider this option:
110 * 121 *
111 * parser.addOption('name', abbr: 'n'); 122 * parser.addOption('name', abbr: 'n');
112 * 123 *
113 * Then a value for it can be specified on the command line using any of: 124 * You can specify its value on the command line using any of the following:
114 * 125 *
115 * --name=somevalue 126 * --name=somevalue
116 * --name somevalue 127 * --name somevalue
117 * -nsomevalue 128 * -nsomevalue
118 * -n somevalue 129 * -n somevalue
119 * 130 *
120 * Given this flag: 131 * Consider this flag:
121 * 132 *
122 * parser.addFlag('name', abbr: 'n'); 133 * parser.addFlag('name', abbr: 'n');
123 * 134 *
124 * You can set it on using one of: 135 * You can set it to true using one of the following:
125 * 136 *
126 * --name 137 * --name
127 * -n 138 * -n
128 * 139 *
129 * Or set it off using: 140 * You can set it to false using the following:
130 * 141 *
131 * --no-name 142 * --no-name
132 * 143 *
133 * Multiple flag abbreviation can also be collapsed into a single argument. If 144 * Multiple flag abbreviations can be collapsed into a single argument.
134 * you define: 145 * Say you define these flags:
135 * 146 *
136 * parser.addFlag('verbose', abbr: 'v'); 147 * parser.addFlag('verbose', abbr: 'v');
137 * parser.addFlag('french', abbr: 'f'); 148 * parser.addFlag('french', abbr: 'f');
138 * parser.addFlag('iambic-pentameter', abbr: 'i'); 149 * parser.addFlag('iambic-pentameter', abbr: 'i');
139 * 150 *
140 * Then all three flags could be set using: 151 * You can set all three flags at once:
141 * 152 *
142 * -vfi 153 * -vfi
143 * 154 *
144 * By default, an option has only a single value, with later option values 155 * By default, an option has only a single value, with later option values
145 * overriding earlier ones; for example: 156 * overriding earlier ones; for example:
146 * 157 *
147 * var parser = new ArgParser(); 158 * var parser = new ArgParser();
148 * parser.addOption('mode'); 159 * parser.addOption('mode');
149 * var results = parser.parse(['--mode', 'on', '--mode', 'off']); 160 * var results = parser.parse(['--mode', 'on', '--mode', 'off']);
150 * print(results['mode']); // prints 'off' 161 * print(results['mode']); // prints 'off'
151 * 162 *
152 * If you need multiple values, set the [allowMultiple] flag. In that 163 * If you need multiple values, set the `allowMultiple` parameter. In that
153 * case the option can occur multiple times and when parsing arguments a 164 * case the option can occur multiple times, and the parse() method returns a
154 * List of values will be returned: 165 * list of values:
155 * 166 *
156 * var parser = new ArgParser(); 167 * var parser = new ArgParser();
157 * parser.addOption('mode', allowMultiple: true); 168 * parser.addOption('mode', allowMultiple: true);
158 * var results = parser.parse(['--mode', 'on', '--mode', 'off']); 169 * var results = parser.parse(['--mode', 'on', '--mode', 'off']);
159 * print(results['mode']); // prints '[on, off]' 170 * print(results['mode']); // prints '[on, off]'
160 * 171 *
161 * ## Defining commands ## 172 * ## Defining commands ##
162 * 173 *
163 * In addition to *options*, you can also define *commands*. A command is a 174 * In addition to *options*, you can also define *commands*. A command is a
164 * named argument that has its own set of options. For example, when you run: 175 * named argument that has its own set of options. For example,
176 * consider this shell command:
165 * 177 *
166 * $ git commit -a 178 * $ git commit -a
167 * 179 *
168 * The executable is `git`, the command is `commit`, and the `-a` option is an 180 * The executable is `git`, the command is `commit`, and the `-a` option is an
169 * option passed to the command. You can add a command like so: 181 * option passed to the command. You can add a command using
182 * the [addCommand] method:
170 * 183 *
171 * var parser = new ArgParser(); 184 * var parser = new ArgParser();
172 * var command = parser.addCommand('commit'); 185 * var command = parser.addCommand('commit');
173 * 186 *
174 * It returns another [ArgParser] which you can then use to define options 187 * The addCommand() method
188 * returns another [ArgParser], which you can then use to define options
175 * specific to that command. If you already have an [ArgParser] for the 189 * specific to that command. If you already have an [ArgParser] for the
176 * command's options, you can pass it to [addCommand]: 190 * command's options, you can pass it to addCommand:
177 * 191 *
178 * var parser = new ArgParser(); 192 * var parser = new ArgParser();
179 * var command = new ArgParser(); 193 * var command = new ArgParser();
180 * parser.addCommand('commit', command); 194 * parser.addCommand('commit', command);
181 * 195 *
182 * The [ArgParser] for a command can then define whatever options or flags: 196 * The [ArgParser] for a command can then define options or flags:
183 * 197 *
184 * command.addFlag('all', abbr: 'a'); 198 * command.addFlag('all', abbr: 'a');
185 * 199 *
186 * You can add multiple commands to the same parser so that a user can select 200 * You can add multiple commands to the same parser so that a user can select
187 * one from a range of possible commands. When an argument list is parsed, 201 * one from a range of possible commands. When parsing an argument list,
188 * you can then determine which command was entered and what options were 202 * you can then determine which command was entered and what options were
189 * provided for it. 203 * provided for it.
190 * 204 *
191 * var results = parser.parse(['commit', '-a']); 205 * var results = parser.parse(['commit', '-a']);
192 * print(results.command.name); // "commit" 206 * print(results.command.name); // "commit"
193 * print(results.command['a']); // true 207 * print(results.command['all']); // true
194 * 208 *
195 * Options for a command must appear after the command in the argument list. 209 * Options for a command must appear after the command in the argument list.
196 * For example, given the above parser, "git -a commit" is *not* valid. The 210 * For example, given the above parser, "git -a commit" is *not* valid. The
197 * parser will try to find the right-most command that accepts an option. For 211 * parser tries to find the right-most command that accepts an option. For
198 * example: 212 * example:
199 * 213 *
200 * var parser = new ArgParser(); 214 * var parser = new ArgParser();
201 * parser.addFlag('all', abbr: 'a'); 215 * parser.addFlag('all', abbr: 'a');
202 * var command = new ArgParser().addCommand('commit'); 216 * var command = parser.addCommand('commit');
203 * parser.addFlag('all', abbr: 'a'); 217 * command.addFlag('all', abbr: 'a');
218 *
204 * var results = parser.parse(['commit', '-a']); 219 * var results = parser.parse(['commit', '-a']);
205 * print(results.command['a']); // true 220 * print(results.command['all']); // true
206 * 221 *
207 * Here, both the top-level parser and the "commit" command can accept a "-a" 222 * Here, both the top-level parser and the "commit" command can accept a "-a"
208 * (which is probably a bad command line interface, admittedly). In that case, 223 * (which is probably a bad command line interface, admittedly). In that case,
209 * when "-a" appears after "commit", it will be applied to that command. If it 224 * when "-a" appears after "commit", it is applied to that command. If it
210 * appears to the left of "commit", it will be given to the top-level parser. 225 * appears to the left of "commit", it is given to the top-level parser.
211 * 226 *
212 * ## Displaying usage ## 227 * ## Displaying usage
213 * 228 *
214 * This library can also be used to automatically generate nice usage help 229 * This library can automatically generate nice usage help
mem 2013/08/29 14:58:10 This sentence is awkward.
Kathy Walrath 2013/09/09 23:26:26 Done.
215 * text like you get when you run a program with `--help`. To use this, you 230 * text like you get when you run a program with `--help`.
216 * will also want to provide some help text when you create your options. To 231 * To display good usage information, you
217 * define help text for the entire option, do: 232 * should provide some help text when you create your options. To
233 * define help text for an entire option, use the `help` parameter:
218 * 234 *
219 * parser.addOption('mode', help: 'The compiler configuration', 235 * parser.addOption('mode', help: 'The compiler configuration',
220 * allowed: ['debug', 'release']); 236 * allowed: ['debug', 'release']);
221 * parser.addFlag('verbose', help: 'Show additional diagnostic info'); 237 * parser.addFlag('verbose', help: 'Show additional diagnostic info');
222 * 238 *
223 * For non-flag options, you can also provide detailed help for each expected 239 * For non-flag options, you can also provide detailed help for each expected
224 * value using a map: 240 * value by using the `allowedHelp` parameter:
225 * 241 *
226 * parser.addOption('arch', help: 'The architecture to compile for', 242 * parser.addOption('arch', help: 'The architecture to compile for',
227 * allowedHelp: { 243 * allowedHelp: {
228 * 'ia32': 'Intel x86', 244 * 'ia32': 'Intel x86',
229 * 'arm': 'ARM Holding 32-bit chip' 245 * 'arm': 'ARM Holding 32-bit chip'
230 * }); 246 * });
231 * 247 *
232 * If you define a set of options like the above, then calling this: 248 * To display the help, use the ArgParser getUsage() method:
233 * 249 *
234 * print(parser.getUsage()); 250 * print(parser.getUsage());
235 * 251 *
236 * Will display something like: 252 * The resulting string looks something like this:
237 * 253 *
238 * --mode The compiler configuration 254 * --mode The compiler configuration
239 * [debug, release] 255 * [debug, release]
240 * 256 *
241 * --[no-]verbose Show additional diagnostic info 257 * --[no-]verbose Show additional diagnostic info
242 * --arch The architecture to compile for 258 * --arch The architecture to compile for
243 * 259 *
244 * [arm] ARM Holding 32-bit chip 260 * [arm] ARM Holding 32-bit chip
245 * [ia32] Intel x86 261 * [ia32] Intel x86
246 * 262 *
247 * To assist the formatting of the usage help, single line help text will 263 * To assist the formatting of the usage help, single-line help text
248 * be followed by a single new line. Options with multi-line help text 264 * is followed by a single new line. Options with multi-line help text
249 * will be followed by two new lines. This provides spatial diversity between 265 * are followed by two new lines. This provides spatial diversity between
250 * options. 266 * options.
251 * 267 *
252 * [posix]: http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap12.h tml#tag_12_02 268 * [posix]: http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap12.h tml#tag_12_02
253 * [gnu]: http://www.gnu.org/prep/standards/standards.html#Command_002dLine-Inte rfaces 269 * [gnu]: http://www.gnu.org/prep/standards/standards.html#Command_002dLine-Inte rfaces
254 * [pub]: http://pub.dartlang.org
255 */ 270 */
256 library args; 271 library args;
257 272
258 import 'package:unmodifiable_collection/unmodifiable_collection.dart'; 273 import 'package:unmodifiable_collection/unmodifiable_collection.dart';
259 274
260 import 'src/parser.dart'; 275 import 'src/parser.dart';
261 import 'src/usage.dart'; 276 import 'src/usage.dart';
262 import 'src/options.dart'; 277 import 'src/options.dart';
263 export 'src/options.dart'; 278 export 'src/options.dart';
264 279
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after
439 'Could not find an option named "$name".'); 454 'Could not find an option named "$name".');
440 } 455 }
441 456
442 return _options[name]; 457 return _options[name];
443 } 458 }
444 459
445 /** Get the names of the options as an [Iterable]. */ 460 /** Get the names of the options as an [Iterable]. */
446 Iterable<String> get options => _options.keys; 461 Iterable<String> get options => _options.keys;
447 } 462 }
448 463
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698