Skip to content

Commit 40d51cc

Browse files
More on tutorial (#24)
- Adds section "Parsing" to tutorial.rdoc. - Removes section "Terminators" from option_params.rdoc. (Terminator '--' is not an option parameter.)
1 parent 7f3195b commit 40d51cc

File tree

4 files changed

+203
-22
lines changed

4 files changed

+203
-22
lines changed

doc/optparse/option_params.rdoc

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ Contents:
4343
- {Handler Blocks}[#label-Handler+Blocks]
4444
- {Handler Procs}[#label-Handler+Procs]
4545
- {Handler Methods}[#label-Handler+Methods]
46-
- {Terminators}[#label-Terminators]
4746

4847
=== Option Names
4948

@@ -508,22 +507,3 @@ Executions:
508507
["Handler method for -xxx called with value:", true]
509508
$ ruby method.rb --yyy FOO
510509
["Handler method for -yyy called with value:", "FOO"]
511-
512-
=== Terminators
513-
514-
And finally, the terminator parameter <tt>--</tt> tells the options parser
515-
to ignore any options farther to the right.
516-
This can be useful if there are options not meant for the current program.
517-
518-
File +terminator.rb+ defines one option <tt>--my_option</tt>.
519-
520-
:include: ruby/terminator.rb
521-
522-
The first execution fails because <tt>--nosuch</tt> is not a defined option;
523-
the second succeeds because <tt>--</tt> causes that option to be ignored:
524-
525-
$ ruby terminator.rb --my_option FOO --other_option BAR
526-
["FOO", String]
527-
terminator.rb:6:in `<main>': invalid option: --other_option (OptionParser::InvalidOption)
528-
$ ruby terminator.rb --my_option FOO -- --other_option BAR
529-
["FOO", String]

doc/optparse/ruby/parse.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
require 'optparse'
2+
parser = OptionParser.new
3+
parser.on('--xxx') do |value|
4+
p ['--xxx', value]
5+
end
6+
parser.on('--yyy YYY') do |value|
7+
p ['--yyy', value]
8+
end
9+
parser.on('--zzz [ZZZ]') do |value|
10+
p ['--zzz', value]
11+
end
12+
ret = parser.parse(ARGV)
13+
puts "Returned: #{ret} (#{ret.class})"

doc/optparse/ruby/parse_bang.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
require 'optparse'
2+
parser = OptionParser.new
3+
parser.on('--xxx') do |value|
4+
p ['--xxx', value]
5+
end
6+
parser.on('--yyy YYY') do |value|
7+
p ['--yyy', value]
8+
end
9+
parser.on('--zzz [ZZZ]') do |value|
10+
p ['--zzz', value]
11+
end
12+
ret = parser.parse!
13+
puts "Returned: #{ret} (#{ret.class})"

doc/optparse/tutorial.rdoc

Lines changed: 177 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,14 @@ The class also has method #help, which displays automatically-generated help tex
5555
- {Argument Converters}[#label-Argument+Converters]
5656
- {Help}[#label-Help]
5757
- {Top List and Base List}[#label-Top+List+and+Base+List]
58-
- {Methods for Defining Options}[#label-Methods+for+Defining+Options]
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]
5966

6067
=== To Begin With
6168

@@ -619,7 +626,7 @@ The stack includes:
619626
When \OptionParser builds its help text, the options in the top list
620627
precede those in the base list.
621628

622-
=== Methods for Defining Options
629+
=== Defining Options
623630

624631
Option-defining methods allow you to create an option, and also append/prepend it
625632
to the top list or append it to the base list.
@@ -658,3 +665,171 @@ here's the core method for defining an option:
658665
option names, and other values;
659666
others return either the created option object
660667
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.

0 commit comments

Comments
 (0)