Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 21 additions & 21 deletions doc/optparse/argument_converters.rdoc
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
== Argument Converters
= Argument Converters

An option can specify that its argument is to be converted
from the default +String+ to an instance of another class.

=== Contents
== Contents

- {Built-In Argument Converters}[#label-Built-In+Argument+Converters]
- {Date}[#label-Date]
Expand All @@ -25,12 +25,12 @@ from the default +String+ to an instance of another class.
- {Regexp}[#label-Regexp]
- {Custom Argument Converters}[#label-Custom+Argument+Converters]

=== Built-In Argument Converters
== Built-In Argument Converters

+OptionParser+ has a number of built-in argument converters,
which are demonstrated below.

==== +Date+
=== +Date+

File +date.rb+
defines an option whose argument is to be converted to a +Date+ object.
Expand All @@ -47,7 +47,7 @@ Executions:
$ ruby date.rb --date "3rd Feb 2001"
[#<Date: 2001-02-03 ((2451944j,0s,0n),+0s,2299161j)>, Date]

==== +DateTime+
=== +DateTime+

File +datetime.rb+
defines an option whose argument is to be converted to a +DateTime+ object.
Expand All @@ -64,7 +64,7 @@ Executions:
$ ruby datetime.rb --datetime "3rd Feb 2001 04:05:06 PM"
[#<DateTime: 2001-02-03T16:05:06+00:00 ((2451944j,57906s,0n),+0s,2299161j)>, DateTime]

==== +Time+
=== +Time+

File +time.rb+
defines an option whose argument is to be converted to a +Time+ object.
Expand All @@ -79,7 +79,7 @@ Executions:
$ ruby time.rb --time 2010-10-31
[2010-10-31 00:00:00 -0500, Time]

==== +URI+
=== +URI+

File +uri.rb+
defines an option whose argument is to be converted to a +URI+ object.
Expand All @@ -96,7 +96,7 @@ Executions:
$ ruby uri.rb --uri file://~/var
[#<URI::File file://~/var>, URI::File]

==== +Shellwords+
=== +Shellwords+

File +shellwords.rb+
defines an option whose argument is to be converted to an +Array+ object by method
Expand All @@ -111,7 +111,7 @@ Executions:
$ ruby shellwords.rb --shellwords "here are 'two words'"
[["here", "are", "two words"], Array]

==== +Integer+
=== +Integer+

File +integer.rb+
defines an option whose argument is to be converted to an +Integer+ object.
Expand All @@ -132,7 +132,7 @@ Executions:
$ ruby integer.rb --integer 0b100
[4, Integer]

==== +Float+
=== +Float+

File +float.rb+
defines an option whose argument is to be converted to a +Float+ object.
Expand All @@ -151,7 +151,7 @@ Executions:
$ ruby float.rb --float 1.234E-2
[0.01234, Float]

==== +Numeric+
=== +Numeric+

File +numeric.rb+
defines an option whose argument is to be converted to an instance
Expand All @@ -170,7 +170,7 @@ Executions:
$ ruby numeric.rb --numeric 3
[3, Integer]

==== +DecimalInteger+
=== +DecimalInteger+

File +decimal_integer.rb+
defines an option whose argument is to be converted to an +Integer+ object.
Expand All @@ -192,7 +192,7 @@ Executions:
$ ruby decimal_integer.rb --decimal_integer -0100
[-100, Integer]

==== +OctalInteger+
=== +OctalInteger+

File +octal_integer.rb+
defines an option whose argument is to be converted to an +Integer+ object.
Expand All @@ -212,7 +212,7 @@ Executions:
$ ruby octal_integer.rb --octal_integer 0100
[64, Integer]

==== +DecimalNumeric+
=== +DecimalNumeric+

File +decimal_numeric.rb+
defines an option whose argument is to be converted to an +Integer+ object.
Expand All @@ -232,7 +232,7 @@ Executions:
$ ruby decimal_numeric.rb --decimal_numeric 0100
[64, Integer]

==== +TrueClass+
=== +TrueClass+

File +true_class.rb+
defines an option whose argument is to be converted to +true+ or +false+.
Expand All @@ -259,7 +259,7 @@ Executions:
$ ruby true_class.rb --true_class nil
[false, FalseClass]

==== +FalseClass+
=== +FalseClass+

File +false_class.rb+
defines an option whose argument is to be converted to +true+ or +false+.
Expand All @@ -286,7 +286,7 @@ Executions:
$ ruby false_class.rb --false_class +
[true, TrueClass]

==== +Object+
=== +Object+

File +object.rb+
defines an option whose argument is not to be converted from +String+.
Expand All @@ -300,7 +300,7 @@ Executions:
$ ruby object.rb --object nil
["nil", String]

==== +String+
=== +String+

File +string.rb+
defines an option whose argument is not to be converted from +String+.
Expand All @@ -314,7 +314,7 @@ Executions:
$ ruby string.rb --string nil
["nil", String]

==== +Array+
=== +Array+

File +array.rb+
defines an option whose argument is to be converted from +String+
Expand All @@ -331,7 +331,7 @@ Executions:
$ ruby array.rb --array "foo, bar, baz"
[["foo", " bar", " baz"], Array]

==== +Regexp+
=== +Regexp+

File +regexp.rb+
defines an option whose argument is to be converted to a +Regexp+ object.
Expand All @@ -342,7 +342,7 @@ Executions:

$ ruby regexp.rb --regexp foo

=== Custom Argument Converters
== Custom Argument Converters

You can create custom argument converters.
To create a custom converter, call OptionParser#accept with:
Expand Down
52 changes: 26 additions & 26 deletions doc/optparse/option_params.rdoc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
== Parameters for New Options
= Parameters for New Options

Option-creating methods in +OptionParser+
accept arguments that determine the behavior of a new option:
Expand Down Expand Up @@ -44,16 +44,16 @@ Contents:
- {Handler Procs}[#label-Handler+Procs]
- {Handler Methods}[#label-Handler+Methods]

=== Option Names
== Option Names

There are two kinds of option names:

- Short option name, consisting of a single hyphen and a single character.
- Long option name, consisting of two hyphens and one or more characters.

==== Short Names
=== Short Names

===== Simple Short Names
==== Simple Short Names

File +short_simple.rb+ defines two options:

Expand All @@ -75,7 +75,7 @@ Executions:
["-x", true]
["-1 or -%", true]

===== Short Names with Required Arguments
==== Short Names with Required Arguments

A short name followed (no whitespace) by a dummy word
defines an option that requires an argument.
Expand All @@ -95,7 +95,7 @@ Executions:
$ ruby short_required.rb -x FOO
["-x", "FOO"]

===== Short Names with Optional Arguments
==== Short Names with Optional Arguments

A short name followed (with whitespace) by a dummy word in square brackets
defines an option that allows an optional argument.
Expand All @@ -115,7 +115,7 @@ Executions:
$ ruby short_optional.rb -x FOO
["-x", "FOO"]

===== Short Names from Range
==== Short Names from Range

You can define an option with multiple short names
taken from a range of characters.
Expand All @@ -140,9 +140,9 @@ Executions:
$ ruby short_range.rb -z
["!-~", "z", nil]

==== Long Names
=== Long Names

===== Simple Long Names
==== Simple Long Names

File +long_simple.rb+ defines two options:

Expand All @@ -165,7 +165,7 @@ Executions:
["--xxx", true]
["--y1% or --z2#", true]

===== Long Names with Required Arguments
==== Long Names with Required Arguments

A long name followed (with whitespace) by a dummy word
defines an option that requires an argument.
Expand All @@ -185,7 +185,7 @@ Executions:
$ ruby long_required.rb --xxx FOO
["--xxx", "FOO"]

===== Long Names with Optional Arguments
==== Long Names with Optional Arguments

A long name followed (with whitespace) by a dummy word in square brackets
defines an option that allows an optional argument.
Expand All @@ -205,7 +205,7 @@ Executions:
$ ruby long_optional.rb --xxx FOO
["--xxx", "FOO"]

===== Long Names with Negation
==== Long Names with Negation

A long name may be defined with both positive and negative senses.

Expand All @@ -223,7 +223,7 @@ Executions:
$ ruby long_with_negation.rb --no-binary
[false, FalseClass]

==== Mixed Names
=== Mixed Names

An option may have both short and long names.

Expand Down Expand Up @@ -259,7 +259,7 @@ Usage: mixed_names [options]
$ ruby mixed_names.rb --zzz BAT
["--zzz", "BAT"]

=== Argument Keywords
== Argument Keywords

As seen above, a given option name string may itself
indicate whether the option has no argument, a required argument,
Expand All @@ -283,7 +283,7 @@ Executions:
$ ruby argument_styles.rb --xxx FOO
["--xxx", "FOO"]

=== Argument Strings
== Argument Strings

Still another way to specify a required argument
is to define it in a string separate from the name string.
Expand All @@ -302,20 +302,20 @@ Executions:
$ ruby argument_strings.rb --xxx FOO
["--xxx", "FOO"]

=== Argument Values
== Argument Values

Permissible argument values may be restricted
either by specifying explicit values
or by providing a pattern that the given value must match.

==== Explicit Argument Values
=== Explicit Argument Values

You can specify argument values in either of two ways:

- Specify values an array of strings.
- Specify values a hash.

===== Explicit Values in Array
==== Explicit Values in Array

You can specify explicit argument values in an array of strings.
The argument value must be one of those strings, or an unambiguous abbreviation.
Expand Down Expand Up @@ -344,7 +344,7 @@ Executions:
explicit_array_values.rb:9:in '<main>': invalid argument: -x baz (OptionParser::InvalidArgument)


===== Explicit Values in Hash
==== Explicit Values in Hash

You can specify explicit argument values in a hash with string keys.
The value passed must be one of those keys, or an unambiguous abbreviation;
Expand Down Expand Up @@ -381,7 +381,7 @@ Executions:
$ ruby explicit_hash_values.rb -y bam
["-y", nil]

==== Argument Value Patterns
=== Argument Value Patterns

You can restrict permissible argument values
by specifying a +Regexp+ that the given argument must match,
Expand Down Expand Up @@ -413,7 +413,7 @@ Executions:
$ ruby matched_values.rb --zzz 2
matched_values.rb:12:in '<main>': invalid argument: --zzz 2 (OptionParser::InvalidArgument)

=== Argument Converters
== Argument Converters

An option can specify that its argument is to be converted
from the default +String+ to an instance of another class.
Expand All @@ -423,7 +423,7 @@ You can also define custom converters.

See {Argument Converters}[./argument_converters.rdoc].

=== Descriptions
== Descriptions

A description parameter is any string parameter
that is not recognized as an
Expand Down Expand Up @@ -453,7 +453,7 @@ Executions:
$ ruby descriptions.rb --xxx
["--xxx", true]

=== Option Handlers
== Option Handlers

The handler for an option is an executable that will be called
when the option is encountered. The handler may be:
Expand All @@ -462,7 +462,7 @@ when the option is encountered. The handler may be:
- A proc.
- A method.

==== Handler Blocks
=== Handler Blocks

An option handler may be a block.

Expand All @@ -481,7 +481,7 @@ Executions:
$ ruby block.rb --yyy FOO
["Handler block for -yyy called with value:", "FOO"]

==== Handler Procs
=== Handler Procs

An option handler may be a Proc.

Expand All @@ -500,7 +500,7 @@ Executions:
$ ruby proc.rb --yyy FOO
["Handler proc for -yyy called with value:", "FOO"]

==== Handler Methods
=== Handler Methods

An option handler may be a Method.

Expand Down
Loading