@@ -40,12 +40,18 @@ The class also has:
4040 - {Short Option Names}[#label-Short+Option+Names]
4141 - {Long Option Names}[#label-Long+Option+Names]
4242 - {Mixing Option Names}[#label-Mixing+Option+Names]
43- - {Command-Line Abbreviations}[#label-Command-Line +Abbreviations]
43+ - {Option Name Abbreviations}[#label-Option+Name +Abbreviations]
4444- {Option Arguments}[#label-Option+Arguments]
4545 - {Option with No Argument}[#label-Option+with+No+Argument]
4646 - {Option with Required Argument}[#label-Option+with+Required+Argument]
4747 - {Option with Optional Argument}[#label-Option+with+Optional+Argument]
48- - {Keyword Argument <tt>into</tt>}[#label-Keyword+Argument+into]
48+ - {Argument Abbreviations}[#label-Argument+Abbreviations]
49+ - {Argument Values}[#label-Argument+Values]
50+ - {Explicit Argument Values}[#label-Explicit+Argument+Values]
51+ - {Explicit Values in Array}[#label-Explicit+Values+in+Array]
52+ - {Explicit Values in Hash}[#label-Explicit+Values+in+Hash]
53+ - {Argument Value Patterns}[#label-Argument+Value+Patterns]
54+ - {Keyword Argument into}[#label-Keyword+Argument+into]
4955 - {Collecting Options}[#label-Collecting+Options]
5056 - {Checking for Missing Options}[#label-Checking+for+Missing+Options]
5157 - {Default Values for Options}[#label-Default+Values+for+Options]
@@ -264,34 +270,34 @@ Executions:
264270 $ ruby mixed_names.rb --zzz BAT
265271 ["--zzz", "BAT"]
266272
267- ==== Command-Line Abbreviations
273+ ==== Option Name Abbreviations
268274
269- By default, abbreviations for command-line option names are allowed.
270- An abbreviated option is valid if it is unique among abbreviated option names.
275+ By default, abbreviated option names on the command-line are allowed.
276+ An abbreviated name is valid if it is unique among abbreviated option names.
271277
272- :include: ruby/abbreviation .rb
278+ :include: ruby/name_abbrev .rb
273279
274280Executions:
275281
276- $ ruby abbreviation .rb --help
277- Usage: abbreviation [options]
282+ $ ruby name_abbrev .rb --help
283+ Usage: name_abbrev [options]
278284 -n, --dry-run
279285 -d, --draft
280- $ ruby abbreviation .rb -n
286+ $ ruby name_abbrev .rb -n
281287 ["--dry-run", true]
282- $ ruby abbreviation .rb --dry-run
288+ $ ruby name_abbrev .rb --dry-run
283289 ["--dry-run", true]
284- $ ruby abbreviation .rb -d
290+ $ ruby name_abbrev .rb -d
285291 ["--draft", true]
286- $ ruby abbreviation .rb --draft
292+ $ ruby name_abbrev .rb --draft
287293 ["--draft", true]
288- $ ruby abbreviation .rb --d
289- abbreviation .rb:9:in `<main>': ambiguous option: --d (OptionParser::AmbiguousOption)
290- $ ruby abbreviation .rb --dr
291- abbreviation .rb:9:in `<main>': ambiguous option: --dr (OptionParser::AmbiguousOption)
292- $ ruby abbreviation .rb --dry
294+ $ ruby name_abbrev .rb --d
295+ name_abbrev .rb:9:in `<main>': ambiguous option: --d (OptionParser::AmbiguousOption)
296+ $ ruby name_abbrev .rb --dr
297+ name_abbrev .rb:9:in `<main>': ambiguous option: --dr (OptionParser::AmbiguousOption)
298+ $ ruby name_abbrev .rb --dry
293299 ["--dry-run", true]
294- $ ruby abbreviation .rb --dra
300+ $ ruby name_abbrev .rb --dra
295301 ["--draft", true]
296302
297303You can disable abbreviation using method +require_exact+.
@@ -367,6 +373,106 @@ Executions:
367373
368374Omitting an optional argument does not raise an error.
369375
376+ === Argument Values
377+
378+ Permissible argument values may be restricted
379+ either by specifying explicit values
380+ or by providing a pattern that the given value must match.
381+
382+ ==== Explicit Argument Values
383+
384+ You can specify argument values in either of two ways:
385+
386+ - Specify values an array of strings.
387+ - Specify values a hash.
388+
389+ ===== Explicit Values in Array
390+
391+ You can specify explicit argument values in an array of strings.
392+ The argument value must be one of those strings, or an unambiguous abbreviation.
393+
394+ File +explicit_array_values.rb+ defines options with explicit argument values.
395+
396+ :include: ruby/explicit_array_values.rb
397+
398+ Executions:
399+
400+ $ ruby explicit_array_values.rb --help
401+ Usage: explicit_array_values [options]
402+ -xXXX Values for required argument
403+ -y [YYY] Values for optional argument
404+ $ ruby explicit_array_values.rb -x
405+ explicit_array_values.rb:9:in `<main>': missing argument: -x (OptionParser::MissingArgument)
406+ $ ruby explicit_array_values.rb -x foo
407+ ["-x", "foo"]
408+ $ ruby explicit_array_values.rb -x f
409+ ["-x", "foo"]
410+ $ ruby explicit_array_values.rb -x bar
411+ ["-x", "bar"]
412+ $ ruby explicit_array_values.rb -y ba
413+ explicit_array_values.rb:9:in `<main>': ambiguous argument: -y ba (OptionParser::AmbiguousArgument)
414+ $ ruby explicit_array_values.rb -x baz
415+ explicit_array_values.rb:9:in `<main>': invalid argument: -x baz (OptionParser::InvalidArgument)
416+
417+
418+ ===== Explicit Values in Hash
419+
420+ You can specify explicit argument values in a hash with string keys.
421+ The value passed must be one of those keys, or an unambiguous abbreviation;
422+ the value yielded will be the value for that key.
423+
424+ File +explicit_hash_values.rb+ defines options with explicit argument values.
425+
426+ :include: ruby/explicit_hash_values.rb
427+
428+ Executions:
429+
430+ $ ruby explicit_hash_values.rb --help
431+ Usage: explicit_hash_values [options]
432+ -xXXX Values for required argument
433+ -y [YYY] Values for optional argument
434+ $ ruby explicit_hash_values.rb -x
435+ explicit_hash_values.rb:9:in `<main>': missing argument: -x (OptionParser::MissingArgument)
436+ $ ruby explicit_hash_values.rb -x foo
437+ ["-x", 0]
438+ $ ruby explicit_hash_values.rb -x f
439+ ["-x", 0]
440+ $ ruby explicit_hash_values.rb -x bar
441+ ["-x", 1]
442+ $ ruby explicit_hash_values.rb -x baz
443+ explicit_hash_values.rb:9:in `<main>': invalid argument: -x baz (OptionParser::InvalidArgument)
444+ $ ruby explicit_hash_values.rb -y
445+ ["-y", nil]
446+ $ ruby explicit_hash_values.rb -y baz
447+ ["-y", 2]
448+ $ ruby explicit_hash_values.rb -y bat
449+ ["-y", 3]
450+ $ ruby explicit_hash_values.rb -y ba
451+ explicit_hash_values.rb:9:in `<main>': ambiguous argument: -y ba (OptionParser::AmbiguousArgument)
452+ $ ruby explicit_hash_values.rb -y bam
453+ ["-y", nil]
454+
455+ ==== Argument Value Patterns
456+
457+ You can restrict permissible argument values
458+ by specifying a Regexp that the given argument must match.
459+
460+ File +matched_values.rb+ defines options with matched argument values.
461+
462+ :include: ruby/matched_values.rb
463+
464+ Executions:
465+
466+ $ ruby matched_values.rb --help
467+ Usage: matched_values [options]
468+ --xxx XXX Matched values
469+ $ ruby matched_values.rb --xxx foo
470+ ["--xxx", "foo"]
471+ $ ruby matched_values.rb --xxx FOO
472+ ["--xxx", "FOO"]
473+ $ ruby matched_values.rb --xxx bar
474+ matched_values.rb:6:in `<main>': invalid argument: --xxx bar (OptionParser::InvalidArgument)
475+
370476=== Keyword Argument +into+
371477
372478In parsing options, you can add keyword option +into+ with a hash-like argument;
0 commit comments