@@ -39,10 +39,15 @@ The class also has:
39
39
- {Short Option Names}[#label-Short+Option+Names]
40
40
- {Long Option Names}[#label-Long+Option+Names]
41
41
- {Mixing Option Names}[#label-Mixing+Option+Names]
42
+ - {Command-Line Abbreviations}[#label-Command-Line+Abbreviations]
42
43
- {Option Arguments}[#label-Option+Arguments]
43
44
- {Option with No Argument}[#label-Option+with+No+Argument]
44
45
- {Option with Required Argument}[#label-Option+with+Required+Argument]
45
46
- {Option with Optional Argument}[#label-Option+with+Optional+Argument]
47
+ - {Keyword Argument <tt>into<tt>}[#label-Keyword+Argument+into]
48
+ - {Collecting Options}[#label-Collecting+Options]
49
+ - {Checking for Missing Options}[#label-Checking+for+Missing+Options]
50
+ - {Default Values for Options}[#label-Default+Values+for+Options]
46
51
- {Argument Converters}[#label-Argument+Converters]
47
52
48
53
=== Defining Options
@@ -185,6 +190,47 @@ Executions:
185
190
$ ruby mixed_names.rb --zzz BAT
186
191
["--zzz", "BAT"]
187
192
193
+ ==== Command-Line Abbreviations
194
+
195
+ By default, abbreviations for command-line option names are allowed.
196
+ An abbreviated option is valid if it is unique among abbreviated option names.
197
+
198
+ :include: ruby/abbreviation.rb
199
+
200
+ Executions:
201
+
202
+ $ ruby abbreviation.rb --help
203
+ Usage: abbreviation [options]
204
+ -n, --dry-run
205
+ -d, --draft
206
+ $ ruby abbreviation.rb -n
207
+ ["--dry-run", true]
208
+ $ ruby abbreviation.rb --dry-run
209
+ ["--dry-run", true]
210
+ $ ruby abbreviation.rb -d
211
+ ["--draft", true]
212
+ $ ruby abbreviation.rb --draft
213
+ ["--draft", true]
214
+ $ ruby abbreviation.rb --d
215
+ abbreviation.rb:9:in `<main>': ambiguous option: --d (OptionParser::AmbiguousOption)
216
+ $ ruby abbreviation.rb --dr
217
+ abbreviation.rb:9:in `<main>': ambiguous option: --dr (OptionParser::AmbiguousOption)
218
+ $ ruby abbreviation.rb --dry
219
+ ["--dry-run", true]
220
+ $ ruby abbreviation.rb --dra
221
+ ["--draft", true]
222
+
223
+ You can disable abbreviation using method +require_exact+.
224
+
225
+ :include: ruby/no_abbreviation.rb
226
+
227
+ Executions:
228
+
229
+ $ ruby no_abbreviation.rb --dry-ru
230
+ no_abbreviation.rb:10:in `<main>': invalid option: --dry-ru (OptionParser::InvalidOption)
231
+ $ ruby no_abbreviation.rb --dry-run
232
+ ["--dry-run", true]
233
+
188
234
=== Option Arguments
189
235
190
236
An option may take no argument, a required argument, or an optional argument.
@@ -247,12 +293,96 @@ Executions:
247
293
248
294
Omitting an optional argument does not raise an error.
249
295
296
+ === Keyword Argument +into+
297
+
298
+ In parsing options, you can add keyword option +into+ with a hash-like argument;
299
+ each parsed option will be added as a name/value pair.
300
+
301
+ This is useful for:
302
+
303
+ - Collecting options.
304
+ - Checking for missing options.
305
+ - Providing default values for options.
306
+
307
+ ==== Collecting Options
308
+
309
+ Use keyword argument +into+ to collect options.
310
+
311
+ :include: ruby/collected_options.rb
312
+
313
+ Executions:
314
+
315
+ $ ruby collected_options.rb --help
316
+ Usage: into [options]
317
+ -x, --xxx Short and long, no argument
318
+ -y, --yyyYYY Short and long, required argument
319
+ -z, --zzz [ZZZ] Short and long, optional argument
320
+ $ ruby collected_options.rb --xxx
321
+ {:xxx=>true}
322
+ $ ruby collected_options.rb --xxx --yyy FOO
323
+ {:xxx=>true, :yyy=>"FOO"}
324
+ $ ruby collected_options.rb --xxx --yyy FOO --zzz Bar
325
+ {:xxx=>true, :yyy=>"FOO", :zzz=>"Bar"}
326
+ $ ruby collected_options.rb --xxx --yyy FOO --yyy BAR
327
+ {:xxx=>true, :yyy=>"BAR"}
328
+
329
+ Note in the last execution that the argument value for option <tt>--yyy</tt>
330
+ was overwritten.
331
+
332
+ ==== Checking for Missing Options
333
+
334
+ Use the collected options to check for missing options.
335
+
336
+ :include: ruby/missing_options.rb
337
+
338
+ Executions:
339
+
340
+ $ ruby missing_options.rb --help
341
+ Usage: missing_options [options]
342
+ -x, --xxx Short and long, no argument
343
+ -y, --yyyYYY Short and long, required argument
344
+ -z, --zzz [ZZZ] Short and long, optional argument
345
+ $ ruby missing_options.rb --yyy FOO
346
+ missing_options.rb:11:in `<main>': Missing required options: [:xxx, :zzz] (RuntimeError)
347
+
348
+ ==== Default Values for Options
349
+
350
+ Initialize the +into+ argument to define default values for options.
351
+
352
+ :include: ruby/default_values.rb
353
+
354
+ Executions:
355
+
356
+ $ ruby default_values.rb --help
357
+ Usage: default_values [options]
358
+ -x, --xxx Short and long, no argument
359
+ -y, --yyyYYY Short and long, required argument
360
+ -z, --zzz [ZZZ] Short and long, optional argument
361
+ $ ruby default_values.rb --yyy FOO
362
+ {:yyy=>"FOO", :zzz=>"BBB"}
363
+
364
+
250
365
=== Argument Converters
251
366
252
367
An option can specify that its argument is to be converted
253
368
from the default \String to an instance of another class.
254
-
255
369
There are a number of built-in converters.
256
- You can also define custom converters.
257
370
258
- See {Argument Converters}[./argument_converters_rdoc.html].
371
+ Example: File +date.rb+
372
+ defines an option whose argument is to be converted to a \Date object.
373
+ The argument is converted by method Date#parse.
374
+
375
+ :include: ruby/date.rb
376
+
377
+ Executions:
378
+
379
+ $ ruby date.rb --date 2001-02-03
380
+ [#<Date: 2001-02-03 ((2451944j,0s,0n),+0s,2299161j)>, Date]
381
+ $ ruby date.rb --date 20010203
382
+ [#<Date: 2001-02-03 ((2451944j,0s,0n),+0s,2299161j)>, Date]
383
+ $ ruby date.rb --date "3rd Feb 2001"
384
+ [#<Date: 2001-02-03 ((2451944j,0s,0n),+0s,2299161j)>, Date]
385
+
386
+ You can also define custom converters.
387
+ See {Argument Converters}[./argument_converters_rdoc.html]
388
+ for both built-in and custom converters.
0 commit comments