OLD | NEW |
---|---|
(Empty) | |
1 | |
nweiz
2014/05/12 20:15:30
Empty line.
Bob Nystrom
2014/05/12 21:29:26
Done.
| |
2 Parses raw command-line arguments into a set of options and values. | |
3 | |
4 This library supports [GNU][] and [POSIX][] style options, and it works | |
5 in both server-side and client-side apps. | |
6 | |
7 For information on installing this library, see the | |
8 [args package on pub.dartlang.org](http://pub.dartlang.org/packages/args). | |
9 Import it like so: | |
10 | |
11 import 'package:args/args.dart'; | |
nweiz
2014/05/12 20:15:30
I think at this point installation instructions pr
Bob Nystrom
2014/05/12 21:29:26
Done.
| |
12 | |
13 ## Defining options | |
14 | |
15 First create an [ArgParser][]: | |
16 | |
17 var parser = new ArgParser(); | |
18 | |
19 Then define a set of options on that parser using [addOption()][addOption] and | |
20 [addFlag()][addFlag]. Here's the minimal way to create an option named "name": | |
21 | |
22 parser.addOption('name'); | |
23 | |
24 When an option can only be set or unset (as opposed to taking a string value), | |
25 use a flag: | |
26 | |
27 parser.addFlag('name'); | |
28 | |
29 Flag options, by default, accept a 'no-' prefix to negate the option. You can | |
30 disable the 'no-' prefix using the `negatable` parameter: | |
31 | |
32 parser.addFlag('name', negatable: false); | |
33 | |
34 *Note:* From here on out, "option" refers to both regular options and flags. In | |
35 cases where the distinction matters, we'll use "non-flag option." | |
36 | |
37 Options can have an optional single-character abbreviation, specified with the | |
38 `abbr` parameter: | |
39 | |
40 parser.addOption('mode', abbr: 'm'); | |
41 parser.addFlag('verbose', abbr: 'v'); | |
42 | |
43 Options can also have a default value, specified with the `defaultsTo` | |
44 parameter. The default value is used when arguments don't specify the option. | |
45 | |
46 parser.addOption('mode', defaultsTo: 'debug'); | |
47 parser.addFlag('verbose', defaultsTo: false); | |
48 | |
49 The default value for non-flag options can be any string. For flags, it must | |
50 be a `bool`. | |
51 | |
52 To validate a non-flag option, you can use the `allowed` parameter to provide an | |
53 allowed set of values. When you do, the parser throws a [FormatException] if the | |
54 value for an option is not in the allowed set. Here's an example of specifying | |
55 allowed values: | |
56 | |
57 parser.addOption('mode', allowed: ['debug', 'release']); | |
58 | |
59 You can use the `callback` parameter to associate a function with an option. | |
60 Later, when parsing occurs, the callback function is invoked with the value of | |
61 the option: | |
62 | |
63 parser.addOption('mode', callback: (mode) => print('Got mode $mode)); | |
64 parser.addFlag('verbose', callback: (verbose) { | |
65 if (verbose) print('Verbose'); | |
66 }); | |
67 | |
68 The callbacks for all options are called whenever a set of arguments is parsed. | |
69 If an option isn't provided in the args, its callback is passed the default | |
70 value, or `null` if no default value is set. | |
71 | |
72 ## Parsing arguments | |
73 | |
74 Once you have an [ArgParser][] set up with some options and flags, you use it by | |
75 calling [ArgParser.parse()][parse] with a set of arguments: | |
76 | |
77 var results = parser.parse(['some', 'command', 'line', 'args']); | |
78 | |
79 These arguments usually come from the arguments to `main()`, but you can pass in | |
80 any list of strings. The `parse()` method returns an instance of [ArgResults][], | |
81 a map-like object that contains the values of the parsed options. | |
82 | |
83 var parser = new ArgParser(); | |
84 parser.addOption('mode'); | |
85 parser.addFlag('verbose', defaultsTo: true); | |
86 var results = parser.parse(['--mode', 'debug', 'something', 'else']); | |
87 | |
88 print(results['mode']); // debug | |
89 print(results['verbose']); // true | |
90 | |
91 By default, the `parse()` method stops as soon as it reaches `--` by itself or | |
92 anything that the parser doesn't recognize as an option, flag, or option value. | |
93 If arguments still remain, they go into [ArgResults.rest][rest]. | |
94 | |
95 print(results.rest); // ['something', 'else'] | |
96 | |
97 To continue to parse options found after non-option arguments, pass | |
98 `allowTrailingOptions: true` when creating the [ArgParser][]. | |
99 | |
100 ## Specifying options | |
101 | |
102 To actually pass in options and flags on the command line, use GNU or POSIX | |
103 style. Consider this option: | |
104 | |
105 parser.addOption('name', abbr: 'n'); | |
106 | |
107 You can specify its value on the command line using any of the following: | |
108 | |
109 --name=somevalue | |
110 --name somevalue | |
111 -nsomevalue | |
112 -n somevalue | |
113 | |
114 Consider this flag: | |
115 | |
116 parser.addFlag('name', abbr: 'n'); | |
117 | |
118 You can set it to true using one of the following: | |
119 | |
120 --name | |
121 -n | |
122 | |
123 You can set it to false using the following: | |
124 | |
125 --no-name | |
126 | |
127 Multiple flag abbreviations can be collapsed into a single argument. Say you | |
128 define these flags: | |
129 | |
130 parser.addFlag('verbose', abbr: 'v'); | |
131 parser.addFlag('french', abbr: 'f'); | |
132 parser.addFlag('iambic-pentameter', abbr: 'i'); | |
133 | |
134 You can set all three flags at once: | |
135 | |
136 -vfi | |
137 | |
138 By default, an option has only a single value, with later option values | |
139 overriding earlier ones; for example: | |
140 | |
141 var parser = new ArgParser(); | |
142 parser.addOption('mode'); | |
143 var results = parser.parse(['--mode', 'on', '--mode', 'off']); | |
144 print(results['mode']); // prints 'off' | |
145 | |
146 If you need multiple values, set the `allowMultiple` parameter. In that case the | |
147 option can occur multiple times, and the `parse()` method returns a list of | |
148 values: | |
149 | |
150 var parser = new ArgParser(); | |
151 parser.addOption('mode', allowMultiple: true); | |
152 var results = parser.parse(['--mode', 'on', '--mode', 'off']); | |
153 print(results['mode']); // prints '[on, off]' | |
154 | |
155 ## Defining commands ## | |
156 | |
157 In addition to *options*, you can also define *commands*. A command is a named | |
158 argument that has its own set of options. For example, consider this shell | |
159 command: | |
160 | |
161 $ git commit -a | |
162 | |
163 The executable is `git`, the command is `commit`, and the `-a` option is an | |
164 option passed to the command. You can add a command using the [addCommand][] | |
165 method: | |
166 | |
167 var parser = new ArgParser(); | |
168 var command = parser.addCommand('commit'); | |
169 | |
170 It returns another [ArgParser][], which you can then use to define options | |
171 specific to that command. If you already have an [ArgParser][] for the command's | |
172 options, you can pass it in: | |
173 | |
174 var parser = new ArgParser(); | |
175 var command = new ArgParser(); | |
176 parser.addCommand('commit', command); | |
177 | |
178 The [ArgParser][] for a command can then define options or flags: | |
179 | |
180 command.addFlag('all', abbr: 'a'); | |
181 | |
182 You can add multiple commands to the same parser so that a user can select one | |
183 from a range of possible commands. When parsing an argument list, you can then | |
184 determine which command was entered and what options were provided for it. | |
185 | |
186 var results = parser.parse(['commit', '-a']); | |
187 print(results.command.name); // "commit" | |
188 print(results.command['all']); // true | |
189 | |
190 Options for a command must appear after the command in the argument list. For | |
191 example, given the above parser, `"git -a commit"` is *not* valid. The parser | |
192 tries to find the right-most command that accepts an option. For example: | |
193 | |
194 var parser = new ArgParser(); | |
195 parser.addFlag('all', abbr: 'a'); | |
196 var command = parser.addCommand('commit'); | |
197 command.addFlag('all', abbr: 'a'); | |
198 | |
199 var results = parser.parse(['commit', '-a']); | |
200 print(results.command['all']); // true | |
201 | |
202 Here, both the top-level parser and the `"commit"` command can accept a `"-a"` | |
203 (which is probably a bad command line interface, admittedly). In that case, when | |
204 `"-a"` appears after `"commit"`, it is applied to that command. If it appears to | |
205 the left of `"commit"`, it is given to the top-level parser. | |
206 | |
207 ## Displaying usage | |
208 | |
209 You can automatically generate nice help text, suitable for use as the output of | |
210 `--help`. To display good usage information, you should provide some help text | |
211 when you create your options. | |
212 | |
213 To define help text for an entire option, use the `help:` parameter: | |
214 | |
215 parser.addOption('mode', help: 'The compiler configuration', | |
216 allowed: ['debug', 'release']); | |
217 parser.addFlag('verbose', help: 'Show additional diagnostic info'); | |
218 | |
219 For non-flag options, you can also provide detailed help for each expected value | |
220 by using the `allowedHelp:` parameter: | |
221 | |
222 parser.addOption('arch', help: 'The architecture to compile for', | |
223 allowedHelp: { | |
224 'ia32': 'Intel x86', | |
225 'arm': 'ARM Holding 32-bit chip' | |
226 }); | |
227 | |
228 To display the help, use the [getUsage()][getUsage] method: | |
229 | |
230 print(parser.getUsage()); | |
231 | |
232 The resulting string looks something like this: | |
233 | |
234 --mode The compiler configuration | |
235 [debug, release] | |
236 | |
237 --[no-]verbose Show additional diagnostic info | |
238 --arch The architecture to compile for | |
239 | |
240 [arm] ARM Holding 32-bit chip | |
241 [ia32] Intel x86 | |
242 | |
243 To assist the formatting of the usage help, single-line help text is followed by | |
244 a single new line. Options with multi-line help text are followed by two new | |
245 lines. This provides spatial diversity between options. | |
246 | |
247 [posix]: http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap12.html #tag_12_02 | |
248 [gnu]: http://www.gnu.org/prep/standards/standards.html#Command_002dLine-Interfa ces | |
249 [ArgParser]: https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/arg s/args.ArgParser | |
250 [ArgResults]: https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/ar gs/args.ArgResults | |
251 [addOption]: https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/arg s/args.ArgParser#id_addOption | |
252 [addFlag]: https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/args/ args.ArgParser#id_addFlag | |
253 [parse]: https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/args/ar gs.ArgParser#id_parse | |
254 [rest]: https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/args/arg s.ArgResults#id_rest | |
255 [addCommand]: https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/ar gs/args.ArgParser#id_addCommand | |
256 [getUsage]: https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/args /args.ArgParser#id_getUsage | |
OLD | NEW |