OLD | NEW |
| (Empty) |
1 Parses raw command-line arguments into a set of options and values. | |
2 | |
3 This library supports [GNU][] and [POSIX][] style options, and it works | |
4 in both server-side and client-side apps. | |
5 | |
6 ## Defining options | |
7 | |
8 First create an [ArgParser][]: | |
9 | |
10 var parser = new ArgParser(); | |
11 | |
12 Then define a set of options on that parser using [addOption()][addOption] and | |
13 [addFlag()][addFlag]. Here's the minimal way to create an option named "name": | |
14 | |
15 parser.addOption('name'); | |
16 | |
17 When an option can only be set or unset (as opposed to taking a string value), | |
18 use a flag: | |
19 | |
20 parser.addFlag('name'); | |
21 | |
22 Flag options, by default, accept a 'no-' prefix to negate the option. You can | |
23 disable the 'no-' prefix using the `negatable` parameter: | |
24 | |
25 parser.addFlag('name', negatable: false); | |
26 | |
27 *Note:* From here on out, "option" refers to both regular options and flags. In | |
28 cases where the distinction matters, we'll use "non-flag option." | |
29 | |
30 Options can have an optional single-character abbreviation, specified with the | |
31 `abbr` parameter: | |
32 | |
33 parser.addOption('mode', abbr: 'm'); | |
34 parser.addFlag('verbose', abbr: 'v'); | |
35 | |
36 Options can also have a default value, specified with the `defaultsTo` | |
37 parameter. The default value is used when arguments don't specify the option. | |
38 | |
39 parser.addOption('mode', defaultsTo: 'debug'); | |
40 parser.addFlag('verbose', defaultsTo: false); | |
41 | |
42 The default value for non-flag options can be any string. For flags, it must | |
43 be a `bool`. | |
44 | |
45 To validate a non-flag option, you can use the `allowed` parameter to provide an | |
46 allowed set of values. When you do, the parser throws a [FormatException] if the | |
47 value for an option is not in the allowed set. Here's an example of specifying | |
48 allowed values: | |
49 | |
50 parser.addOption('mode', allowed: ['debug', 'release']); | |
51 | |
52 You can use the `callback` parameter to associate a function with an option. | |
53 Later, when parsing occurs, the callback function is invoked with the value of | |
54 the option: | |
55 | |
56 parser.addOption('mode', callback: (mode) => print('Got mode $mode')); | |
57 parser.addFlag('verbose', callback: (verbose) { | |
58 if (verbose) print('Verbose'); | |
59 }); | |
60 | |
61 The callbacks for all options are called whenever a set of arguments is parsed. | |
62 If an option isn't provided in the args, its callback is passed the default | |
63 value, or `null` if no default value is set. | |
64 | |
65 ## Parsing arguments | |
66 | |
67 Once you have an [ArgParser][] set up with some options and flags, you use it by | |
68 calling [ArgParser.parse()][parse] with a set of arguments: | |
69 | |
70 var results = parser.parse(['some', 'command', 'line', 'args']); | |
71 | |
72 These arguments usually come from the arguments to `main()`, but you can pass in | |
73 any list of strings. The `parse()` method returns an instance of [ArgResults][], | |
74 a map-like object that contains the values of the parsed options. | |
75 | |
76 var parser = new ArgParser(); | |
77 parser.addOption('mode'); | |
78 parser.addFlag('verbose', defaultsTo: true); | |
79 var results = parser.parse(['--mode', 'debug', 'something', 'else']); | |
80 | |
81 print(results['mode']); // debug | |
82 print(results['verbose']); // true | |
83 | |
84 By default, the `parse()` method stops as soon as it reaches `--` by itself or | |
85 anything that the parser doesn't recognize as an option, flag, or option value. | |
86 If arguments still remain, they go into [ArgResults.rest][rest]. | |
87 | |
88 print(results.rest); // ['something', 'else'] | |
89 | |
90 To continue to parse options found after non-option arguments, pass | |
91 `allowTrailingOptions: true` when creating the [ArgParser][]. | |
92 | |
93 ## Specifying options | |
94 | |
95 To actually pass in options and flags on the command line, use GNU or POSIX | |
96 style. Consider this option: | |
97 | |
98 parser.addOption('name', abbr: 'n'); | |
99 | |
100 You can specify its value on the command line using any of the following: | |
101 | |
102 --name=somevalue | |
103 --name somevalue | |
104 -nsomevalue | |
105 -n somevalue | |
106 | |
107 Consider this flag: | |
108 | |
109 parser.addFlag('name', abbr: 'n'); | |
110 | |
111 You can set it to true using one of the following: | |
112 | |
113 --name | |
114 -n | |
115 | |
116 You can set it to false using the following: | |
117 | |
118 --no-name | |
119 | |
120 Multiple flag abbreviations can be collapsed into a single argument. Say you | |
121 define these flags: | |
122 | |
123 parser.addFlag('verbose', abbr: 'v'); | |
124 parser.addFlag('french', abbr: 'f'); | |
125 parser.addFlag('iambic-pentameter', abbr: 'i'); | |
126 | |
127 You can set all three flags at once: | |
128 | |
129 -vfi | |
130 | |
131 By default, an option has only a single value, with later option values | |
132 overriding earlier ones; for example: | |
133 | |
134 var parser = new ArgParser(); | |
135 parser.addOption('mode'); | |
136 var results = parser.parse(['--mode', 'on', '--mode', 'off']); | |
137 print(results['mode']); // prints 'off' | |
138 | |
139 If you need multiple values, set the `allowMultiple` parameter. In that case the | |
140 option can occur multiple times, and the `parse()` method returns a list of | |
141 values: | |
142 | |
143 var parser = new ArgParser(); | |
144 parser.addOption('mode', allowMultiple: true); | |
145 var results = parser.parse(['--mode', 'on', '--mode', 'off']); | |
146 print(results['mode']); // prints '[on, off]' | |
147 | |
148 ## Defining commands ## | |
149 | |
150 In addition to *options*, you can also define *commands*. A command is a named | |
151 argument that has its own set of options. For example, consider this shell | |
152 command: | |
153 | |
154 $ git commit -a | |
155 | |
156 The executable is `git`, the command is `commit`, and the `-a` option is an | |
157 option passed to the command. You can add a command using the [addCommand][] | |
158 method: | |
159 | |
160 var parser = new ArgParser(); | |
161 var command = parser.addCommand('commit'); | |
162 | |
163 It returns another [ArgParser][], which you can then use to define options | |
164 specific to that command. If you already have an [ArgParser][] for the command's | |
165 options, you can pass it in: | |
166 | |
167 var parser = new ArgParser(); | |
168 var command = new ArgParser(); | |
169 parser.addCommand('commit', command); | |
170 | |
171 The [ArgParser][] for a command can then define options or flags: | |
172 | |
173 command.addFlag('all', abbr: 'a'); | |
174 | |
175 You can add multiple commands to the same parser so that a user can select one | |
176 from a range of possible commands. When parsing an argument list, you can then | |
177 determine which command was entered and what options were provided for it. | |
178 | |
179 var results = parser.parse(['commit', '-a']); | |
180 print(results.command.name); // "commit" | |
181 print(results.command['all']); // true | |
182 | |
183 Options for a command must appear after the command in the argument list. For | |
184 example, given the above parser, `"git -a commit"` is *not* valid. The parser | |
185 tries to find the right-most command that accepts an option. For example: | |
186 | |
187 var parser = new ArgParser(); | |
188 parser.addFlag('all', abbr: 'a'); | |
189 var command = parser.addCommand('commit'); | |
190 command.addFlag('all', abbr: 'a'); | |
191 | |
192 var results = parser.parse(['commit', '-a']); | |
193 print(results.command['all']); // true | |
194 | |
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 | |
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. | |
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] properties must be defined by every | |
216 // subclass. | |
217 final name = "commit"; | |
218 final description = "Record changes to the repository."; | |
219 | |
220 CommitCommand() { | |
221 // [argParser] is automatically created by the parent class. | |
222 argParser.addFlag('all', abbr: 'a'); | |
223 } | |
224 | |
225 // [run] may also return a Future. | |
226 void run() { | |
227 // [options] is set before [run()] is called and contains the options | |
228 // passed to this command. | |
229 print(options['all']); | |
230 } | |
231 } | |
232 | |
233 Commands can also have subcommands, which are added with [addSubcommand][]. A | |
234 command with subcommands can't run its own code, so [run][] doesn't need to be | |
235 implemented. For example: | |
236 | |
237 class StashCommand extends Command { | |
238 final String name = "stash"; | |
239 final String description = "Stash changes in the working directory."; | |
240 | |
241 StashCommand() { | |
242 addSubcommand(new StashSaveCommand()); | |
243 addSubcommand(new StashListCommand()); | |
244 } | |
245 } | |
246 | |
247 [CommandRunner][] automatically adds a `help` command that displays usage | |
248 information for commands, as well as support for the `--help` flag for all | |
249 commands. If it encounters an error parsing the arguments or processing a | |
250 command, it throws a [UsageError][]; your `main()` method should catch these and | |
251 print them appropriately. For example: | |
252 | |
253 runner.run(arguments).catchError((error) { | |
254 if (error is! UsageError) throw error; | |
255 print(error); | |
256 exit(64); // Exit code 64 indicates a usage error. | |
257 }); | |
258 | |
259 ## Displaying usage | |
260 | |
261 You can automatically generate nice help text, suitable for use as the output of | |
262 `--help`. To display good usage information, you should provide some help text | |
263 when you create your options. | |
264 | |
265 To define help text for an entire option, use the `help:` parameter: | |
266 | |
267 parser.addOption('mode', help: 'The compiler configuration', | |
268 allowed: ['debug', 'release']); | |
269 parser.addFlag('verbose', help: 'Show additional diagnostic info'); | |
270 | |
271 For non-flag options, you can also provide a help string for the parameter: | |
272 | |
273 parser.addOption('out', help: 'The output path', valueHelp: 'path', | |
274 allowed: ['debug', 'release']); | |
275 | |
276 For non-flag options, you can also provide detailed help for each expected value | |
277 by using the `allowedHelp:` parameter: | |
278 | |
279 parser.addOption('arch', help: 'The architecture to compile for', | |
280 allowedHelp: { | |
281 'ia32': 'Intel x86', | |
282 'arm': 'ARM Holding 32-bit chip' | |
283 }); | |
284 | |
285 To display the help, use the [getUsage()][getUsage] method: | |
286 | |
287 print(parser.getUsage()); | |
288 | |
289 The resulting string looks something like this: | |
290 | |
291 --mode The compiler configuration | |
292 [debug, release] | |
293 | |
294 --out=<path> The output path | |
295 --[no-]verbose Show additional diagnostic info | |
296 --arch The architecture to compile for | |
297 [arm] ARM Holding 32-bit chip | |
298 [ia32] Intel x86 | |
299 | |
300 [posix]: http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap12.html
#tag_12_02 | |
301 [gnu]: http://www.gnu.org/prep/standards/standards.html#Command_002dLine-Interfa
ces | |
302 [ArgParser]: http://www.dartdocs.org/documentation/args/latest/index.html#args/a
rgs.ArgParser | |
303 [ArgResults]: http://www.dartdocs.org/documentation/args/latest/index.html#args/
args.ArgResults | |
304 [CommandRunner]: http://www.dartdocs.org/documentation/args/latest/index.html#ar
gs/args.CommandRunner | |
305 [Command]: http://www.dartdocs.org/documentation/args/latest/index.html#args/arg
s.Command | |
306 [UsageError]: http://www.dartdocs.org/documentation/args/latest/index.html#args/
args.UsageError | |
307 [addOption]: http://www.dartdocs.org/documentation/args/latest/index.html#args/a
rgs.ArgParser@id_addOption | |
308 [addFlag]: http://www.dartdocs.org/documentation/args/latest/index.html#args/arg
s.ArgParser@id_addFlag | |
309 [parse]: http://www.dartdocs.org/documentation/args/latest/index.html#args/args.
ArgParser@id_parse | |
310 [rest]: http://www.dartdocs.org/documentation/args/latest/index.html#args/args.A
rgResults@id_rest | |
311 [addCommand]: http://www.dartdocs.org/documentation/args/latest/index.html#args/
args.ArgParser@id_addCommand | |
312 [getUsage]: http://www.dartdocs.org/documentation/args/latest/index.html#args/ar
gs.ArgParser@id_getUsage | |
313 [addSubcommand]: http://www.dartdocs.org/documentation/args/latest/index.html#ar
gs/args.Command@id_addSubcommand | |
314 [run]: http://www.dartdocs.org/documentation/args/latest/index.html#args/args.Co
mmand@id_run | |
OLD | NEW |