optparse 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (69) hide show
  1. checksums.yaml +4 -4
  2. data/ChangeLog +264 -0
  3. data/doc/optparse/argument_converters.rdoc +380 -0
  4. data/doc/optparse/creates_option.rdoc +7 -0
  5. data/doc/optparse/option_params.rdoc +509 -0
  6. data/doc/optparse/ruby/argument_keywords.rb +6 -0
  7. data/doc/optparse/ruby/argument_strings.rb +6 -0
  8. data/doc/optparse/ruby/argv.rb +2 -0
  9. data/doc/optparse/ruby/array.rb +6 -0
  10. data/doc/optparse/ruby/basic.rb +17 -0
  11. data/doc/optparse/ruby/block.rb +9 -0
  12. data/doc/optparse/ruby/collected_options.rb +8 -0
  13. data/doc/optparse/ruby/custom_converter.rb +9 -0
  14. data/doc/optparse/ruby/date.rb +6 -0
  15. data/doc/optparse/ruby/datetime.rb +6 -0
  16. data/doc/optparse/ruby/decimal_integer.rb +7 -0
  17. data/doc/optparse/ruby/decimal_numeric.rb +7 -0
  18. data/doc/optparse/ruby/default_values.rb +8 -0
  19. data/doc/optparse/ruby/descriptions.rb +15 -0
  20. data/doc/optparse/ruby/explicit_array_values.rb +9 -0
  21. data/doc/optparse/ruby/explicit_hash_values.rb +9 -0
  22. data/doc/optparse/ruby/false_class.rb +6 -0
  23. data/doc/optparse/ruby/float.rb +6 -0
  24. data/doc/optparse/ruby/help.rb +18 -0
  25. data/doc/optparse/ruby/help_banner.rb +7 -0
  26. data/doc/optparse/ruby/help_format.rb +25 -0
  27. data/doc/optparse/ruby/help_program_name.rb +7 -0
  28. data/doc/optparse/ruby/integer.rb +6 -0
  29. data/doc/optparse/ruby/long_names.rb +9 -0
  30. data/doc/optparse/ruby/long_optional.rb +6 -0
  31. data/doc/optparse/ruby/long_required.rb +6 -0
  32. data/doc/optparse/ruby/long_simple.rb +9 -0
  33. data/doc/optparse/ruby/long_with_negation.rb +6 -0
  34. data/doc/optparse/ruby/match_converter.rb +9 -0
  35. data/doc/optparse/ruby/matched_values.rb +6 -0
  36. data/doc/optparse/ruby/method.rb +11 -0
  37. data/doc/optparse/ruby/missing_options.rb +12 -0
  38. data/doc/optparse/ruby/mixed_names.rb +12 -0
  39. data/doc/optparse/ruby/name_abbrev.rb +9 -0
  40. data/doc/optparse/ruby/no_abbreviation.rb +10 -0
  41. data/doc/optparse/ruby/numeric.rb +6 -0
  42. data/doc/optparse/ruby/object.rb +6 -0
  43. data/doc/optparse/ruby/octal_integer.rb +7 -0
  44. data/doc/optparse/ruby/optional_argument.rb +9 -0
  45. data/doc/optparse/ruby/parse.rb +13 -0
  46. data/doc/optparse/ruby/parse_bang.rb +13 -0
  47. data/doc/optparse/ruby/proc.rb +13 -0
  48. data/doc/optparse/ruby/regexp.rb +6 -0
  49. data/doc/optparse/ruby/required_argument.rb +9 -0
  50. data/doc/optparse/ruby/shellwords.rb +6 -0
  51. data/doc/optparse/ruby/short_names.rb +9 -0
  52. data/doc/optparse/ruby/short_optional.rb +6 -0
  53. data/doc/optparse/ruby/short_range.rb +6 -0
  54. data/doc/optparse/ruby/short_required.rb +6 -0
  55. data/doc/optparse/ruby/short_simple.rb +9 -0
  56. data/doc/optparse/ruby/string.rb +6 -0
  57. data/doc/optparse/ruby/terminator.rb +6 -0
  58. data/doc/optparse/ruby/time.rb +6 -0
  59. data/doc/optparse/ruby/true_class.rb +6 -0
  60. data/doc/optparse/ruby/uri.rb +6 -0
  61. data/doc/optparse/tutorial.rdoc +835 -0
  62. data/lib/optparse/kwargs.rb +2 -0
  63. data/lib/optparse.rb +33 -75
  64. metadata +62 -7
  65. data/Rakefile +0 -10
  66. data/optparse.gemspec +0 -33
  67. data/rakelib/changelogs.rake +0 -34
  68. data/rakelib/epoch.rake +0 -5
  69. data/rakelib/version.rake +0 -47
