@@ -27,10 +27,7 @@ With \OptionParser, you can define options so that for each option:
27
27
- The argument may be restricted to specified _forms_.
28
28
- The argument may be restricted to specified _values_.
29
29
30
- The class also has:
31
-
32
- - Method #summarize: returns a text summary of the options.
33
- - Method #help: displays automatically-generated help text.
30
+ The class also has method #help, which displays automatically-generated help text.
34
31
35
32
=== Contents
36
33
@@ -57,6 +54,8 @@ The class also has:
57
54
- {Default Values for Options}[#label-Default+Values+for+Options]
58
55
- {Argument Converters}[#label-Argument+Converters]
59
56
- {Help}[#label-Help]
57
+ - {Top List and Base List}[#label-Top+List+and+Base+List]
58
+ - {Methods for Defining Options}[#label-Methods+for+Defining+Options]
60
59
61
60
=== To Begin With
62
61
@@ -83,16 +82,29 @@ From these defined options, the parser automatically builds help text:
83
82
84
83
When an option is found during parsing,
85
84
the block defined for the option is called with the argument value.
85
+ An invalid option raises an exception.
86
+
87
+ Method #parse!, which is used most often in this tutorial,
88
+ removes from \ARGV the options and arguments it finds,
89
+ leaving other non-option arguments for the program to handle on its own.
90
+ The method returns the possibly-reduced \ARGV array.
86
91
87
92
Executions:
88
93
89
94
$ ruby basic.rb -x -z
90
95
["x", true]
91
96
["z", true]
97
+ []
92
98
$ ruby basic.rb -z -y -x
93
99
["z", true]
94
100
["y", true]
95
101
["x", true]
102
+ []
103
+ $ ruby basic.rb -x input_file.txt output_file.txt
104
+ ["x", true]
105
+ ["input_file.txt", "output_file.txt"]
106
+ $ ruby basic.rb -a
107
+ basic.rb:16:in `<main>': invalid option: -a (OptionParser::InvalidOption)
96
108
97
109
=== Defining Options
98
110
@@ -151,11 +163,6 @@ Multiple short names can "share" a hyphen:
151
163
["-1 or -%", true]
152
164
["-1 or -%", true]
153
165
154
- This is a good time to note that giving an undefined option raises an exception:
155
-
156
- $ ruby short_names.rb -z
157
- short_names.rb:9:in `<main>': invalid option: -z (OptionParser::InvalidOption)
158
-
159
166
==== Long Option Names
160
167
161
168
A long option name consists of two hyphens and a one or more characters
@@ -597,3 +604,57 @@ Execution:
597
604
-z, --zzz [ZZZ] Et magnis dis parturient montes, nascetur
598
605
ridiculus mus. Donec quam felis, ultricies
599
606
nec, pellentesque eu, pretium quis, sem.
607
+
608
+ === Top List and Base List
609
+
610
+ An \OptionParser object maintains a stack of \OptionParser::List objects,
611
+ each of which has a collection of zero or more options.
612
+ It is unlikely that you'll need to add or take away from that stack.
613
+
614
+ The stack includes:
615
+
616
+ - The <em>top list</em>, given by \OptionParser#top.
617
+ - The <em>base list</em>, given by \OptionParser#base.
618
+
619
+ When \OptionParser builds its help text, the options in the top list
620
+ precede those in the base list.
621
+
622
+ === Methods for Defining Options
623
+
624
+ Option-defining methods allow you to create an option, and also append/prepend it
625
+ to the top list or append it to the base list.
626
+
627
+ Each of these next three methods accepts a sequence of parameter arguments and a block,
628
+ creates an option object using method \Option#make_switch (see below),
629
+ and returns the created option:
630
+
631
+ - \Method \OptionParser#define appends the created option to the top list.
632
+
633
+ - \Method \OptionParser#define_head prepends the created option to the top list.
634
+
635
+ - \Method \OptionParser#define_tail appends the created option to the base list.
636
+
637
+ These next three methods are identical to the three above,
638
+ except for their return values:
639
+
640
+ - \Method \OptionParser#on is identical to method \OptionParser#define,
641
+ except that it returns the parser object +self+.
642
+
643
+ - \Method \OptionParser#on_head is identical to method \OptionParser#define_head,
644
+ except that it returns the parser object +self+.
645
+
646
+ - \Method \OptionParser#on_tail is identical to method \OptionParser#define_tail,
647
+ except that it returns the parser object +self+.
648
+
649
+ Though you may never need to call it directly,
650
+ here's the core method for defining an option:
651
+
652
+ - \Method \OptionParser#make_switch accepts an array of parameters and a block.
653
+ See {Parameters for New Options}[./option_params_rdoc.html].
654
+ This method is unlike others here in that it:
655
+ - Accepts an <em>array of parameters</em>;
656
+ others accept a <em>sequence of parameter arguments</em>.
657
+ - Returns an array containing the created option object,
658
+ option names, and other values;
659
+ others return either the created option object
660
+ or the parser object +self+.
0 commit comments