@@ -15,13 +15,24 @@ defmodule OptionParser do
15
15
@ doc """
16
16
Parses `argv` into a keywords list.
17
17
18
- It returns a three-element tuple as follows :
18
+ It returns a three-element tuple with the form `{parsed, args, invalid}`, where :
19
19
20
- 1. parsed switches,
21
- 2. remaining arguments,
22
- 3. invalid options.
20
+ * `parsed` is a keyword list of parsed switches with `{switch_name, value}`
21
+ tuples in it; `switch_name` is the atom representing the switch name while
22
+ `value` is the value for that switch parsed according to `opts` (see the
23
+ "Examples" section for more information)
24
+ * `args` is a list of the remaining arguments in `argv` as strings
25
+ * `invalid` is a list of invalid options as `{option_name, value}` where
26
+ `option_name` is the raw option and `value` is `nil` if the option wasn't
27
+ expected or the string value if the value didn't have the expected type for
28
+ the corresponding option
23
29
24
- ## Examples
30
+ Elixir converts switches to underscored atoms, so `--source-path` becomes
31
+ `:source_path`. This is done to better suit Elixir conventions. However, this
32
+ means that switches can't contain underscores and switches that do contain
33
+ underscores are always returned in the list of invalid options.
34
+
35
+ Without any options, this function will try to parse all switches in the `argv`.
25
36
26
37
iex> OptionParser.parse(["--debug"])
27
38
{[debug: true], [], []}
@@ -32,65 +43,85 @@ defmodule OptionParser do
32
43
iex> OptionParser.parse(["--source-path", "lib", "test/enum_test.exs", "--verbose"])
33
44
{[source_path: "lib", verbose: true], ["test/enum_test.exs"], []}
34
45
35
- By default, Elixir will try to automatically parse all switches.
36
46
Switches followed by a value will be assigned the value, as a string.
37
- Switches without an argument, like `--debug` will automatically
38
- be set to `true`.
47
+ Switches without an argument, like `--debug` in the examples above, will
48
+ automatically be set to `true`.
49
+
50
+ ## Options
39
51
40
- Note: Elixir also converts the switches to underscore atoms, so
41
- `--source-path` becomes `:source_path`, to better suit Elixir
42
- conventions. This means that option names on the command line cannot
43
- contain underscores; such options will be put in the invalid options
44
- list.
52
+ The following options are supported:
45
53
46
- ## Switch Definitions
54
+ * `:switches` or `:strict` - see the "Switch definitions" section below
55
+ * `:aliases` - see the "Aliases" section below
56
+
57
+ ## Switch definitions
47
58
48
59
Often it is better to explicitly list the known
49
- switches and their formats. The switches can be specified via two
50
- alternative options:
60
+ switches and their formats. The switches can be specified via one of two
61
+ options:
51
62
52
- * `:switches` - defines some switches. An attempt is still made to parse
53
- switches that do not appear in the list.
63
+ * `:switches` - defines some switches and their types. This function
64
+ still attempts to parse switches that are not in this list.
65
+ * `:strict` - defines strict switches. Any switch in `argv` that is not
66
+ specified in the list is returned in the invalid options list.
54
67
55
- * `:strict` - the switches are strict. Any switch that is not specified
56
- in the list is returned in the invalid options list.
68
+ Both these options accept a keyword list of `{name, type}` tuples where `name`
69
+ is an atom defining the name of the switch and `type` is an atom that
70
+ specifies the type for the value of this switch (see the "Types" section below
71
+ for the possible types and more information about type casting).
57
72
58
73
Note that you should only supply the `:switches` or `:strict` option.
59
- If you supply both, an error will be raised.
74
+ If you supply both, an `ArgumentError` exception will be raised.
60
75
61
76
### Types
62
77
63
- Option parser switches may take 0 or 1 argument .
78
+ Switches parsed by `OptionParser` may take zero or one arguments .
64
79
65
- The following switches take no argument :
80
+ The following switches types take no arguments :
66
81
67
- * `:boolean` - sets the value to true when given
68
- * `:count` - counts the number of times the switch is given
82
+ * `:boolean` - sets the value to `true` when given (see also the
83
+ "Negation switches" section below)
84
+ * `:count` - counts the number of times the switch is given
69
85
70
- The following switches take 1 argument:
86
+ The following switches take one argument:
71
87
72
- * `:integer` - parses the upcoming value as an integer.
73
- * `:float` - parses the upcoming value as a float.
74
- * `:string` - parses the upcoming value as a string.
88
+ * `:integer` - parses the value as an integer
89
+ * `:float` - parses the value as a float
90
+ * `:string` - parses the value as a string
75
91
76
- If a switch can't be parsed, it is returned in the invalid
77
- options list.
92
+ If a switch can't be parsed according to the given type , it is returned
93
+ in the invalid options list.
78
94
79
95
### Modifiers
80
96
81
97
Switches can be specified with modifiers, which change how
82
98
they behave. The following modifiers are supported:
83
99
84
- * `:keep` - keeps duplicated items instead of overriding them.
85
- Works with all types except `:count`.
100
+ * `:keep` - keeps duplicated items instead of overriding them; works with
101
+ all types except `:count`. Specifying `switch_name: :keep` assumes the
102
+ type of `:switch_name` will be `:string`.
86
103
87
- Note: if you want to use `:keep` with a non-string type, use a list, e.g.
88
- `[foo: [:integer, :keep]]`.
104
+ Note that if you want to use `:keep` with a type other than `:string` , use a list
105
+ as the type for the switch. For example: `[foo: [:integer, :keep]]`.
89
106
90
- ### Examples
107
+ ### Negation switches
108
+
109
+ In case a switch `SWITCH` is specified to have type `:boolean`, it may be
110
+ passed as `--no-SWITCH` as well which will set the option to `false`:
111
+
112
+ iex> OptionParser.parse(["--no-op", "path/to/file"], switches: [op: :boolean])
113
+ {[op: false], ["path/to/file"], []}
114
+
115
+ ## Aliases
116
+
117
+ A set of aliases can be specified in the `:aliases` option:
118
+
119
+ iex> OptionParser.parse(["-d"], aliases: [d: :debug])
120
+ {[debug: true], [], []}
121
+
122
+ ## Examples
91
123
92
- Here are some examples of option parser working with different types
93
- and modifiers:
124
+ Here are some examples of working with different types and modifiers:
94
125
95
126
iex> OptionParser.parse(["--unlock", "path/to/file"], strict: [unlock: :boolean])
96
127
{[unlock: true], ["path/to/file"], []}
@@ -121,21 +152,6 @@ defmodule OptionParser do
121
152
iex> OptionParser.parse(["--unlock", "path/to/file", "--unlock", "path/to/another/file"], strict: [unlock: :keep])
122
153
{[unlock: "path/to/file", unlock: "path/to/another/file"], [], []}
123
154
124
- ### Negation switches
125
-
126
- In case a switch is declared as boolean, it may be passed as `--no-SWITCH`
127
- which will set the option to `false`:
128
-
129
- iex> OptionParser.parse(["--no-op", "path/to/file"], switches: [op: :boolean])
130
- {[op: false], ["path/to/file"], []}
131
-
132
- ## Aliases
133
-
134
- A set of aliases can be given as options too:
135
-
136
- iex> OptionParser.parse(["-d"], aliases: [d: :debug])
137
- {[debug: true], [], []}
138
-
139
155
"""
140
156
@ spec parse ( argv , options ) :: { parsed , argv , errors }
141
157
def parse ( argv , opts \\ [ ] ) when is_list ( argv ) and is_list ( opts ) do
@@ -146,14 +162,16 @@ defmodule OptionParser do
146
162
The same as `parse/2` but raises an `OptionParser.ParseError`
147
163
exception if any invalid options are given.
148
164
149
- If there weren't any errors, returns a three-element tuple as follows :
165
+ If there are no errors, returns a `{parsed, rest}` tuple where :
150
166
151
- 1. parsed options,
152
- 2. remaining arguments,
153
- 3. empty list.
167
+ * `parsed` is the list of parsed switches (same as in `parse/2`)
168
+ * `rest` is the list of arguments (same as in `parse/2`)
154
169
155
170
## Examples
156
171
172
+ iex> OptionParser.parse!(["--debug", "path/to/file"], strict: [debug: :boolean])
173
+ {[debug: true], ["path/to/file"]}
174
+
157
175
iex> OptionParser.parse!(["--limit", "xyz"], strict: [limit: :integer])
158
176
** (OptionParser.ParseError) 1 error found!
159
177
--limit : Expected type integer, got "xyz"
@@ -172,7 +190,7 @@ defmodule OptionParser do
172
190
@ spec parse! ( argv , options ) :: { parsed , argv } | no_return
173
191
def parse! ( argv , opts \\ [ ] ) when is_list ( argv ) and is_list ( opts ) do
174
192
case parse ( argv , opts ) do
175
- { parsed , argv , [ ] } -> { parsed , argv }
193
+ { parsed , args , [ ] } -> { parsed , args }
176
194
{ _ , _ , errors } -> raise ParseError , format_errors ( errors , opts )
177
195
end
178
196
end
@@ -201,14 +219,16 @@ defmodule OptionParser do
201
219
The same as `parse_head/2` but raises an `OptionParser.ParseError`
202
220
exception if any invalid options are given.
203
221
204
- If there weren't any errors, returns a three-element tuple as follows :
222
+ If there are no errors, returns a `{parsed, rest}` tuple where :
205
223
206
- 1. parsed options,
207
- 2. remaining arguments,
208
- 3. empty list.
224
+ * `parsed` is the list of parsed switches (same as in `parse_head/2`)
225
+ * `rest` is the list of arguments (same as in `parse_head/2`)
209
226
210
227
## Examples
211
228
229
+ iex> OptionParser.parse_head!(["--source", "lib", "path/to/file", "--verbose"])
230
+ {[source: "lib"], ["path/to/file", "--verbose"]}
231
+
212
232
iex> OptionParser.parse_head!(["--number", "lib", "test/enum_test.exs", "--verbose"], strict: [number: :integer])
213
233
** (OptionParser.ParseError) 1 error found!
214
234
--number : Expected type integer, got "lib"
@@ -222,7 +242,7 @@ defmodule OptionParser do
222
242
@ spec parse_head! ( argv , options ) :: { parsed , argv , errors } | no_return
223
243
def parse_head! ( argv , opts \\ [ ] ) when is_list ( argv ) and is_list ( opts ) do
224
244
case parse_head ( argv , opts ) do
225
- { parsed , argv , [ ] } -> { parsed , argv }
245
+ { parsed , args , [ ] } -> { parsed , args }
226
246
{ _ , _ , errors } -> raise ParseError , format_errors ( errors , opts )
227
247
end
228
248
end
@@ -250,7 +270,7 @@ defmodule OptionParser do
250
270
{ :error , [ "--" | rest ] } ->
251
271
{ Enum . reverse ( opts ) , Enum . reverse ( args , rest ) , Enum . reverse ( invalid ) }
252
272
253
- { :error , [ arg | rest ] = remaining_args } ->
273
+ { :error , [ arg | rest ] = remaining_args } ->
254
274
# there is no option
255
275
if all? do
256
276
do_parse ( rest , config , opts , [ arg | args ] , invalid , all? )
@@ -264,20 +284,20 @@ defmodule OptionParser do
264
284
Low-level function that parses one option.
265
285
266
286
It accepts the same options as `parse/2` and `parse_head/2`
267
- as both functions are built on top of next . This function
287
+ as both functions are built on top of this function . This function
268
288
may return:
269
289
270
290
* `{:ok, key, value, rest}` - the option `key` with `value` was
271
291
successfully parsed
272
292
273
293
* `{:invalid, key, value, rest}` - the option `key` is invalid with `value`
274
- (returned when the switch type does not match the one given via the
275
- command line)
294
+ (returned when the value cannot be parsed according to the switch type)
276
295
277
296
* `{:undefined, key, value, rest}` - the option `key` is undefined
278
297
(returned in strict mode when the switch is unknown)
279
298
280
- * `{:error, rest}` - there are no switches at the top of the given argv
299
+ * `{:error, rest}` - there are no switches at the head of the given `argv`
300
+
281
301
"""
282
302
283
303
@ spec next ( argv , options ) ::
@@ -295,15 +315,15 @@ defmodule OptionParser do
295
315
{ :error , [ ] }
296
316
end
297
317
298
- defp next ( [ "--" | _ ] = argv , _aliases , _switches , _strict ) do
318
+ defp next ( [ "--" | _ ] = argv , _aliases , _switches , _strict ) do
299
319
{ :error , argv }
300
320
end
301
321
302
- defp next ( [ "-" | _ ] = argv , _aliases , _switches , _strict ) do
322
+ defp next ( [ "-" | _ ] = argv , _aliases , _switches , _strict ) do
303
323
{ :error , argv }
304
324
end
305
325
306
- defp next ( [ "- " <> _ | _ ] = argv , _aliases , _switches , _strict ) do
326
+ defp next ( [ "- " <> _ | _ ] = argv , _aliases , _switches , _strict ) do
307
327
{ :error , argv }
308
328
end
309
329
@@ -334,8 +354,9 @@ defmodule OptionParser do
334
354
@ doc """
335
355
Receives a key-value enumerable and converts it to argv.
336
356
337
- Keys must be atoms. Keys with nil value are discarded,
357
+ Keys must be atoms. Keys with ` nil` value are discarded,
338
358
boolean values are converted to `--key` or `--no-key`
359
+ (if the value is `true` or `false`, respectively),
339
360
and all other values are converted using `to_string/1`.
340
361
341
362
## Examples
@@ -364,13 +385,17 @@ defmodule OptionParser do
364
385
@ doc ~S"""
365
386
Splits a string into argv chunks.
366
387
388
+ This function splits the given `string` into a list of strings in a similar
389
+ way to many shells.
390
+
367
391
## Examples
368
392
369
393
iex> OptionParser.split("foo bar")
370
394
["foo", "bar"]
371
395
372
396
iex> OptionParser.split("foo \" bar baz\" ")
373
397
["foo", "bar baz"]
398
+
374
399
"""
375
400
@ spec split ( String . t ) :: argv
376
401
def split ( string ) do
0 commit comments