@@ -0,0 +1,835 @@
1
+ == Tutorial
2
+
3
+ === Why \OptionParser?
4
+
5
+ When a Ruby program executes, it captures its command-line arguments
6
+ and options into variable ARGV.
7
+ This simple program just prints its \ARGV:
8
+
9
+ :include: ruby/argv.rb
10
+
11
+ Execution, with arguments and options:
12
+
13
+ $ ruby argv.rb foo --bar --baz bat bam
14
+ ["foo", "--bar", "--baz", "bat", "bam"]
15
+
16
+ The executing program is responsible for parsing and handling
17
+ the command-line options.
18
+
19
+ OptionParser offers methods for parsing and handling those options.
20
+
21
+ With \OptionParser, you can define options so that for each option:
22
+
23
+ - The code that defines the option and code that handles that option
24
+ are in the same place.
25
+ - The option may take no argument, a required argument, or an optional argument.
26
+ - The argument may be automatically converted to a specified class.
27
+ - The argument may be restricted to specified _forms_.
28
+ - The argument may be restricted to specified _values_.
29
+
30
+ The class also has method #help, which displays automatically-generated help text.
31
+
32
+ === Contents
33
+
34
+ - {To Begin With}[#label-To+Begin+With]
35
+ - {Defining Options}[#label-Defining+Options]
36
+ - {Option Names}[#label-Option+Names]
37
+ - {Short Option Names}[#label-Short+Option+Names]
38
+ - {Long Option Names}[#label-Long+Option+Names]
39
+ - {Mixing Option Names}[#label-Mixing+Option+Names]
40
+ - {Option Name Abbreviations}[#label-Option+Name+Abbreviations]
41
+ - {Option Arguments}[#label-Option+Arguments]
42
+ - {Option with No Argument}[#label-Option+with+No+Argument]
43
+ - {Option with Required Argument}[#label-Option+with+Required+Argument]
44
+ - {Option with Optional Argument}[#label-Option+with+Optional+Argument]
45
+ - {Argument Abbreviations}[#label-Argument+Abbreviations]
46
+ - {Argument Values}[#label-Argument+Values]
47
+ - {Explicit Argument Values}[#label-Explicit+Argument+Values]
48
+ - {Explicit Values in Array}[#label-Explicit+Values+in+Array]
49
+ - {Explicit Values in Hash}[#label-Explicit+Values+in+Hash]
50
+ - {Argument Value Patterns}[#label-Argument+Value+Patterns]
51
+ - {Keyword Argument into}[#label-Keyword+Argument+into]
52
+ - {Collecting Options}[#label-Collecting+Options]
53
+ - {Checking for Missing Options}[#label-Checking+for+Missing+Options]
54
+ - {Default Values for Options}[#label-Default+Values+for+Options]
55
+ - {Argument Converters}[#label-Argument+Converters]
56
+ - {Help}[#label-Help]
57
+ - {Top List and Base List}[#label-Top+List+and+Base+List]
58
+ - {Defining Options}[#label-Defining+Options]
59
+ - {Parsing}[#label-Parsing]
60
+ - {Method parse!}[#label-Method+parse-21]
61
+ - {Method parse}[#label-Method+parse]
62
+ - {Method order!}[#label-Method+order-21]
63
+ - {Method order}[#label-Method+order]
64
+ - {Method permute!}[#label-Method+permute-21]
65
+ - {Method permute}[#label-Method+permute]
66
+
67
+ === To Begin With
68
+
69
+ To use \OptionParser:
70
+
71
+ 1. Require the \OptionParser code.
72
+ 2. Create an \OptionParser object.
73
+ 3. Define one or more options.
74
+ 4. Parse the command line.
75
+
76
+ File +basic.rb+ defines three options, <tt>-x</tt>,
77
+ <tt>-y</tt>, and <tt>-z</tt>, each with a descriptive string,
78
+ and each with a block.
79
+
80
+ :include: ruby/basic.rb
81
+
82
+ From these defined options, the parser automatically builds help text:
83
+
84
+ $ ruby basic.rb --help
85
+ Usage: basic [options]
86
+ -x Whether to X
87
+ -y Whether to Y
88
+ -z Whether to Z
89
+
90
+ When an option is found during parsing,
91
+ the block defined for the option is called with the argument value.
92
+ An invalid option raises an exception.
93
+
94
+ Method #parse!, which is used most often in this tutorial,
95
+ removes from \ARGV the options and arguments it finds,
96
+ leaving other non-option arguments for the program to handle on its own.
97
+ The method returns the possibly-reduced \ARGV array.
98
+
99
+ Executions:
100
+
101
+ $ ruby basic.rb -x -z
102
+ ["x", true]
103
+ ["z", true]
104
+ []
105
+ $ ruby basic.rb -z -y -x
106
+ ["z", true]
107
+ ["y", true]
108
+ ["x", true]
109
+ []
110
+ $ ruby basic.rb -x input_file.txt output_file.txt
111
+ ["x", true]
112
+ ["input_file.txt", "output_file.txt"]
113
+ $ ruby basic.rb -a
114
+ basic.rb:16:in `<main>': invalid option: -a (OptionParser::InvalidOption)
115
+
116
+ === Defining Options
117
+
118
+ A common way to define an option in \OptionParser
119
+ is with instance method OptionParser#on.
120
+
121
+ The method may be called with any number of arguments
122
+ (whose order does not matter),
123
+ and may also have a trailing optional keyword argument +into+.
124
+
125
+ The given arguments determine the characteristics of the new option.
126
+ These may include:
127
+
128
+ - One or more short option names.
129
+ - One or more long option names.
130
+ - Whether the option takes no argument, an optional argument, or a required argument.
131
+ - Acceptable _forms_ for the argument.
132
+ - Acceptable _values_ for the argument.
133
+ - A proc or method to be called when the parser encounters the option.
134
+ - String descriptions for the option.
135
+
136
+ === Option Names
137
+
138
+ You can give an option one or more names of two types:
139
+
140
+ - Short (1-character) name, beginning with one hyphen (<tt>-</tt>).
141
+ - Long (multi-character) name, beginning with two hyphens (<tt>--</tt>).
142
+
143
+ ==== Short Option Names
144
+
145
+ A short option name consists of a hyphen and a single character.
146
+
147
+ File +short_names.rb+
148
+ defines an option with a short name, <tt>-x</tt>,
149
+ and an option with two short names (aliases, in effect) <tt>-y</tt> and <tt>-z</tt>.
150
+
151
+ :include: ruby/short_names.rb
152
+
153
+ Executions:
154
+
155
+ $ ruby short_names.rb --help
156
+ Usage: short_names [options]
157
+ -x Short name
158
+ -1, -% Two short names
159
+ $ ruby short_names.rb -x
160
+ ["x", true]
161
+ $ ruby short_names.rb -1
162
+ ["-1 or -%", true]
163
+ $ ruby short_names.rb -%
164
+ ["-1 or -%", true]
165
+
166
+ Multiple short names can "share" a hyphen:
167
+
168
+ $ ruby short_names.rb -x1%
169
+ ["x", true]
170
+ ["-1 or -%", true]
171
+ ["-1 or -%", true]
172
+
173
+ ==== Long Option Names
174
+
175
+ A long option name consists of two hyphens and a one or more characters
176
+ (usually two or more characters).
177
+
178
+ File +long_names.rb+
179
+ defines an option with a long name, <tt>--xxx</tt>,
180
+ and an option with two long names (aliases, in effect) <tt>--y1%</tt> and <tt>--z2#</tt>.
181
+
182
+ :include: ruby/long_names.rb
183
+
184
+ Executions:
185
+
186
+ $ ruby long_names.rb --help
187
+ Usage: long_names [options]
188
+ --xxx Long name
189
+ --y1%, --z2# Two long names
190
+ $ ruby long_names.rb --xxx
191
+ ["-xxx", true]
192
+ $ ruby long_names.rb --y1%
193
+ ["--y1% or --z2#", true]
194
+ $ ruby long_names.rb --z2#
195
+ ["--y1% or --z2#", true]
196
+
197
+ A long name may be defined with both positive and negative senses.
198
+
199
+ File +long_with_negation.rb+ defines an option that has both senses.
200
+
201
+ :include: ruby/long_with_negation.rb
202
+
203
+ Executions:
204
+
205
+ $ ruby long_with_negation.rb --help
206
+ Usage: long_with_negation [options]
207
+ --[no-]binary Long name with negation
208
+ $ ruby long_with_negation.rb --binary
209
+ [true, TrueClass]
210
+ $ ruby long_with_negation.rb --no-binary
211
+ [false, FalseClass]
212
+
213
+ ==== Mixing Option Names
214
+
215
+ Many developers like to mix short and long option names,
216
+ so that a short name is in effect an abbreviation of a long name.
217
+
218
+ File +mixed_names.rb+
219
+ defines options that each have both a short and a long name.
220
+
221
+ :include: ruby/mixed_names.rb
222
+
223
+ Executions:
224
+
225
+ $ ruby mixed_names.rb --help
226
+ Usage: mixed_names [options]
227
+ -x, --xxx Short and long, no argument
228
+ -y, --yyyYYY Short and long, required argument
229
+ -z, --zzz [ZZZ] Short and long, optional argument
230
+ $ ruby mixed_names.rb -x
231
+ ["--xxx", true]
232
+ $ ruby mixed_names.rb --xxx
233
+ ["--xxx", true]
234
+ $ ruby mixed_names.rb -y
235
+ mixed_names.rb:12:in `<main>': missing argument: -y (OptionParser::MissingArgument)
236
+ $ ruby mixed_names.rb -y FOO
237
+ ["--yyy", "FOO"]
238
+ $ ruby mixed_names.rb --yyy
239
+ mixed_names.rb:12:in `<main>': missing argument: --yyy (OptionParser::MissingArgument)
240
+ $ ruby mixed_names.rb --yyy BAR
241
+ ["--yyy", "BAR"]
242
+ $ ruby mixed_names.rb -z
243
+ ["--zzz", nil]
244
+ $ ruby mixed_names.rb -z BAZ
245
+ ["--zzz", "BAZ"]
246
+ $ ruby mixed_names.rb --zzz
247
+ ["--zzz", nil]
248
+ $ ruby mixed_names.rb --zzz BAT
249
+ ["--zzz", "BAT"]
250
+
251
+ ==== Option Name Abbreviations
252
+
253
+ By default, abbreviated option names on the command-line are allowed.
254
+ An abbreviated name is valid if it is unique among abbreviated option names.
255
+
256
+ :include: ruby/name_abbrev.rb
257
+
258
+ Executions:
259
+
260
+ $ ruby name_abbrev.rb --help
261
+ Usage: name_abbrev [options]
262
+ -n, --dry-run
263
+ -d, --draft
264
+ $ ruby name_abbrev.rb -n
265
+ ["--dry-run", true]
266
+ $ ruby name_abbrev.rb --dry-run
267
+ ["--dry-run", true]
268
+ $ ruby name_abbrev.rb -d
269
+ ["--draft", true]
270
+ $ ruby name_abbrev.rb --draft
271
+ ["--draft", true]
272
+ $ ruby name_abbrev.rb --d
273
+ name_abbrev.rb:9:in `<main>': ambiguous option: --d (OptionParser::AmbiguousOption)
274
+ $ ruby name_abbrev.rb --dr
275
+ name_abbrev.rb:9:in `<main>': ambiguous option: --dr (OptionParser::AmbiguousOption)
276
+ $ ruby name_abbrev.rb --dry
277
+ ["--dry-run", true]
278
+ $ ruby name_abbrev.rb --dra
279
+ ["--draft", true]
280
+
281
+ You can disable abbreviation using method +require_exact+.
282
+
283
+ :include: ruby/no_abbreviation.rb
284
+
285
+ Executions:
286
+
287
+ $ ruby no_abbreviation.rb --dry-ru
288
+ no_abbreviation.rb:10:in `<main>': invalid option: --dry-ru (OptionParser::InvalidOption)
289
+ $ ruby no_abbreviation.rb --dry-run
290
+ ["--dry-run", true]
291
+
292
+ === Option Arguments
293
+
294
+ An option may take no argument, a required argument, or an optional argument.
295
+
296
+ ==== Option with No Argument
297
+
298
+ All the examples above define options with no argument.
299
+
300
+ ==== Option with Required Argument
301
+
302
+ Specify a required argument for an option by adding a dummy word
303
+ to its name definition.
304
+
305
+ File +required_argument.rb+ defines two options;
306
+ each has a required argument because the name definition has a following dummy word.
307
+
308
+ :include: ruby/required_argument.rb
309
+
310
+ When an option is found, the given argument is yielded.
311
+
312
+ Executions:
313
+
314
+ $ ruby required_argument.rb --help
315
+ Usage: required_argument [options]
316
+ -x, --xxx XXX Required argument via short name
317
+ -y, --y YYY Required argument via long name
318
+ $ ruby required_argument.rb -x AAA
319
+ ["--xxx", "AAA"]
320
+ $ ruby required_argument.rb -y BBB
321
+ ["--yyy", "BBB"]
322
+
323
+ Omitting a required argument raises an error:
324
+
325
+ $ ruby required_argument.rb -x
326
+ required_argument.rb:9:in `<main>': missing argument: -x (OptionParser::MissingArgument)
327
+
328
+ ==== Option with Optional Argument
329
+
330
+ Specify an optional argument for an option by adding a dummy word
331
+ enclosed in square brackets to its name definition.
332
+
333
+ File +optional_argument.rb+ defines two options;
334
+ each has an optional argument because the name definition has a following dummy word
335
+ in square brackets.
336
+
337
+ :include: ruby/optional_argument.rb
338
+
339
+ When an option with an argument is found, the given argument yielded.
340
+
341
+ Executions:
342
+
343
+ $ ruby optional_argument.rb --help
344
+ Usage: optional_argument [options]
345
+ -x, --xxx [XXX] Optional argument via short name
346
+ -y, --yyy [YYY] Optional argument via long name
347
+ $ ruby optional_argument.rb -x AAA
348
+ ["--xxx", "AAA"]
349
+ $ ruby optional_argument.rb -y BBB
350
+ ["--yyy", "BBB"]
351
+
352
+ Omitting an optional argument does not raise an error.
353
+
354
+ === Argument Values
355
+
356
+ Permissible argument values may be restricted
357
+ either by specifying explicit values
358
+ or by providing a pattern that the given value must match.
359
+
360
+ ==== Explicit Argument Values
361
+
362
+ You can specify argument values in either of two ways:
363
+
364
+ - Specify values an array of strings.
365
+ - Specify values a hash.
366
+
367
+ ===== Explicit Values in Array
368
+
369
+ You can specify explicit argument values in an array of strings.
370
+ The argument value must be one of those strings, or an unambiguous abbreviation.
371
+
372
+ File +explicit_array_values.rb+ defines options with explicit argument values.
373
+
374
+ :include: ruby/explicit_array_values.rb
375
+
376
+ Executions:
377
+
378
+ $ ruby explicit_array_values.rb --help
379
+ Usage: explicit_array_values [options]
380
+ -xXXX Values for required argument
381
+ -y [YYY] Values for optional argument
382
+ $ ruby explicit_array_values.rb -x
383
+ explicit_array_values.rb:9:in `<main>': missing argument: -x (OptionParser::MissingArgument)
384
+ $ ruby explicit_array_values.rb -x foo
385
+ ["-x", "foo"]
386
+ $ ruby explicit_array_values.rb -x f
387
+ ["-x", "foo"]
388
+ $ ruby explicit_array_values.rb -x bar
389
+ ["-x", "bar"]
390
+ $ ruby explicit_array_values.rb -y ba
391
+ explicit_array_values.rb:9:in `<main>': ambiguous argument: -y ba (OptionParser::AmbiguousArgument)
392
+ $ ruby explicit_array_values.rb -x baz
393
+ explicit_array_values.rb:9:in `<main>': invalid argument: -x baz (OptionParser::InvalidArgument)
394
+
395
+
396
+ ===== Explicit Values in Hash
397
+
398
+ You can specify explicit argument values in a hash with string keys.
399
+ The value passed must be one of those keys, or an unambiguous abbreviation;
400
+ the value yielded will be the value for that key.
401
+
402
+ File +explicit_hash_values.rb+ defines options with explicit argument values.
403
+
404
+ :include: ruby/explicit_hash_values.rb
405
+
406
+ Executions:
407
+
408
+ $ ruby explicit_hash_values.rb --help
409
+ Usage: explicit_hash_values [options]
410
+ -xXXX Values for required argument
411
+ -y [YYY] Values for optional argument
412
+ $ ruby explicit_hash_values.rb -x
413
+ explicit_hash_values.rb:9:in `<main>': missing argument: -x (OptionParser::MissingArgument)
414
+ $ ruby explicit_hash_values.rb -x foo
415
+ ["-x", 0]
416
+ $ ruby explicit_hash_values.rb -x f
417
+ ["-x", 0]
418
+ $ ruby explicit_hash_values.rb -x bar
419
+ ["-x", 1]
420
+ $ ruby explicit_hash_values.rb -x baz
421
+ explicit_hash_values.rb:9:in `<main>': invalid argument: -x baz (OptionParser::InvalidArgument)
422
+ $ ruby explicit_hash_values.rb -y
423
+ ["-y", nil]
424
+ $ ruby explicit_hash_values.rb -y baz
425
+ ["-y", 2]
426
+ $ ruby explicit_hash_values.rb -y bat
427
+ ["-y", 3]
428
+ $ ruby explicit_hash_values.rb -y ba
429
+ explicit_hash_values.rb:9:in `<main>': ambiguous argument: -y ba (OptionParser::AmbiguousArgument)
430
+ $ ruby explicit_hash_values.rb -y bam
431
+ ["-y", nil]
432
+
433
+ ==== Argument Value Patterns
434
+
435
+ You can restrict permissible argument values
436
+ by specifying a Regexp that the given argument must match.
437
+
438
+ File +matched_values.rb+ defines options with matched argument values.
439
+
440
+ :include: ruby/matched_values.rb
441
+
442
+ Executions:
443
+
444
+ $ ruby matched_values.rb --help
445
+ Usage: matched_values [options]
446
+ --xxx XXX Matched values
447
+ $ ruby matched_values.rb --xxx foo
448
+ ["--xxx", "foo"]
449
+ $ ruby matched_values.rb --xxx FOO
450
+ ["--xxx", "FOO"]
451
+ $ ruby matched_values.rb --xxx bar
452
+ matched_values.rb:6:in `<main>': invalid argument: --xxx bar (OptionParser::InvalidArgument)
453
+
454
+ === Keyword Argument +into+
455
+
456
+ In parsing options, you can add keyword option +into+ with a hash-like argument;
457
+ each parsed option will be added as a name/value pair.
458
+
459
+ This is useful for:
460
+
461
+ - Collecting options.
462
+ - Checking for missing options.
463
+ - Providing default values for options.
464
+
465
+ ==== Collecting Options
466
+
467
+ Use keyword argument +into+ to collect options.
468
+
469
+ :include: ruby/collected_options.rb
470
+
471
+ Executions:
472
+
473
+ $ ruby collected_options.rb --help
474
+ Usage: into [options]
475
+ -x, --xxx Short and long, no argument
476
+ -y, --yyyYYY Short and long, required argument
477
+ -z, --zzz [ZZZ] Short and long, optional argument
478
+ $ ruby collected_options.rb --xxx
479
+ {:xxx=>true}
480
+ $ ruby collected_options.rb --xxx --yyy FOO
481
+ {:xxx=>true, :yyy=>"FOO"}
482
+ $ ruby collected_options.rb --xxx --yyy FOO --zzz Bar
483
+ {:xxx=>true, :yyy=>"FOO", :zzz=>"Bar"}
484
+ $ ruby collected_options.rb --xxx --yyy FOO --yyy BAR
485
+ {:xxx=>true, :yyy=>"BAR"}
486
+
487
+ Note in the last execution that the argument value for option <tt>--yyy</tt>
488
+ was overwritten.
489
+
490
+ ==== Checking for Missing Options
491
+
492
+ Use the collected options to check for missing options.
493
+
494
+ :include: ruby/missing_options.rb
495
+
496
+ Executions:
497
+
498
+ $ ruby missing_options.rb --help
499
+ Usage: missing_options [options]
500
+ -x, --xxx Short and long, no argument
501
+ -y, --yyyYYY Short and long, required argument
502
+ -z, --zzz [ZZZ] Short and long, optional argument
503
+ $ ruby missing_options.rb --yyy FOO
504
+ missing_options.rb:11:in `<main>': Missing required options: [:xxx, :zzz] (RuntimeError)
505
+
506
+ ==== Default Values for Options
507
+
508
+ Initialize the +into+ argument to define default values for options.
509
+
510
+ :include: ruby/default_values.rb
511
+
512
+ Executions:
513
+
514
+ $ ruby default_values.rb --help
515
+ Usage: default_values [options]
516
+ -x, --xxx Short and long, no argument
517
+ -y, --yyyYYY Short and long, required argument
518
+ -z, --zzz [ZZZ] Short and long, optional argument
519
+ $ ruby default_values.rb --yyy FOO
520
+ {:yyy=>"FOO", :zzz=>"BBB"}
521
+
522
+ === Argument Converters
523
+
524
+ An option can specify that its argument is to be converted
525
+ from the default \String to an instance of another class.
526
+ There are a number of built-in converters.
527
+
528
+ Example: File +date.rb+
529
+ defines an option whose argument is to be converted to a \Date object.
530
+ The argument is converted by method Date#parse.
531
+
532
+ :include: ruby/date.rb
533
+
534
+ Executions:
535
+
536
+ $ ruby date.rb --date 2001-02-03
537
+ [#<Date: 2001-02-03 ((2451944j,0s,0n),+0s,2299161j)>, Date]
538
+ $ ruby date.rb --date 20010203
539
+ [#<Date: 2001-02-03 ((2451944j,0s,0n),+0s,2299161j)>, Date]
540
+ $ ruby date.rb --date "3rd Feb 2001"
541
+ [#<Date: 2001-02-03 ((2451944j,0s,0n),+0s,2299161j)>, Date]
542
+
543
+ You can also define custom converters.
544
+ See {Argument Converters}[./argument_converters_rdoc.html]
545
+ for both built-in and custom converters.
546
+
547
+ === Help
548
+
549
+ \OptionParser makes automatically generated help text available.
550
+
551
+ The help text consists of:
552
+
553
+ - A banner, showing the usage.
554
+ - Option short and long names.
555
+ - Option dummy argument names.
556
+ - Option descriptions.
557
+
558
+ Example code:
559
+
560
+ :include: ruby/help.rb
561
+
562
+ The option names and dummy argument names are defined as described above.
563
+
564
+ The option description consists of the strings that are not themselves option names;
565
+ An option can have more than one description string.
566
+ Execution:
567
+
568
+ Usage: help [options]
569
+ -x, --xxx Adipiscing elit. Aenean commodo ligula eget.
570
+ Aenean massa. Cum sociis natoque penatibus
571
+ -y, --yyy YYY Lorem ipsum dolor sit amet, consectetuer.
572
+ -z, --zzz [ZZZ] Et magnis dis parturient montes, nascetur
573
+ ridiculus mus. Donec quam felis, ultricies
574
+ nec, pellentesque eu, pretium quis, sem.
575
+
576
+ The program name is included in the default banner:
577
+ <tt>Usage: #{program_name} [options]</tt>;
578
+ you can change the program name.
579
+
580
+ :include: ruby/help_program_name.rb
581
+
582
+ Execution:
583
+
584
+ $ ruby help_program_name.rb --help
585
+ Usage: help_program_name.rb [options]
586
+
587
+ You can also change the entire banner.
588
+
589
+ :include: ruby/help_banner.rb
590
+
591
+ Execution:
592
+
593
+ $ ruby help_banner.rb --help
594
+ Usage: ruby help_banner.rb
595
+
596
+ By default, the option names are indented 4 spaces
597
+ and the width of the option-names field is 32 spaces.
598
+
599
+ You can change these values, along with the banner,
600
+ by passing parameters to OptionParser.new.
601
+
602
+ :include: ruby/help_format.rb
603
+
604
+ Execution:
605
+
606
+ $ ruby help_format.rb --help
607
+ ruby help_format.rb [options]
608
+ -x, --xxx Adipiscing elit. Aenean commodo ligula eget.
609
+ Aenean massa. Cum sociis natoque penatibus
610
+ -y, --yyy YYY Lorem ipsum dolor sit amet, consectetuer.
611
+ -z, --zzz [ZZZ] Et magnis dis parturient montes, nascetur
612
+ ridiculus mus. Donec quam felis, ultricies
613
+ nec, pellentesque eu, pretium quis, sem.
614
+
615
+ === Top List and Base List
616
+
617
+ An \OptionParser object maintains a stack of \OptionParser::List objects,
618
+ each of which has a collection of zero or more options.
619
+ It is unlikely that you'll need to add or take away from that stack.
620
+
621
+ The stack includes:
622
+
623
+ - The <em>top list</em>, given by \OptionParser#top.
624
+ - The <em>base list</em>, given by \OptionParser#base.
625
+
626
+ When \OptionParser builds its help text, the options in the top list
627
+ precede those in the base list.
628
+
629
+ === Defining Options
630
+
631
+ Option-defining methods allow you to create an option, and also append/prepend it
632
+ to the top list or append it to the base list.
633
+
634
+ Each of these next three methods accepts a sequence of parameter arguments and a block,
635
+ creates an option object using method \Option#make_switch (see below),
636
+ and returns the created option:
637
+
638
+ - \Method \OptionParser#define appends the created option to the top list.
639
+
640
+ - \Method \OptionParser#define_head prepends the created option to the top list.
641
+
642
+ - \Method \OptionParser#define_tail appends the created option to the base list.
643
+
644
+ These next three methods are identical to the three above,
645
+ except for their return values:
646
+
647
+ - \Method \OptionParser#on is identical to method \OptionParser#define,
648
+ except that it returns the parser object +self+.
649
+
650
+ - \Method \OptionParser#on_head is identical to method \OptionParser#define_head,
651
+ except that it returns the parser object +self+.
652
+
653
+ - \Method \OptionParser#on_tail is identical to method \OptionParser#define_tail,
654
+ except that it returns the parser object +self+.
655
+
656
+ Though you may never need to call it directly,
657
+ here's the core method for defining an option:
658
+
659
+ - \Method \OptionParser#make_switch accepts an array of parameters and a block.
660
+ See {Parameters for New Options}[./option_params_rdoc.html].
661
+ This method is unlike others here in that it:
662
+ - Accepts an <em>array of parameters</em>;
663
+ others accept a <em>sequence of parameter arguments</em>.
664
+ - Returns an array containing the created option object,
665
+ option names, and other values;
666
+ others return either the created option object
667
+ or the parser object +self+.
668
+
669
+ === Parsing
670
+
671
+ \OptionParser has six instance methods for parsing.
672
+
673
+ Three have names ending with a "bang" (<tt>!</tt>):
674
+
675
+ - parse!
676
+ - order!
677
+ - permute!
678
+
679
+ Each of these methods:
680
+
681
+ - Accepts an optional array of string arguments +argv+;
682
+ if not given, +argv+ defaults to the value of OptionParser#default_argv,
683
+ whose initial value is ARGV.
684
+ - Accepts an optional keyword argument +into+
685
+ (see {Keyword Argument into}[#label-Keyword+Argument+into]).
686
+ - Returns +argv+, possibly with some elements removed.
687
+
688
+ The three other methods have names _not_ ending with a "bang":
689
+
690
+ - parse
691
+ - order
692
+ - permute
693
+
694
+ Each of these methods:
695
+
696
+ - Accepts an array of string arguments
697
+ _or_ zero or more string arguments.
698
+ - Accepts an optional keyword argument +into+ and its value _into_.
699
+ (see {Keyword Argument into}[#label-Keyword+Argument+into]).
700
+ - Returns +argv+, possibly with some elements removed.
701
+
702
+ ==== \Method parse!
703
+
704
+ \Method parse!:
705
+
706
+ - Accepts an optional array of string arguments +argv+;
707
+ if not given, +argv+ defaults to the value of OptionParser#default_argv,
708
+ whose initial value is ARGV.
709
+ - Accepts an optional keyword argument +into+
710
+ (see {Keyword Argument into}[#label-Keyword+Argument+into]).
711
+ - Returns +argv+, possibly with some elements removed.
712
+
713
+ The method processes the elements in +argv+ beginning at <tt>argv[0]</tt>,
714
+ and ending, by default, at the end.
715
+
716
+ Otherwise processing ends and the method returns when:
717
+
718
+ - The terminator argument <tt>--</tt> is found;
719
+ the terminator argument is removed before the return.
720
+ - Environment variable +POSIXLY_CORRECT+ is defined
721
+ and a non-option argument is found;
722
+ the non-option argument is not removed.
723
+ Note that the _value_ of that variable does not matter,
724
+ as only its existence is checked.
725
+
726
+ File +parse_bang.rb+:
727
+
728
+ :include: ruby/parse_bang.rb
729
+
730
+ Help:
731
+
732
+ $ ruby parse_bang.rb --help
733
+ Usage: parse_bang [options]
734
+ --xxx
735
+ --yyy YYY
736
+ --zzz [ZZZ]
737
+
738
+ Default behavior:
739
+
740
+ $ ruby parse_bang.rb input_file.txt output_file.txt --xxx --yyy FOO --zzz BAR
741
+ ["--xxx", true]
742
+ ["--yyy", "FOO"]
743
+ ["--zzz", "BAR"]
744
+ Returned: ["input_file.txt", "output_file.txt"] (Array)
745
+
746
+ Processing ended by terminator argument:
747
+
748
+ $ ruby parse_bang.rb input_file.txt output_file.txt --xxx --yyy FOO -- --zzz BAR
749
+ ["--xxx", true]
750
+ ["--yyy", "FOO"]
751
+ Returned: ["input_file.txt", "output_file.txt", "--zzz", "BAR"] (Array)
752
+
753
+ Processing ended by non-option found when +POSIXLY_CORRECT+ is defined:
754
+
755
+ $ POSIXLY_CORRECT=true ruby parse_bang.rb --xxx input_file.txt output_file.txt -yyy FOO
756
+ ["--xxx", true]
757
+ Returned: ["input_file.txt", "output_file.txt", "-yyy", "FOO"] (Array)
758
+
759
+ ==== \Method parse
760
+
761
+ \Method parse:
762
+
763
+ - Accepts an array of string arguments
764
+ _or_ zero or more string arguments.
765
+ - Accepts an optional keyword argument +into+ and its value _into_.
766
+ (see {Keyword Argument into}[#label-Keyword+Argument+into]).
767
+ - Returns +argv+, possibly with some elements removed.
768
+
769
+ If given an array +ary+, the method forms array +argv+ as <tt>ary.dup</tt>.
770
+ If given zero or more string arguments, those arguments are formed
771
+ into array +argv+.
772
+
773
+ The method calls
774
+
775
+ parse!(argv, into: into)
776
+
777
+ Note that environment variable +POSIXLY_CORRECT+
778
+ and the terminator argument <tt>--</tt> are honored.
779
+
780
+ File +parse.rb+:
781
+
782
+ :include: ruby/parse.rb
783
+
784
+ Help:
785
+
786
+ $ ruby parse.rb --help
787
+ Usage: parse [options]
788
+ --xxx
789
+ --yyy YYY
790
+ --zzz [ZZZ]
791
+
792
+ Default behavior:
793
+
794
+ $ ruby parse.rb input_file.txt output_file.txt --xxx --yyy FOO --zzz BAR
795
+ ["--xxx", true]
796
+ ["--yyy", "FOO"]
797
+ ["--zzz", "BAR"]
798
+ Returned: ["input_file.txt", "output_file.txt"] (Array)
799
+
800
+ Processing ended by terminator argument:
801
+
802
+ $ ruby parse.rb input_file.txt output_file.txt --xxx --yyy FOO -- --zzz BAR
803
+ ["--xxx", true]
804
+ ["--yyy", "FOO"]
805
+ Returned: ["input_file.txt", "output_file.txt", "--zzz", "BAR"] (Array)
806
+
807
+ Processing ended by non-option found when +POSIXLY_CORRECT+ is defined:
808
+
809
+ $ POSIXLY_CORRECT=true ruby parse.rb --xxx input_file.txt output_file.txt -yyy FOO
810
+ ["--xxx", true]
811
+ Returned: ["input_file.txt", "output_file.txt", "-yyy", "FOO"] (Array)
812
+
813
+ ==== \Method order!
814
+
815
+ Calling method OptionParser#order! gives exactly the same result as
816
+ calling method OptionParser#parse! with environment variable
817
+ +POSIXLY_CORRECT+ defined.
818
+
819
+ ==== \Method order
820
+
821
+ Calling method OptionParser#order gives exactly the same result as
822
+ calling method OptionParser#parse with environment variable
823
+ +POSIXLY_CORRECT+ defined.
824
+
825
+ ==== \Method permute!
826
+
827
+ Calling method OptionParser#permute! gives exactly the same result as
828
+ calling method OptionParser#parse! with environment variable
829
+ +POSIXLY_CORRECT+ _not_ defined.
830
+
831
+ ==== \Method permute
832
+
833
+ Calling method OptionParser#permute gives exactly the same result as
834
+ calling method OptionParser#parse with environment variable
835
+ +POSIXLY_CORRECT+ _not_ defined.