Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More on tutorial #9

Merged
merged 4 commits into from
Apr 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 0 additions & 9 deletions doc/ruby/long_names.rb

This file was deleted.

9 changes: 0 additions & 9 deletions doc/ruby/mixed_names.rb

This file was deleted.

9 changes: 0 additions & 9 deletions doc/ruby/short_names.rb

This file was deleted.

File renamed without changes.
9 changes: 9 additions & 0 deletions doc/tutorial/long_names.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'optparse'
parser = OptionParser.new
parser.on('--xxx') do |value|
p ['-xxx', value]
end
parser.on('--y1%', '--z2#') do |value|
p ['--y1% or --z2#', value]
end
parser.parse!
9 changes: 9 additions & 0 deletions doc/tutorial/mixed_names.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'optparse'
parser = OptionParser.new
parser.on('-x', '--xxx') do |value|
p ['--xxx', value]
end
parser.on('-y', '--y1%') do |value|
p ['--y1%', value]
end
parser.parse!
9 changes: 9 additions & 0 deletions doc/tutorial/optional_argument.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'optparse'
parser = OptionParser.new
parser.on('-x [XXX]', '--xxx') do |value|
p ['--xxx', value]
end
parser.on('-y', '--yyy [YYY]') do |value|
p ['--yyy', value]
end
parser.parse!
9 changes: 9 additions & 0 deletions doc/tutorial/required_argument.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'optparse'
parser = OptionParser.new
parser.on('-x XXX', '--xxx') do |value|
p ['--xxx', value]
end
parser.on('-y', '--y YYY') do |value|
p ['--yyy', value]
end
parser.parse!
9 changes: 9 additions & 0 deletions doc/tutorial/short_names.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'optparse'
parser = OptionParser.new
parser.on('-x') do |value|
p ['x', value]
end
parser.on('-1', '-%') do |value|
p ['-1 or -%', value]
end
parser.parse!
115 changes: 88 additions & 27 deletions doc/tutorial.rdoc → doc/tutorial/tutorial.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ When a Ruby program executes, it captures its command-line arguments
and options into variable ARGV.
This simple program just prints its \ARGV:

:include: ruby/argv.rb
:include: argv.rb

Execution, with arguments and options:

$ ruby doc/ruby/argv.rb foo --bar --baz bat bam
$ ruby argv.rb foo --bar --baz bat bam
["foo", "--bar", "--baz", "bat", "bam"]

The executing program is responsible for parsing and handling
Expand All @@ -27,8 +27,10 @@ With \OptionParser, you can define options so that for each option:
- The argument may be restricted to specified _forms_.
- The argument may be restricted to specified _values_.

The class also has a method #summarize that returns a string summary
of all defined options.
The class also has:

- Method #summarize: returns a text summary of the options.
- Method #help: displays automatically-generated help text.

=== Defining Options

Expand Down Expand Up @@ -65,23 +67,28 @@ File +short_names.rb+
defines an option with a short name, <tt>-x</tt>,
and an option with two short names (aliases, in effect) <tt>-y</tt> and <tt>-z</tt>.

:include: ruby/short_names.rb
:include: short_names.rb

Executions:

$ ruby doc/ruby/short_names.rb -x
"-x true"
$ ruby doc/ruby/short_names.rb -1
"-1 or -% true"
$ ruby doc/ruby/short_names.rb -%
"-1 or -% true"
$ ruby short_names.rb -x
["x", true]
$ ruby short_names.rb -1
["-1 or -%", true]
$ ruby short_names.rb -%
["-1 or -%", true]

Multiple short names can "share" a hyphen:

$ ruby short_names.rb -x1%
"-x true"
"-1 or -% true"
"-1 or -% true"
["x", true]
["-1 or -%", true]
["-1 or -%", true]

This is a good time to note that giving an undefined option raises an exception:

$ ruby short_names.rb -z
short_names.rb:9:in `<main>': invalid option: -z (OptionParser::InvalidOption)

==== Long Option Names

Expand All @@ -92,16 +99,16 @@ File +long_names.rb+
defines an option with a long name, <tt>--xxx</tt>,
and an option with two long names (aliases, in effect) <tt>--y1%</tt> and <tt>--z2#</tt>.

:include: ruby/long_names.rb
:include: long_names.rb

Executions:

$ ruby long_names.rb --xxx
"--xxx true"
["-xxx", true]
$ ruby long_names.rb --y1%
"--y1% or -z2# true"
["--y1% or --z2#", true]
$ ruby long_names.rb --z2#
"--y1% or -z2# true"
["--y1% or --z2#", true]

==== Mixing Option Names

Expand All @@ -111,15 +118,69 @@ so that a short name is in effect an abbreviation of a long name.
File +mixed_names.rb+
defines options that each have both a short and a long name.

:include: ruby/mixed_names.rb
:include: mixed_names.rb

Executions:

$ ruby mixed_names.rb -x
["--xxx", true]
$ ruby mixed_names.rb --xxx
["--xxx", true]
$ ruby mixed_names.rb -y
["--y1%", true]
$ ruby mixed_names.rb --y1%
["--y1%", true]

=== Option Arguments

An option may take no argument, a required argument, or an optional argument.

==== Option with No Argument

All the examples above define options with no argument.

==== Option with Required Argument

Specify a required argument for an option by adding a dummy word
to its name definition.

File +required_argument.rb+ defines two options;
each has a required argument because the name definition has a following dummy word.

:include: required_argument.rb

When an option is found, the given argument is yielded.

Executions:

$ ruby doc/ruby/mixed_names.rb -x
"--xxx true"
$ ruby doc/ruby/mixed_names.rb --xxx
"--xxx true"
$ ruby doc/ruby/mixed_names.rb -y
"--y1% true"
$ ruby doc/ruby/mixed_names.rb --y1%
"--y1% true"
$ ruby required_argument.rb -x AAA
["--xxx", "AAA"]
$ ruby required_argument.rb -y BBB
["--yyy", "BBB"]

Omitting a required argument raises an error:

$ ruby required_argument.rb -x
required_argument.rb:9:in `<main>': missing argument: -x (OptionParser::MissingArgument)

==== Option with Optional Argument

Specify an optional argument for an option by adding a dummy word
enclosed in square brackets to its name definition.

File +optional_argument.rb+ defines two options;
each has an optional argument because the name definition has a following dummy word
in square brackets.

:include: optional_argument.rb

When an option with an argument is found, the given argument yielded.

Executions:

$ ruby optional_argument.rb -x AAA
["--xxx", "AAA"]
$ ruby optional_argument.rb -y BBB
["--yyy", "BBB"]

Omitting an optional argument does not raise an error.