Skip to content

Commit 9c5b3f2

Browse files
More on tutorial (#22)
Adds argument abbreviation in option_params.rdoc. Adds entire Argument Values section to tutorial.rdoc.
1 parent d07cb96 commit 9c5b3f2

File tree

4 files changed

+135
-24
lines changed

4 files changed

+135
-24
lines changed

doc/optparse/option_params.rdoc

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ You can specify argument values in either of two ways:
319319
===== Explicit Values in Array
320320

321321
You can specify explicit argument values in an array of strings.
322-
The argument value must be one of those strings.
322+
The argument value must be one of those strings, or an unambiguous abbreviation.
323323

324324
File +explicit_array_values.rb+ defines options with explicit argument values.
325325

@@ -335,17 +335,21 @@ Executions:
335335
explicit_array_values.rb:9:in `<main>': missing argument: -x (OptionParser::MissingArgument)
336336
$ ruby explicit_array_values.rb -x foo
337337
["-x", "foo"]
338+
$ ruby explicit_array_values.rb -x f
339+
["-x", "foo"]
338340
$ ruby explicit_array_values.rb -x bar
339341
["-x", "bar"]
342+
$ ruby explicit_array_values.rb -y ba
343+
explicit_array_values.rb:9:in `<main>': ambiguous argument: -y ba (OptionParser::AmbiguousArgument)
340344
$ ruby explicit_array_values.rb -x baz
341345
explicit_array_values.rb:9:in `<main>': invalid argument: -x baz (OptionParser::InvalidArgument)
342346

343347

344348
===== Explicit Values in Hash
345349

346350
You can specify explicit argument values in a hash with string keys.
347-
The value passed must be one of those keys,
348-
and the value yielded will be the value for that key.
351+
The value passed must be one of those keys, or an unambiguous abbreviation;
352+
the value yielded will be the value for that key.
349353

350354
File +explicit_hash_values.rb+ defines options with explicit argument values.
351355

@@ -361,6 +365,8 @@ Executions:
361365
explicit_hash_values.rb:9:in `<main>': missing argument: -x (OptionParser::MissingArgument)
362366
$ ruby explicit_hash_values.rb -x foo
363367
["-x", 0]
368+
$ ruby explicit_hash_values.rb -x f
369+
["-x", 0]
364370
$ ruby explicit_hash_values.rb -x bar
365371
["-x", 1]
366372
$ ruby explicit_hash_values.rb -x baz
@@ -371,6 +377,8 @@ Executions:
371377
["-y", 2]
372378
$ ruby explicit_hash_values.rb -y bat
373379
["-y", 3]
380+
$ ruby explicit_hash_values.rb -y ba
381+
explicit_hash_values.rb:9:in `<main>': ambiguous argument: -y ba (OptionParser::AmbiguousArgument)
374382
$ ruby explicit_hash_values.rb -y bam
375383
["-y", nil]
376384

doc/optparse/ruby/help.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,3 @@
1616
'nec, pellentesque eu, pretium quis, sem.',
1717
)
1818
parser.parse!
19-
20-
21-
File renamed without changes.

doc/optparse/tutorial.rdoc

Lines changed: 124 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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

274280
Executions:
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

297303
You can disable abbreviation using method +require_exact+.
@@ -367,6 +373,106 @@ Executions:
367373

368374
Omitting 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

372478
In parsing options, you can add keyword option +into+ with a hash-like argument;

0 commit comments

Comments
 (0)