cmd-optparse.rb 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.bundle/config +2 -0
- data/.document +7 -0
- data/.editorconfig +13 -0
- data/.github/dependabot.yml +6 -0
- data/.github/workflows/test.yml +26 -0
- data/.gitignore +12 -0
- data/.rdoc_options +4 -0
- data/COPYING +56 -0
- data/Gemfile +5 -0
- data/README.md +59 -0
- data/Rakefile +14 -0
- data/cmd-optparse.rb.gemspec +30 -0
- data/doc/optparse/.document +1 -0
- data/doc/optparse/argument_converters.rdoc +380 -0
- data/doc/optparse/creates_option.rdoc +7 -0
- data/doc/optparse/option_params.rdoc +509 -0
- data/doc/optparse/ruby/argument_abbreviation.rb +9 -0
- data/doc/optparse/ruby/argument_keywords.rb +6 -0
- data/doc/optparse/ruby/argument_strings.rb +6 -0
- data/doc/optparse/ruby/argv.rb +2 -0
- data/doc/optparse/ruby/array.rb +6 -0
- data/doc/optparse/ruby/basic.rb +17 -0
- data/doc/optparse/ruby/block.rb +9 -0
- data/doc/optparse/ruby/collected_options.rb +8 -0
- data/doc/optparse/ruby/custom_converter.rb +9 -0
- data/doc/optparse/ruby/date.rb +6 -0
- data/doc/optparse/ruby/datetime.rb +6 -0
- data/doc/optparse/ruby/decimal_integer.rb +7 -0
- data/doc/optparse/ruby/decimal_numeric.rb +7 -0
- data/doc/optparse/ruby/default_values.rb +8 -0
- data/doc/optparse/ruby/descriptions.rb +15 -0
- data/doc/optparse/ruby/explicit_array_values.rb +9 -0
- data/doc/optparse/ruby/explicit_hash_values.rb +9 -0
- data/doc/optparse/ruby/false_class.rb +6 -0
- data/doc/optparse/ruby/float.rb +6 -0
- data/doc/optparse/ruby/help.rb +18 -0
- data/doc/optparse/ruby/help_banner.rb +7 -0
- data/doc/optparse/ruby/help_format.rb +25 -0
- data/doc/optparse/ruby/help_program_name.rb +7 -0
- data/doc/optparse/ruby/integer.rb +6 -0
- data/doc/optparse/ruby/long_names.rb +9 -0
- data/doc/optparse/ruby/long_optional.rb +6 -0
- data/doc/optparse/ruby/long_required.rb +6 -0
- data/doc/optparse/ruby/long_simple.rb +9 -0
- data/doc/optparse/ruby/long_with_negation.rb +6 -0
- data/doc/optparse/ruby/match_converter.rb +9 -0
- data/doc/optparse/ruby/matched_values.rb +6 -0
- data/doc/optparse/ruby/method.rb +11 -0
- data/doc/optparse/ruby/missing_options.rb +12 -0
- data/doc/optparse/ruby/mixed_names.rb +12 -0
- data/doc/optparse/ruby/name_abbrev.rb +9 -0
- data/doc/optparse/ruby/no_abbreviation.rb +10 -0
- data/doc/optparse/ruby/numeric.rb +6 -0
- data/doc/optparse/ruby/object.rb +6 -0
- data/doc/optparse/ruby/octal_integer.rb +7 -0
- data/doc/optparse/ruby/optional_argument.rb +9 -0
- data/doc/optparse/ruby/parse.rb +13 -0
- data/doc/optparse/ruby/parse_bang.rb +13 -0
- data/doc/optparse/ruby/proc.rb +13 -0
- data/doc/optparse/ruby/regexp.rb +6 -0
- data/doc/optparse/ruby/required_argument.rb +9 -0
- data/doc/optparse/ruby/shellwords.rb +6 -0
- data/doc/optparse/ruby/short_names.rb +9 -0
- data/doc/optparse/ruby/short_optional.rb +6 -0
- data/doc/optparse/ruby/short_range.rb +6 -0
- data/doc/optparse/ruby/short_required.rb +6 -0
- data/doc/optparse/ruby/short_simple.rb +9 -0
- data/doc/optparse/ruby/string.rb +6 -0
- data/doc/optparse/ruby/terminator.rb +6 -0
- data/doc/optparse/ruby/time.rb +6 -0
- data/doc/optparse/ruby/true_class.rb +6 -0
- data/doc/optparse/ruby/uri.rb +6 -0
- data/doc/optparse/tutorial.rdoc +858 -0
- data/lib/optionparser.rb +2 -0
- data/lib/optparse/ac.rb +54 -0
- data/lib/optparse/date.rb +18 -0
- data/lib/optparse/kwargs.rb +22 -0
- data/lib/optparse/shellwords.rb +7 -0
- data/lib/optparse/time.rb +11 -0
- data/lib/optparse/uri.rb +7 -0
- data/lib/optparse/version.rb +71 -0
- data/lib/optparse.rb +2346 -0
- data/misc/rb_optparse.bash +21 -0
- data/misc/rb_optparse.zsh +39 -0
- data/rakelib/.document +0 -0
- data/rakelib/changelogs.rake +34 -0
- data/rakelib/epoch.rake +5 -0
- data/rakelib/version.rake +51 -0
- data/test/lib/helper.rb +4 -0
- data/test/optparse/test_acceptable.rb +198 -0
- data/test/optparse/test_autoconf.rb +69 -0
- data/test/optparse/test_bash_completion.rb +46 -0
- data/test/optparse/test_cclass.rb +18 -0
- data/test/optparse/test_did_you_mean.rb +48 -0
- data/test/optparse/test_getopts.rb +49 -0
- data/test/optparse/test_kwargs.rb +38 -0
- data/test/optparse/test_load.rb +141 -0
- data/test/optparse/test_noarg.rb +79 -0
- data/test/optparse/test_optarg.rb +60 -0
- data/test/optparse/test_optparse.rb +127 -0
- data/test/optparse/test_placearg.rb +76 -0
- data/test/optparse/test_reqarg.rb +95 -0
- data/test/optparse/test_summary.rb +81 -0
- data/test/optparse/test_zsh_completion.rb +21 -0
- metadata +154 -0
@@ -0,0 +1,509 @@
|
|
1
|
+
== Parameters for New Options
|
2
|
+
|
3
|
+
Option-creating methods in +OptionParser+
|
4
|
+
accept arguments that determine the behavior of a new option:
|
5
|
+
|
6
|
+
- OptionParser#on
|
7
|
+
- OptionParser#on_head
|
8
|
+
- OptionParser#on_tail
|
9
|
+
- OptionParser#define
|
10
|
+
- OptionParser#define_head
|
11
|
+
- OptionParser#define_tail
|
12
|
+
- OptionParser#make_switch
|
13
|
+
|
14
|
+
The code examples on this page use:
|
15
|
+
|
16
|
+
- OptionParser#on, to define options.
|
17
|
+
- OptionParser#parse!, to parse the command line.
|
18
|
+
- Built-in option <tt>--help</tt>, to display defined options.
|
19
|
+
|
20
|
+
Contents:
|
21
|
+
|
22
|
+
- {Option Names}[#label-Option+Names]
|
23
|
+
- {Short Names}[#label-Short+Names]
|
24
|
+
- {Simple Short Names}[#label-Simple+Short+Names]
|
25
|
+
- {Short Names with Required Arguments}[#label-Short+Names+with+Required+Arguments]
|
26
|
+
- {Short Names with Optional Arguments}[#label-Short+Names+with+Optional+Arguments]
|
27
|
+
- {Short Names from Range}[#label-Short+Names+from+Range]
|
28
|
+
- {Long Names}[#label-Long+Names]
|
29
|
+
- {Simple Long Names}[#label-Simple+Long+Names]
|
30
|
+
- {Long Names with Required Arguments}[#label-Long+Names+with+Required+Arguments]
|
31
|
+
- {Long Names with Optional Arguments}[#label-Long+Names+with+Optional+Arguments]
|
32
|
+
- {Long Names with Negation}[#label-Long+Names+with+Negation]
|
33
|
+
- {Mixed Names}[#label-Mixed+Names]
|
34
|
+
- {Argument Strings}[#label-Argument+Strings]
|
35
|
+
- {Argument Values}[#label-Argument+Values]
|
36
|
+
- {Explicit Argument Values}[#label-Explicit+Argument+Values]
|
37
|
+
- {Explicit Values in Array}[#label-Explicit+Values+in+Array]
|
38
|
+
- {Explicit Values in Hash}[#label-Explicit+Values+in+Hash]
|
39
|
+
- {Argument Value Patterns}[#label-Argument+Value+Patterns]
|
40
|
+
- {Argument Converters}[#label-Argument+Converters]
|
41
|
+
- {Descriptions}[#label-Descriptions]
|
42
|
+
- {Option Handlers}[#label-Option+Handlers]
|
43
|
+
- {Handler Blocks}[#label-Handler+Blocks]
|
44
|
+
- {Handler Procs}[#label-Handler+Procs]
|
45
|
+
- {Handler Methods}[#label-Handler+Methods]
|
46
|
+
|
47
|
+
=== Option Names
|
48
|
+
|
49
|
+
There are two kinds of option names:
|
50
|
+
|
51
|
+
- Short option name, consisting of a single hyphen and a single character.
|
52
|
+
- Long option name, consisting of two hyphens and one or more characters.
|
53
|
+
|
54
|
+
==== Short Names
|
55
|
+
|
56
|
+
===== Simple Short Names
|
57
|
+
|
58
|
+
File +short_simple.rb+ defines two options:
|
59
|
+
|
60
|
+
- One with short name <tt>-x</tt>.
|
61
|
+
- The other with two short names, in effect, aliases, <tt>-1</tt> and <tt>-%</tt>.
|
62
|
+
|
63
|
+
:include: ruby/short_simple.rb
|
64
|
+
|
65
|
+
Executions:
|
66
|
+
|
67
|
+
$ ruby short_simple.rb --help
|
68
|
+
Usage: short_simple [options]
|
69
|
+
-x One short name
|
70
|
+
-1, -% Two short names (aliases)
|
71
|
+
$ ruby short_simple.rb -x
|
72
|
+
["-x", true]
|
73
|
+
$ ruby short_simple.rb -1 -x -%
|
74
|
+
["-1 or -%", true]
|
75
|
+
["-x", true]
|
76
|
+
["-1 or -%", true]
|
77
|
+
|
78
|
+
===== Short Names with Required Arguments
|
79
|
+
|
80
|
+
A short name followed (no whitespace) by a dummy word
|
81
|
+
defines an option that requires an argument.
|
82
|
+
|
83
|
+
File +short_required.rb+ defines an option <tt>-x</tt>
|
84
|
+
that requires an argument.
|
85
|
+
|
86
|
+
:include: ruby/short_required.rb
|
87
|
+
|
88
|
+
Executions:
|
89
|
+
|
90
|
+
$ ruby short_required.rb --help
|
91
|
+
Usage: short_required [options]
|
92
|
+
-xXXX Short name with required argument
|
93
|
+
$ ruby short_required.rb -x
|
94
|
+
short_required.rb:6:in `<main>': missing argument: -x (OptionParser::MissingArgument)
|
95
|
+
$ ruby short_required.rb -x FOO
|
96
|
+
["-x", "FOO"]
|
97
|
+
|
98
|
+
===== Short Names with Optional Arguments
|
99
|
+
|
100
|
+
A short name followed (with whitespace) by a dummy word in square brackets
|
101
|
+
defines an option that allows an optional argument.
|
102
|
+
|
103
|
+
File +short_optional.rb+ defines an option <tt>-x</tt>
|
104
|
+
that allows an optional argument.
|
105
|
+
|
106
|
+
:include: ruby/short_optional.rb
|
107
|
+
|
108
|
+
Executions:
|
109
|
+
|
110
|
+
$ ruby short_optional.rb --help
|
111
|
+
Usage: short_optional [options]
|
112
|
+
-x [XXX] Short name with optional argument
|
113
|
+
$ ruby short_optional.rb -x
|
114
|
+
["-x", nil]
|
115
|
+
$ ruby short_optional.rb -x FOO
|
116
|
+
["-x", "FOO"]
|
117
|
+
|
118
|
+
===== Short Names from Range
|
119
|
+
|
120
|
+
You can define an option with multiple short names
|
121
|
+
taken from a range of characters.
|
122
|
+
The parser yields both the actual character cited and the value.
|
123
|
+
|
124
|
+
File +short_range.rb+ defines an option with short names
|
125
|
+
for all printable characters from <tt>!</tt> to <tt>~</tt>:
|
126
|
+
|
127
|
+
:include: ruby/short_range.rb
|
128
|
+
|
129
|
+
Executions:
|
130
|
+
|
131
|
+
$ ruby short_range.rb --help
|
132
|
+
Usage: short_range [options]
|
133
|
+
-[!-~] Short names in (very large) range
|
134
|
+
$ ruby short_range.rb -!
|
135
|
+
["!-~", "!", nil]
|
136
|
+
$ ruby short_range.rb -!
|
137
|
+
["!-~", "!", nil]
|
138
|
+
$ ruby short_range.rb -A
|
139
|
+
["!-~", "A", nil]
|
140
|
+
$ ruby short_range.rb -z
|
141
|
+
["!-~", "z", nil]
|
142
|
+
|
143
|
+
==== Long Names
|
144
|
+
|
145
|
+
===== Simple Long Names
|
146
|
+
|
147
|
+
File +long_simple.rb+ defines two options:
|
148
|
+
|
149
|
+
- One with long name <tt>-xxx</tt>.
|
150
|
+
- The other with two long names, in effect, aliases,
|
151
|
+
<tt>--y1%</tt> and <tt>--z2#</tt>.
|
152
|
+
|
153
|
+
:include: ruby/long_simple.rb
|
154
|
+
|
155
|
+
Executions:
|
156
|
+
|
157
|
+
$ ruby long_simple.rb --help
|
158
|
+
Usage: long_simple [options]
|
159
|
+
--xxx One long name
|
160
|
+
--y1%, --z2# Two long names (aliases)
|
161
|
+
$ ruby long_simple.rb --xxx
|
162
|
+
["--xxx", true]
|
163
|
+
$ ruby long_simple.rb --y1% --xxx --z2#
|
164
|
+
["--y1% or --z2#", true]
|
165
|
+
["--xxx", true]
|
166
|
+
["--y1% or --z2#", true]
|
167
|
+
|
168
|
+
===== Long Names with Required Arguments
|
169
|
+
|
170
|
+
A long name followed (with whitespace) by a dummy word
|
171
|
+
defines an option that requires an argument.
|
172
|
+
|
173
|
+
File +long_required.rb+ defines an option <tt>--xxx</tt>
|
174
|
+
that requires an argument.
|
175
|
+
|
176
|
+
:include: ruby/long_required.rb
|
177
|
+
|
178
|
+
Executions:
|
179
|
+
|
180
|
+
$ ruby long_required.rb --help
|
181
|
+
Usage: long_required [options]
|
182
|
+
--xxx XXX Long name with required argument
|
183
|
+
$ ruby long_required.rb --xxx
|
184
|
+
long_required.rb:6:in `<main>': missing argument: --xxx (OptionParser::MissingArgument)
|
185
|
+
$ ruby long_required.rb --xxx FOO
|
186
|
+
["--xxx", "FOO"]
|
187
|
+
|
188
|
+
===== Long Names with Optional Arguments
|
189
|
+
|
190
|
+
A long name followed (with whitespace) by a dummy word in square brackets
|
191
|
+
defines an option that allows an optional argument.
|
192
|
+
|
193
|
+
File +long_optional.rb+ defines an option <tt>--xxx</tt>
|
194
|
+
that allows an optional argument.
|
195
|
+
|
196
|
+
:include: ruby/long_optional.rb
|
197
|
+
|
198
|
+
Executions:
|
199
|
+
|
200
|
+
$ ruby long_optional.rb --help
|
201
|
+
Usage: long_optional [options]
|
202
|
+
--xxx [XXX] Long name with optional argument
|
203
|
+
$ ruby long_optional.rb --xxx
|
204
|
+
["--xxx", nil]
|
205
|
+
$ ruby long_optional.rb --xxx FOO
|
206
|
+
["--xxx", "FOO"]
|
207
|
+
|
208
|
+
===== Long Names with Negation
|
209
|
+
|
210
|
+
A long name may be defined with both positive and negative senses.
|
211
|
+
|
212
|
+
File +long_with_negation.rb+ defines an option that has both senses.
|
213
|
+
|
214
|
+
:include: ruby/long_with_negation.rb
|
215
|
+
|
216
|
+
Executions:
|
217
|
+
|
218
|
+
$ ruby long_with_negation.rb --help
|
219
|
+
Usage: long_with_negation [options]
|
220
|
+
--[no-]binary Long name with negation
|
221
|
+
$ ruby long_with_negation.rb --binary
|
222
|
+
[true, TrueClass]
|
223
|
+
$ ruby long_with_negation.rb --no-binary
|
224
|
+
[false, FalseClass]
|
225
|
+
|
226
|
+
==== Mixed Names
|
227
|
+
|
228
|
+
An option may have both short and long names.
|
229
|
+
|
230
|
+
File +mixed_names.rb+ defines a mixture of short and long names.
|
231
|
+
|
232
|
+
:include: ruby/mixed_names.rb
|
233
|
+
|
234
|
+
Executions:
|
235
|
+
|
236
|
+
$ ruby mixed_names.rb --help
|
237
|
+
Usage: mixed_names [options]
|
238
|
+
-x, --xxx Short and long, no argument
|
239
|
+
-y, --yyyYYY Short and long, required argument
|
240
|
+
-z, --zzz [ZZZ] Short and long, optional argument
|
241
|
+
$ ruby mixed_names.rb -x
|
242
|
+
["--xxx", true]
|
243
|
+
$ ruby mixed_names.rb --xxx
|
244
|
+
["--xxx", true]
|
245
|
+
$ ruby mixed_names.rb -y
|
246
|
+
mixed_names.rb:12:in `<main>': missing argument: -y (OptionParser::MissingArgument)
|
247
|
+
$ ruby mixed_names.rb -y FOO
|
248
|
+
["--yyy", "FOO"]
|
249
|
+
$ ruby mixed_names.rb --yyy
|
250
|
+
mixed_names.rb:12:in `<main>': missing argument: --yyy (OptionParser::MissingArgument)
|
251
|
+
$ ruby mixed_names.rb --yyy BAR
|
252
|
+
["--yyy", "BAR"]
|
253
|
+
$ ruby mixed_names.rb -z
|
254
|
+
["--zzz", nil]
|
255
|
+
$ ruby mixed_names.rb -z BAZ
|
256
|
+
["--zzz", "BAZ"]
|
257
|
+
$ ruby mixed_names.rb --zzz
|
258
|
+
["--zzz", nil]
|
259
|
+
$ ruby mixed_names.rb --zzz BAT
|
260
|
+
["--zzz", "BAT"]
|
261
|
+
|
262
|
+
=== Argument Keywords
|
263
|
+
|
264
|
+
As seen above, a given option name string may itself
|
265
|
+
indicate whether the option has no argument, a required argument,
|
266
|
+
or an optional argument.
|
267
|
+
|
268
|
+
An alternative is to use a separate symbol keyword,
|
269
|
+
which is one of <tt>:NONE</tt> (the default),
|
270
|
+
<tt>:REQUIRED</tt>, <tt>:OPTIONAL</tt>.
|
271
|
+
|
272
|
+
File +argument_keywords.rb+ defines an option with a required argument.
|
273
|
+
|
274
|
+
:include: ruby/argument_keywords.rb
|
275
|
+
|
276
|
+
Executions:
|
277
|
+
|
278
|
+
$ ruby argument_keywords.rb --help
|
279
|
+
Usage: argument_keywords [options]
|
280
|
+
-x, --xxx Required argument
|
281
|
+
$ ruby argument_styles.rb --xxx
|
282
|
+
argument_styles.rb:6:in `<main>': missing argument: --xxx (OptionParser::MissingArgument)
|
283
|
+
$ ruby argument_styles.rb --xxx FOO
|
284
|
+
["--xxx", "FOO"]
|
285
|
+
|
286
|
+
=== Argument Strings
|
287
|
+
|
288
|
+
Still another way to specify a required argument
|
289
|
+
is to define it in a string separate from the name string.
|
290
|
+
|
291
|
+
File +argument_strings.rb+ defines an option with a required argument.
|
292
|
+
|
293
|
+
:include: ruby/argument_strings.rb
|
294
|
+
|
295
|
+
Executions:
|
296
|
+
|
297
|
+
$ ruby argument_strings.rb --help
|
298
|
+
Usage: argument_strings [options]
|
299
|
+
-x, --xxx=XXX Required argument
|
300
|
+
$ ruby argument_strings.rb --xxx
|
301
|
+
argument_strings.rb:9:in `<main>': missing argument: --xxx (OptionParser::MissingArgument)
|
302
|
+
$ ruby argument_strings.rb --xxx FOO
|
303
|
+
["--xxx", "FOO"]
|
304
|
+
|
305
|
+
=== Argument Values
|
306
|
+
|
307
|
+
Permissible argument values may be restricted
|
308
|
+
either by specifying explicit values
|
309
|
+
or by providing a pattern that the given value must match.
|
310
|
+
|
311
|
+
==== Explicit Argument Values
|
312
|
+
|
313
|
+
You can specify argument values in either of two ways:
|
314
|
+
|
315
|
+
- Specify values an array of strings.
|
316
|
+
- Specify values a hash.
|
317
|
+
|
318
|
+
===== Explicit Values in Array
|
319
|
+
|
320
|
+
You can specify explicit argument values in an array of strings.
|
321
|
+
The argument value must be one of those strings, or an unambiguous abbreviation.
|
322
|
+
|
323
|
+
File +explicit_array_values.rb+ defines options with explicit argument values.
|
324
|
+
|
325
|
+
:include: ruby/explicit_array_values.rb
|
326
|
+
|
327
|
+
Executions:
|
328
|
+
|
329
|
+
$ ruby explicit_array_values.rb --help
|
330
|
+
Usage: explicit_array_values [options]
|
331
|
+
-xXXX Values for required argument
|
332
|
+
-y [YYY] Values for optional argument
|
333
|
+
$ ruby explicit_array_values.rb -x
|
334
|
+
explicit_array_values.rb:9:in `<main>': missing argument: -x (OptionParser::MissingArgument)
|
335
|
+
$ ruby explicit_array_values.rb -x foo
|
336
|
+
["-x", "foo"]
|
337
|
+
$ ruby explicit_array_values.rb -x f
|
338
|
+
["-x", "foo"]
|
339
|
+
$ ruby explicit_array_values.rb -x bar
|
340
|
+
["-x", "bar"]
|
341
|
+
$ ruby explicit_array_values.rb -y ba
|
342
|
+
explicit_array_values.rb:9:in `<main>': ambiguous argument: -y ba (OptionParser::AmbiguousArgument)
|
343
|
+
$ ruby explicit_array_values.rb -x baz
|
344
|
+
explicit_array_values.rb:9:in `<main>': invalid argument: -x baz (OptionParser::InvalidArgument)
|
345
|
+
|
346
|
+
|
347
|
+
===== Explicit Values in Hash
|
348
|
+
|
349
|
+
You can specify explicit argument values in a hash with string keys.
|
350
|
+
The value passed must be one of those keys, or an unambiguous abbreviation;
|
351
|
+
the value yielded will be the value for that key.
|
352
|
+
|
353
|
+
File +explicit_hash_values.rb+ defines options with explicit argument values.
|
354
|
+
|
355
|
+
:include: ruby/explicit_hash_values.rb
|
356
|
+
|
357
|
+
Executions:
|
358
|
+
|
359
|
+
$ ruby explicit_hash_values.rb --help
|
360
|
+
Usage: explicit_hash_values [options]
|
361
|
+
-xXXX Values for required argument
|
362
|
+
-y [YYY] Values for optional argument
|
363
|
+
$ ruby explicit_hash_values.rb -x
|
364
|
+
explicit_hash_values.rb:9:in `<main>': missing argument: -x (OptionParser::MissingArgument)
|
365
|
+
$ ruby explicit_hash_values.rb -x foo
|
366
|
+
["-x", 0]
|
367
|
+
$ ruby explicit_hash_values.rb -x f
|
368
|
+
["-x", 0]
|
369
|
+
$ ruby explicit_hash_values.rb -x bar
|
370
|
+
["-x", 1]
|
371
|
+
$ ruby explicit_hash_values.rb -x baz
|
372
|
+
explicit_hash_values.rb:9:in `<main>': invalid argument: -x baz (OptionParser::InvalidArgument)
|
373
|
+
$ ruby explicit_hash_values.rb -y
|
374
|
+
["-y", nil]
|
375
|
+
$ ruby explicit_hash_values.rb -y baz
|
376
|
+
["-y", 2]
|
377
|
+
$ ruby explicit_hash_values.rb -y bat
|
378
|
+
["-y", 3]
|
379
|
+
$ ruby explicit_hash_values.rb -y ba
|
380
|
+
explicit_hash_values.rb:9:in `<main>': ambiguous argument: -y ba (OptionParser::AmbiguousArgument)
|
381
|
+
$ ruby explicit_hash_values.rb -y bam
|
382
|
+
["-y", nil]
|
383
|
+
|
384
|
+
==== Argument Value Patterns
|
385
|
+
|
386
|
+
You can restrict permissible argument values
|
387
|
+
by specifying a Regexp that the given argument must match.
|
388
|
+
|
389
|
+
File +matched_values.rb+ defines options with matched argument values.
|
390
|
+
|
391
|
+
:include: ruby/matched_values.rb
|
392
|
+
|
393
|
+
Executions:
|
394
|
+
|
395
|
+
$ ruby matched_values.rb --help
|
396
|
+
Usage: matched_values [options]
|
397
|
+
--xxx XXX Matched values
|
398
|
+
$ ruby matched_values.rb --xxx foo
|
399
|
+
["--xxx", "foo"]
|
400
|
+
$ ruby matched_values.rb --xxx FOO
|
401
|
+
["--xxx", "FOO"]
|
402
|
+
$ ruby matched_values.rb --xxx bar
|
403
|
+
matched_values.rb:6:in `<main>': invalid argument: --xxx bar (OptionParser::InvalidArgument)
|
404
|
+
|
405
|
+
=== Argument Converters
|
406
|
+
|
407
|
+
An option can specify that its argument is to be converted
|
408
|
+
from the default +String+ to an instance of another class.
|
409
|
+
|
410
|
+
There are a number of built-in converters.
|
411
|
+
You can also define custom converters.
|
412
|
+
|
413
|
+
See {Argument Converters}[./argument_converters.rdoc].
|
414
|
+
|
415
|
+
=== Descriptions
|
416
|
+
|
417
|
+
A description parameter is any string parameter
|
418
|
+
that is not recognized as an
|
419
|
+
{option name}[#label-Option+Names] or a
|
420
|
+
{terminator}[#label-Terminators];
|
421
|
+
in other words, it does not begin with a hyphen.
|
422
|
+
|
423
|
+
You may give any number of description parameters;
|
424
|
+
each becomes a line in the text generated by option <tt>--help</tt>.
|
425
|
+
|
426
|
+
File +descriptions.rb+ has six strings in its array +descriptions+.
|
427
|
+
These are all passed as parameters to OptionParser#on, so that they
|
428
|
+
all, line for line, become the option's description.
|
429
|
+
|
430
|
+
:include: ruby/descriptions.rb
|
431
|
+
|
432
|
+
Executions:
|
433
|
+
|
434
|
+
$ ruby descriptions.rb --help
|
435
|
+
Usage: descriptions [options]
|
436
|
+
--xxx Lorem ipsum dolor sit amet, consectetuer
|
437
|
+
adipiscing elit. Aenean commodo ligula eget.
|
438
|
+
Aenean massa. Cum sociis natoque penatibus
|
439
|
+
et magnis dis parturient montes, nascetur
|
440
|
+
ridiculus mus. Donec quam felis, ultricies
|
441
|
+
nec, pellentesque eu, pretium quis, sem.
|
442
|
+
$ ruby descriptions.rb --xxx
|
443
|
+
["--xxx", true]
|
444
|
+
|
445
|
+
=== Option Handlers
|
446
|
+
|
447
|
+
The handler for an option is an executable that will be called
|
448
|
+
when the option is encountered. The handler may be:
|
449
|
+
|
450
|
+
- A block (this is most often seen).
|
451
|
+
- A proc.
|
452
|
+
- A method.
|
453
|
+
|
454
|
+
==== Handler Blocks
|
455
|
+
|
456
|
+
An option handler may be a block.
|
457
|
+
|
458
|
+
File +block.rb+ defines an option that has a handler block.
|
459
|
+
|
460
|
+
:include: ruby/block.rb
|
461
|
+
|
462
|
+
Executions:
|
463
|
+
|
464
|
+
$ ruby block.rb --help
|
465
|
+
Usage: block [options]
|
466
|
+
--xxx Option with no argument
|
467
|
+
--yyy YYY Option with required argument
|
468
|
+
$ ruby block.rb --xxx
|
469
|
+
["Handler block for -xxx called with value:", true]
|
470
|
+
$ ruby block.rb --yyy FOO
|
471
|
+
["Handler block for -yyy called with value:", "FOO"]
|
472
|
+
|
473
|
+
==== Handler Procs
|
474
|
+
|
475
|
+
An option handler may be a Proc.
|
476
|
+
|
477
|
+
File +proc.rb+ defines an option that has a handler proc.
|
478
|
+
|
479
|
+
:include: ruby/proc.rb
|
480
|
+
|
481
|
+
Executions:
|
482
|
+
|
483
|
+
$ ruby proc.rb --help
|
484
|
+
Usage: proc [options]
|
485
|
+
--xxx Option with no argument
|
486
|
+
--yyy YYY Option with required argument
|
487
|
+
$ ruby proc.rb --xxx
|
488
|
+
["Handler proc for -xxx called with value:", true]
|
489
|
+
$ ruby proc.rb --yyy FOO
|
490
|
+
["Handler proc for -yyy called with value:", "FOO"]
|
491
|
+
|
492
|
+
==== Handler Methods
|
493
|
+
|
494
|
+
An option handler may be a Method.
|
495
|
+
|
496
|
+
File +proc.rb+ defines an option that has a handler method.
|
497
|
+
|
498
|
+
:include: ruby/method.rb
|
499
|
+
|
500
|
+
Executions:
|
501
|
+
|
502
|
+
$ ruby method.rb --help
|
503
|
+
Usage: method [options]
|
504
|
+
--xxx Option with no argument
|
505
|
+
--yyy YYY Option with required argument
|
506
|
+
$ ruby method.rb --xxx
|
507
|
+
["Handler method for -xxx called with value:", true]
|
508
|
+
$ ruby method.rb --yyy FOO
|
509
|
+
["Handler method for -yyy called with value:", "FOO"]
|
@@ -0,0 +1,9 @@
|
|
1
|
+
require 'optparse'
|
2
|
+
parser = OptionParser.new
|
3
|
+
parser.on('-x', '--xxx=VALUE', %w[ABC def], 'Argument abbreviations') do |value|
|
4
|
+
p ['--xxx', value]
|
5
|
+
end
|
6
|
+
parser.on('-y', '--yyy=VALUE', {"abc"=>"XYZ", def: "FOO"}, 'Argument abbreviations') do |value|
|
7
|
+
p ['--yyy', value]
|
8
|
+
end
|
9
|
+
parser.parse!
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# Require the OptionParser code.
|
2
|
+
require 'optparse'
|
3
|
+
# Create an OptionParser object.
|
4
|
+
parser = OptionParser.new
|
5
|
+
# Define one or more options.
|
6
|
+
parser.on('-x', 'Whether to X') do |value|
|
7
|
+
p ['x', value]
|
8
|
+
end
|
9
|
+
parser.on('-y', 'Whether to Y') do |value|
|
10
|
+
p ['y', value]
|
11
|
+
end
|
12
|
+
parser.on('-z', 'Whether to Z') do |value|
|
13
|
+
p ['z', value]
|
14
|
+
end
|
15
|
+
# Parse the command line and return pared-down ARGV.
|
16
|
+
p parser.parse!
|
17
|
+
|
@@ -0,0 +1,9 @@
|
|
1
|
+
require 'optparse'
|
2
|
+
parser = OptionParser.new
|
3
|
+
parser.on('--xxx', 'Option with no argument') do |value|
|
4
|
+
p ['Handler block for -xxx called with value:', value]
|
5
|
+
end
|
6
|
+
parser.on('--yyy YYY', 'Option with required argument') do |value|
|
7
|
+
p ['Handler block for -yyy called with value:', value]
|
8
|
+
end
|
9
|
+
parser.parse!
|
@@ -0,0 +1,8 @@
|
|
1
|
+
require 'optparse'
|
2
|
+
parser = OptionParser.new
|
3
|
+
parser.on('-x', '--xxx', 'Short and long, no argument')
|
4
|
+
parser.on('-yYYY', '--yyy', 'Short and long, required argument')
|
5
|
+
parser.on('-z [ZZZ]', '--zzz', 'Short and long, optional argument')
|
6
|
+
options = {}
|
7
|
+
parser.parse!(into: options)
|
8
|
+
p options
|
@@ -0,0 +1,8 @@
|
|
1
|
+
require 'optparse'
|
2
|
+
parser = OptionParser.new
|
3
|
+
parser.on('-x', '--xxx', 'Short and long, no argument')
|
4
|
+
parser.on('-yYYY', '--yyy', 'Short and long, required argument')
|
5
|
+
parser.on('-z [ZZZ]', '--zzz', 'Short and long, optional argument')
|
6
|
+
options = {yyy: 'AAA', zzz: 'BBB'}
|
7
|
+
parser.parse!(into: options)
|
8
|
+
p options
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'optparse'
|
2
|
+
parser = OptionParser.new
|
3
|
+
description = <<-EOT
|
4
|
+
Lorem ipsum dolor sit amet, consectetuer
|
5
|
+
adipiscing elit. Aenean commodo ligula eget.
|
6
|
+
Aenean massa. Cum sociis natoque penatibus
|
7
|
+
et magnis dis parturient montes, nascetur
|
8
|
+
ridiculus mus. Donec quam felis, ultricies
|
9
|
+
nec, pellentesque eu, pretium quis, sem.
|
10
|
+
EOT
|
11
|
+
descriptions = description.split($/)
|
12
|
+
parser.on('--xxx', *descriptions) do |value|
|
13
|
+
p ['--xxx', value]
|
14
|
+
end
|
15
|
+
parser.parse!
|
@@ -0,0 +1,9 @@
|
|
1
|
+
require 'optparse'
|
2
|
+
parser = OptionParser.new
|
3
|
+
parser.on('-xXXX', ['foo', 'bar'], 'Values for required argument' ) do |value|
|
4
|
+
p ['-x', value]
|
5
|
+
end
|
6
|
+
parser.on('-y [YYY]', ['baz', 'bat'], 'Values for optional argument') do |value|
|
7
|
+
p ['-y', value]
|
8
|
+
end
|
9
|
+
parser.parse!
|
@@ -0,0 +1,9 @@
|
|
1
|
+
require 'optparse'
|
2
|
+
parser = OptionParser.new
|
3
|
+
parser.on('-xXXX', {foo: 0, bar: 1}, 'Values for required argument' ) do |value|
|
4
|
+
p ['-x', value]
|
5
|
+
end
|
6
|
+
parser.on('-y [YYY]', {baz: 2, bat: 3}, 'Values for optional argument') do |value|
|
7
|
+
p ['-y', value]
|
8
|
+
end
|
9
|
+
parser.parse!
|