Skip to content

Commit 7f3195b

Browse files
More on tutorial (#23)
- Removed a largish block of repeated text. - Added sections "Top List and Base List" and "Methods for Defining Options" (on, define, etc.). - Linked from class OptionParser doc to the tutorial.
1 parent 7ef3d89 commit 7f3195b

File tree

3 files changed

+81
-13
lines changed

3 files changed

+81
-13
lines changed

doc/optparse/ruby/basic.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@
1212
parser.on('-z', 'Whether to Z') do |value|
1313
p ['z', value]
1414
end
15-
# Parse the command line.
16-
parser.parse!
15+
# Parse the command line and return pared-down ARGV.
16+
p parser.parse!
17+

doc/optparse/tutorial.rdoc

Lines changed: 70 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ With \OptionParser, you can define options so that for each option:
2727
- The argument may be restricted to specified _forms_.
2828
- The argument may be restricted to specified _values_.
2929

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.
3431

3532
=== Contents
3633

@@ -57,6 +54,8 @@ The class also has:
5754
- {Default Values for Options}[#label-Default+Values+for+Options]
5855
- {Argument Converters}[#label-Argument+Converters]
5956
- {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]
6059

6160
=== To Begin With
6261

@@ -83,16 +82,29 @@ From these defined options, the parser automatically builds help text:
8382

8483
When an option is found during parsing,
8584
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.
8691

8792
Executions:
8893

8994
$ ruby basic.rb -x -z
9095
["x", true]
9196
["z", true]
97+
[]
9298
$ ruby basic.rb -z -y -x
9399
["z", true]
94100
["y", true]
95101
["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)
96108

97109
=== Defining Options
98110

@@ -151,11 +163,6 @@ Multiple short names can "share" a hyphen:
151163
["-1 or -%", true]
152164
["-1 or -%", true]
153165

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-
159166
==== Long Option Names
160167

161168
A long option name consists of two hyphens and a one or more characters
@@ -597,3 +604,57 @@ Execution:
597604
-z, --zzz [ZZZ] Et magnis dis parturient montes, nascetur
598605
ridiculus mus. Donec quam felis, ultricies
599606
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+.

lib/optparse.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@
4848
#
4949
# == OptionParser
5050
#
51+
# === New to \OptionParser?
52+
#
53+
# See the {Tutorial}[./doc/optparse/tutorial_rdoc.html].
54+
#
5155
# === Introduction
5256
#
5357
# OptionParser is a class for command-line option analysis. It is much more
@@ -415,8 +419,10 @@
415419
#
416420
# === Further documentation
417421
#
418-
# The above examples should be enough to learn how to use this class. If you
419-
# have any questions, file a ticket at http://bugs.ruby-lang.org.
422+
# The above examples, along with the accompanying
423+
# {Tutorial}[./doc/optparse/tutorial_rdoc.html],
424+
# should be enough to learn how to use this class.
425+
# If you have any questions, file a ticket at http://bugs.ruby-lang.org.
420426
#
421427
class OptionParser
422428
OptionParser::Version = "0.1.1"

0 commit comments

Comments
 (0)