From f89486965b64bc04ed49073fd5ef48390e0026d2 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Sun, 11 Apr 2021 09:03:07 +0900 Subject: [ruby/optparse] Moved rdoc files to doc/optparse https://github.com/ruby/optparse/commit/cccb28e0de --- doc/argument_converters.rdoc | 362 -------------------- doc/creates_option.rdoc | 7 - doc/option_params.rdoc | 521 ----------------------------- doc/optparse/argument_converters.rdoc | 362 ++++++++++++++++++++ doc/optparse/creates_option.rdoc | 7 + doc/optparse/option_params.rdoc | 521 +++++++++++++++++++++++++++++ doc/optparse/ruby/abbreviation.rb | 9 + doc/optparse/ruby/argument_keywords.rb | 6 + doc/optparse/ruby/argument_strings.rb | 6 + doc/optparse/ruby/argv.rb | 2 + doc/optparse/ruby/array.rb | 6 + doc/optparse/ruby/block.rb | 9 + doc/optparse/ruby/collected_options.rb | 8 + doc/optparse/ruby/custom_converter.rb | 9 + doc/optparse/ruby/date.rb | 6 + doc/optparse/ruby/datetime.rb | 6 + doc/optparse/ruby/decimal_integer.rb | 7 + doc/optparse/ruby/decimal_numeric.rb | 7 + doc/optparse/ruby/default_values.rb | 8 + doc/optparse/ruby/descriptions.rb | 15 + doc/optparse/ruby/explicit_array_values.rb | 9 + doc/optparse/ruby/explicit_hash_values.rb | 9 + doc/optparse/ruby/false_class.rb | 6 + doc/optparse/ruby/float.rb | 6 + doc/optparse/ruby/integer.rb | 6 + doc/optparse/ruby/long_names.rb | 9 + doc/optparse/ruby/long_optional.rb | 6 + doc/optparse/ruby/long_required.rb | 6 + doc/optparse/ruby/long_simple.rb | 9 + doc/optparse/ruby/long_with_negation.rb | 6 + doc/optparse/ruby/matched_values.rb | 6 + doc/optparse/ruby/method.rb | 11 + doc/optparse/ruby/missing_options.rb | 12 + doc/optparse/ruby/mixed_names.rb | 12 + doc/optparse/ruby/no_abbreviation.rb | 10 + doc/optparse/ruby/numeric.rb | 6 + doc/optparse/ruby/object.rb | 6 + doc/optparse/ruby/octal_integer.rb | 7 + doc/optparse/ruby/optional_argument.rb | 9 + doc/optparse/ruby/proc.rb | 13 + doc/optparse/ruby/regexp.rb | 6 + doc/optparse/ruby/required_argument.rb | 9 + doc/optparse/ruby/shellwords.rb | 6 + doc/optparse/ruby/short_names.rb | 9 + doc/optparse/ruby/short_optional.rb | 6 + doc/optparse/ruby/short_range.rb | 6 + doc/optparse/ruby/short_required.rb | 6 + doc/optparse/ruby/short_simple.rb | 9 + doc/optparse/ruby/string.rb | 6 + doc/optparse/ruby/terminator.rb | 6 + doc/optparse/ruby/time.rb | 6 + doc/optparse/ruby/true_class.rb | 6 + doc/optparse/ruby/uri.rb | 6 + doc/optparse/tutorial.rdoc | 388 +++++++++++++++++++++ doc/ruby/abbreviation.rb | 9 - doc/ruby/argument_keywords.rb | 6 - doc/ruby/argument_strings.rb | 6 - doc/ruby/argv.rb | 2 - doc/ruby/array.rb | 6 - doc/ruby/block.rb | 9 - doc/ruby/collected_options.rb | 8 - doc/ruby/custom_converter.rb | 9 - doc/ruby/date.rb | 6 - doc/ruby/datetime.rb | 6 - doc/ruby/decimal_integer.rb | 7 - doc/ruby/decimal_numeric.rb | 7 - doc/ruby/default_values.rb | 8 - doc/ruby/descriptions.rb | 15 - doc/ruby/explicit_array_values.rb | 9 - doc/ruby/explicit_hash_values.rb | 9 - doc/ruby/false_class.rb | 6 - doc/ruby/float.rb | 6 - doc/ruby/integer.rb | 6 - doc/ruby/long_names.rb | 9 - doc/ruby/long_optional.rb | 6 - doc/ruby/long_required.rb | 6 - doc/ruby/long_simple.rb | 9 - doc/ruby/long_with_negation.rb | 6 - doc/ruby/matched_values.rb | 6 - doc/ruby/method.rb | 11 - doc/ruby/missing_options.rb | 12 - doc/ruby/mixed_names.rb | 12 - doc/ruby/no_abbreviation.rb | 10 - doc/ruby/numeric.rb | 6 - doc/ruby/object.rb | 6 - doc/ruby/octal_integer.rb | 7 - doc/ruby/optional_argument.rb | 9 - doc/ruby/proc.rb | 13 - doc/ruby/regexp.rb | 6 - doc/ruby/required_argument.rb | 9 - doc/ruby/shellwords.rb | 6 - doc/ruby/short_names.rb | 9 - doc/ruby/short_optional.rb | 6 - doc/ruby/short_range.rb | 6 - doc/ruby/short_required.rb | 6 - doc/ruby/short_simple.rb | 9 - doc/ruby/string.rb | 6 - doc/ruby/terminator.rb | 6 - doc/ruby/time.rb | 6 - doc/ruby/true_class.rb | 6 - doc/ruby/uri.rb | 6 - doc/tutorial.rdoc | 388 --------------------- 102 files changed, 1633 insertions(+), 1633 deletions(-) delete mode 100644 doc/argument_converters.rdoc delete mode 100644 doc/creates_option.rdoc delete mode 100644 doc/option_params.rdoc create mode 100644 doc/optparse/argument_converters.rdoc create mode 100644 doc/optparse/creates_option.rdoc create mode 100644 doc/optparse/option_params.rdoc create mode 100644 doc/optparse/ruby/abbreviation.rb create mode 100644 doc/optparse/ruby/argument_keywords.rb create mode 100644 doc/optparse/ruby/argument_strings.rb create mode 100644 doc/optparse/ruby/argv.rb create mode 100644 doc/optparse/ruby/array.rb create mode 100644 doc/optparse/ruby/block.rb create mode 100644 doc/optparse/ruby/collected_options.rb create mode 100644 doc/optparse/ruby/custom_converter.rb create mode 100644 doc/optparse/ruby/date.rb create mode 100644 doc/optparse/ruby/datetime.rb create mode 100644 doc/optparse/ruby/decimal_integer.rb create mode 100644 doc/optparse/ruby/decimal_numeric.rb create mode 100644 doc/optparse/ruby/default_values.rb create mode 100644 doc/optparse/ruby/descriptions.rb create mode 100644 doc/optparse/ruby/explicit_array_values.rb create mode 100644 doc/optparse/ruby/explicit_hash_values.rb create mode 100644 doc/optparse/ruby/false_class.rb create mode 100644 doc/optparse/ruby/float.rb create mode 100644 doc/optparse/ruby/integer.rb create mode 100644 doc/optparse/ruby/long_names.rb create mode 100644 doc/optparse/ruby/long_optional.rb create mode 100644 doc/optparse/ruby/long_required.rb create mode 100644 doc/optparse/ruby/long_simple.rb create mode 100644 doc/optparse/ruby/long_with_negation.rb create mode 100644 doc/optparse/ruby/matched_values.rb create mode 100644 doc/optparse/ruby/method.rb create mode 100644 doc/optparse/ruby/missing_options.rb create mode 100644 doc/optparse/ruby/mixed_names.rb create mode 100644 doc/optparse/ruby/no_abbreviation.rb create mode 100644 doc/optparse/ruby/numeric.rb create mode 100644 doc/optparse/ruby/object.rb create mode 100644 doc/optparse/ruby/octal_integer.rb create mode 100644 doc/optparse/ruby/optional_argument.rb create mode 100644 doc/optparse/ruby/proc.rb create mode 100644 doc/optparse/ruby/regexp.rb create mode 100644 doc/optparse/ruby/required_argument.rb create mode 100644 doc/optparse/ruby/shellwords.rb create mode 100644 doc/optparse/ruby/short_names.rb create mode 100644 doc/optparse/ruby/short_optional.rb create mode 100644 doc/optparse/ruby/short_range.rb create mode 100644 doc/optparse/ruby/short_required.rb create mode 100644 doc/optparse/ruby/short_simple.rb create mode 100644 doc/optparse/ruby/string.rb create mode 100644 doc/optparse/ruby/terminator.rb create mode 100644 doc/optparse/ruby/time.rb create mode 100644 doc/optparse/ruby/true_class.rb create mode 100644 doc/optparse/ruby/uri.rb create mode 100644 doc/optparse/tutorial.rdoc delete mode 100644 doc/ruby/abbreviation.rb delete mode 100644 doc/ruby/argument_keywords.rb delete mode 100644 doc/ruby/argument_strings.rb delete mode 100644 doc/ruby/argv.rb delete mode 100644 doc/ruby/array.rb delete mode 100644 doc/ruby/block.rb delete mode 100644 doc/ruby/collected_options.rb delete mode 100644 doc/ruby/custom_converter.rb delete mode 100644 doc/ruby/date.rb delete mode 100644 doc/ruby/datetime.rb delete mode 100644 doc/ruby/decimal_integer.rb delete mode 100644 doc/ruby/decimal_numeric.rb delete mode 100644 doc/ruby/default_values.rb delete mode 100644 doc/ruby/descriptions.rb delete mode 100644 doc/ruby/explicit_array_values.rb delete mode 100644 doc/ruby/explicit_hash_values.rb delete mode 100644 doc/ruby/false_class.rb delete mode 100644 doc/ruby/float.rb delete mode 100644 doc/ruby/integer.rb delete mode 100644 doc/ruby/long_names.rb delete mode 100644 doc/ruby/long_optional.rb delete mode 100644 doc/ruby/long_required.rb delete mode 100644 doc/ruby/long_simple.rb delete mode 100644 doc/ruby/long_with_negation.rb delete mode 100644 doc/ruby/matched_values.rb delete mode 100644 doc/ruby/method.rb delete mode 100644 doc/ruby/missing_options.rb delete mode 100644 doc/ruby/mixed_names.rb delete mode 100644 doc/ruby/no_abbreviation.rb delete mode 100644 doc/ruby/numeric.rb delete mode 100644 doc/ruby/object.rb delete mode 100644 doc/ruby/octal_integer.rb delete mode 100644 doc/ruby/optional_argument.rb delete mode 100644 doc/ruby/proc.rb delete mode 100644 doc/ruby/regexp.rb delete mode 100644 doc/ruby/required_argument.rb delete mode 100644 doc/ruby/shellwords.rb delete mode 100644 doc/ruby/short_names.rb delete mode 100644 doc/ruby/short_optional.rb delete mode 100644 doc/ruby/short_range.rb delete mode 100644 doc/ruby/short_required.rb delete mode 100644 doc/ruby/short_simple.rb delete mode 100644 doc/ruby/string.rb delete mode 100644 doc/ruby/terminator.rb delete mode 100644 doc/ruby/time.rb delete mode 100644 doc/ruby/true_class.rb delete mode 100644 doc/ruby/uri.rb delete mode 100644 doc/tutorial.rdoc (limited to 'doc') diff --git a/doc/argument_converters.rdoc b/doc/argument_converters.rdoc deleted file mode 100644 index fc5a0152a3..0000000000 --- a/doc/argument_converters.rdoc +++ /dev/null @@ -1,362 +0,0 @@ -== Argument Converters - -An option can specify that its argument is to be converted -from the default \String to an instance of another class. - -=== Contents - -- {Built-In Argument Converters}[#label-Built-In+Argument+Converters] - - {Date}[#label-Date] - - {DateTime}[#label-DateTime] - - {Time}[#label-Time] - - {URI}[#label-URI] - - {Shellwords}[#label-Shellwords] - - {Integer}[#label-Integer] - - {Float}[#label-Float] - - {Numeric}[#label-Numeric] - - {DecimalInteger}[#label-DecimalInteger] - - {OctalInteger}[#label-OctalInteger] - - {DecimalNumeric}[#label-DecimalNumeric] - - {TrueClass}[#label-TrueClass] - - {FalseClass}[#label-FalseClass] - - {Object}[#label-Object] - - {String}[#label-String] - - {Array}[#label-Array] - - {Regexp}[#label-Regexp] -- {Custom Argument Converters}[#label-Custom+Argument+Converters] - -=== Built-In Argument Converters - -\OptionParser has a number of built-in argument converters, -which are demonstrated below. - -==== \Date - -File +date.rb+ -defines an option whose argument is to be converted to a \Date object. -The argument is converted by method Date#parse. - - :include: ruby/date.rb - -Executions: - - $ ruby date.rb --date 2001-02-03 - [#, Date] - $ ruby date.rb --date 20010203 - [#, Date] - $ ruby date.rb --date "3rd Feb 2001" - [#, Date] - -==== \DateTime - -File +datetime.rb+ -defines an option whose argument is to be converted to a \DateTime object. -The argument is converted by method DateTime#parse. - - :include: ruby/datetime.rb - -Executions: - - $ ruby datetime.rb --datetime 2001-02-03T04:05:06+07:00 - [#, DateTime] - $ ruby datetime.rb --datetime 20010203T040506+0700 - [#, DateTime] - $ ruby datetime.rb --datetime "3rd Feb 2001 04:05:06 PM" - [#, DateTime] - -==== \Time - -File +time.rb+ -defines an option whose argument is to be converted to a \Time object. -The argument is converted by method Time#httpdate or Time#parse. - - :include: ruby/time.rb - -Executions: - - $ ruby time.rb --time "Thu, 06 Oct 2011 02:26:12 GMT" - [2011-10-06 02:26:12 UTC, Time] - $ ruby time.rb --time 2010-10-31 - [2010-10-31 00:00:00 -0500, Time] - -==== \URI - -File +uri.rb+ -defines an option whose argument is to be converted to a \URI object. -The argument is converted by method URI#parse. - - :include: ruby/uri.rb - -Executions: - - $ ruby uri.rb --uri https://github.com - [#, URI::HTTPS] - $ ruby uri.rb --uri http://github.com - [#, URI::HTTP] - $ ruby uri.rb --uri file://~/var - [#, URI::File] - -==== \Shellwords - -File +shellwords.rb+ -defines an option whose argument is to be converted to an \Array object by method -Shellwords#shellwords. - - :include: ruby/shellwords.rb - -Executions: - - $ ruby shellwords.rb --shellwords "ruby my_prog.rb | less" - [["ruby", "my_prog.rb", "|", "less"], Array] - $ ruby shellwords.rb --shellwords "here are 'two words'" - [["here", "are", "two words"], Array] - -==== \Integer - -File +integer.rb+ -defines an option whose argument is to be converted to an \Integer object. -The argument is converted by method Kernel#Integer. - - :include: ruby/integer.rb - -Executions: - - $ ruby integer.rb --integer 100 - [100, Integer] - $ ruby integer.rb --integer -100 - [-100, Integer] - $ ruby integer.rb --integer 0100 - [64, Integer] - $ ruby integer.rb --integer 0x100 - [256, Integer] - $ ruby integer.rb --integer 0b100 - [4, Integer] - -==== \Float - -File +float.rb+ -defines an option whose argument is to be converted to a \Float object. -The argument is converted by method Kernel#Float. - - :include: ruby/float.rb - -Executions: - - $ ruby float.rb --float 1 - [1.0, Float] - $ ruby float.rb --float 3.14159 - [3.14159, Float] - $ ruby float.rb --float 1.234E2 - [123.4, Float] - $ ruby float.rb --float 1.234E-2 - [0.01234, Float] - -==== \Numeric - -File +numeric.rb+ -defines an option whose argument is to be converted to an instance -of \Rational, \Float, or \Integer. -The argument is converted by method Kernel#Rational, -Kernel#Float, or Kernel#Integer. - - :include: ruby/numeric.rb - -Executions: - - $ ruby numeric.rb --numeric 1/3 - [(1/3), Rational] - $ ruby numeric.rb --numeric 3.333E-1 - [0.3333, Float] - $ ruby numeric.rb --numeric 3 - [3, Integer] - -==== \DecimalInteger - -File +decimal_integer.rb+ -defines an option whose argument is to be converted to an \Integer object. -The argument is converted by method Kernel#Integer. - - :include: ruby/decimal_integer.rb - -The argument may not be in a binary or hexadecimal format; -a leading zero is ignored (not parsed as octal). - -Executions: - - $ ruby decimal_integer.rb --decimal_integer 100 - [100, Integer] - $ ruby decimal_integer.rb --decimal_integer -100 - [-100, Integer] - $ ruby decimal_integer.rb --decimal_integer 0100 - [100, Integer] - $ ruby decimal_integer.rb --decimal_integer -0100 - [-100, Integer] - -==== \OctalInteger - -File +octal_integer.rb+ -defines an option whose argument is to be converted to an \Integer object. -The argument is converted by method Kernel#Integer. - - :include: ruby/octal_integer.rb - -The argument may not be in a binary or hexadecimal format; -it is parsed as octal, regardless of whether it has a leading zero. - -Executions: - - $ ruby octal_integer.rb --octal_integer 100 - [64, Integer] - $ ruby octal_integer.rb --octal_integer -100 - [-64, Integer] - $ ruby octal_integer.rb --octal_integer 0100 - [64, Integer] - -==== \DecimalNumeric - -File +decimal_numeric.rb+ -defines an option whose argument is to be converted to an \Integer object. -The argument is converted by method {Kernel#Integer - - :include: ruby/decimal_numeric.rb - -The argument may not be in a binary or hexadecimal format; -a leading zero causes the argument to be parsed as octal. - -Executions: - - $ ruby decimal_numeric.rb --decimal_numeric 100 - [100, Integer] - $ ruby decimal_numeric.rb --decimal_numeric -100 - [-100, Integer] - $ ruby decimal_numeric.rb --decimal_numeric 0100 - [64, Integer] - -==== \TrueClass - -File +true_class.rb+ -defines an option whose argument is to be converted to +true+ or +false+. -The argument is evaluated by method Object#nil?. - - :include: ruby/true_class.rb - -The argument may be any of those shown in the examples below. - -Executions: - - $ ruby true_class.rb --true_class true - [true, TrueClass] - $ ruby true_class.rb --true_class yes - [true, TrueClass] - $ ruby true_class.rb --true_class + - [true, TrueClass] - $ ruby true_class.rb --true_class false - [false, FalseClass] - $ ruby true_class.rb --true_class no - [false, FalseClass] - $ ruby true_class.rb --true_class - - [false, FalseClass] - $ ruby true_class.rb --true_class nil - [false, FalseClass] - -==== \FalseClass - -File +false_class.rb+ -defines an option whose argument is to be converted to +true+ or +false+. -The argument is evaluated by method Object#nil?. - - :include: ruby/false_class.rb - -The argument may be any of those shown in the examples below. - -Executions: - - $ ruby false_class.rb --false_class false - [false, FalseClass] - $ ruby false_class.rb --false_class no - [false, FalseClass] - $ ruby false_class.rb --false_class - - [false, FalseClass] - $ ruby false_class.rb --false_class nil - [false, FalseClass] - $ ruby false_class.rb --false_class true - [true, TrueClass] - $ ruby false_class.rb --false_class yes - [true, TrueClass] - $ ruby false_class.rb --false_class + - [true, TrueClass] - -==== \Object - -File +object.rb+ -defines an option whose argument is not to be converted from \String. - - :include: ruby/object.rb - -Executions: - - $ ruby object.rb --object foo - ["foo", String] - $ ruby object.rb --object nil - ["nil", String] - -==== \String - -File +string.rb+ -defines an option whose argument is not to be converted from \String. - - :include: ruby/string.rb - -Executions: - - $ ruby string.rb --string foo - ["foo", String] - $ ruby string.rb --string nil - ["nil", String] - -==== \Array - -File +array.rb+ -defines an option whose argument is to be converted from \String -to an array of strings, based on comma-separated substrings. - - :include: ruby/array.rb - -Executions: - - $ ruby array.rb --array "" - [[], Array] - $ ruby array.rb --array foo,bar,baz - [["foo", "bar", "baz"], Array] - $ ruby array.rb --array "foo, bar, baz" - [["foo", " bar", " baz"], Array] - -==== \Regexp - -File +regexp.rb+ -defines an option whose argument is to be converted to a \Regexp object. - - :include: ruby/regexp.rb - -Executions: - - $ ruby regexp.rb --regexp foo - -=== Custom Argument Converters - -You can create custom argument converters. -To create a custom converter, call OptionParser#accept with a class argument, -along with a block that converts the argument and returns the converted value. - - :include: ruby/custom_converter.rb - -Executions: - - $ ruby custom_converter.rb --complex 0 - [(0+0i), Complex] - $ ruby custom_converter.rb --complex 1 - [(1+0i), Complex] - $ ruby custom_converter.rb --complex 1+2i - [(1+2i), Complex] - $ ruby custom_converter.rb --complex 0.3-0.5i - [(0.3-0.5i), Complex] diff --git a/doc/creates_option.rdoc b/doc/creates_option.rdoc deleted file mode 100644 index aaeb72d745..0000000000 --- a/doc/creates_option.rdoc +++ /dev/null @@ -1,7 +0,0 @@ -Creates an option from the given parameters +params+. -See {Parameters for New Options}[./option_params_rdoc.html]. - -The block, if given, is the handler for the created option. -When the option is encountered during command-line parsing, -the block is called with the argument given for the option, if any. -See {Option Handlers}[./option_params_rdoc.html#label-Option+Handlers]. diff --git a/doc/option_params.rdoc b/doc/option_params.rdoc deleted file mode 100644 index 0d06b41e7d..0000000000 --- a/doc/option_params.rdoc +++ /dev/null @@ -1,521 +0,0 @@ -== Parameters for New Options - -Option-creating methods in \OptionParser -accept arguments that determine the behavior of a new option: - -- OptionParser#on -- OptionParser#on_head -- OptionParser#on_tail -- OptionParser#define -- OptionParser#define_head -- OptionParser#define_tail -- OptionParser#make_switch - -The code examples on this page use: - -- OptionParser#on, to define options. -- OptionParser#parse!, to parse the command line. -- Built-in option --help, to display defined options. - -Contents: - -- {Option Names}[#label-Option+Names] - - {Short Names}[#label-Short+Names] - - {Simple Short Names}[#label-Simple+Short+Names] - - {Short Names with Required Arguments}[#label-Short+Names+with+Required+Arguments] - - {Short Names with Optional Arguments}[#label-Short+Names+with+Optional+Arguments] - - {Short Names from Range}[#label-Short+Names+from+Range] - - {Long Names}[#label-Long+Names] - - {Simple Long Names}[#label-Simple+Long+Names] - - {Long Names with Required Arguments}[#label-Long+Names+with+Required+Arguments] - - {Long Names with Optional Arguments}[#label-Long+Names+with+Optional+Arguments] - - {Long Names with Negation}[#label-Long+Names+with+Negation] - - {Mixed Names}[#label-Mixed+Names] -- {Argument Styles}[#label-Argument+Styles] -- {Argument Values}[#label-Argument+Values] - - {Explicit Argument Values}[#label-Explicit+Argument+Values] - - {Explicit Values in Array}[#label-Explicit+Values+in+Array] - - {Explicit Values in Hash}[#label-Explicit+Values+in+Hash] - - {Argument Value Patterns}[#label-Argument+Value+Patterns] -- {Argument Converters}[#label-Argument+Converters] -- {Descriptions}[#label-Descriptions] -- {Option Handlers}[#label-Option+Handlers] - - {Handler Blocks}[#label-Handler+Blocks] - - {Handler Procs}[#label-Handler+Procs] - - {Handler Methods}[#label-Handler+Methods] -- {Terminators}[#label-Terminators] - -=== 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 - -===== Simple Short Names - -File +short_simple.rb+ defines two options: - -- One with short name -x. -- The other with two short names, in effect, aliases, -1 and -%. - - :include: ruby/short_simple.rb - -Executions: - - $ ruby short_simple.rb --help - Usage: short_simple [options] - -x One short name - -1, -% Two short names (aliases) - $ ruby short_simple.rb -x - ["-x", true] - $ ruby short_simple.rb -1 -x -% - ["-1 or -%", true] - ["-x", true] - ["-1 or -%", true] - -===== Short Names with Required Arguments - -A short name followed (no whitespace) by a dummy word -defines an option that requires an argument. - -File +short_required.rb+ defines an option -x -that requires an argument. - - :include: ruby/short_required.rb - -Executions: - - $ ruby short_required.rb --help - Usage: short_required [options] - -xXXX Short name with required argument - $ ruby short_required.rb -x - short_required.rb:6:in `
': missing argument: -x (OptionParser::MissingArgument) - $ ruby short_required.rb -x FOO - ["-x", "FOO"] - -===== 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. - -File +short_optional.rb+ defines an option -x -that allows an optional argument. - - :include: ruby/short_optional.rb - -Executions: - - $ ruby short_optional.rb --help - Usage: short_optional [options] - -x [XXX] Short name with optional argument - $ ruby short_optional.rb -x - ["-x", nil] - $ ruby short_optional.rb -x FOO - ["-x", "FOO"] - -===== Short Names from Range - -You can define an option with multiple short names -taken from a range of characters. -The parser yields both the actual character cited and the value. - -File +short_range.rb+ defines an option with short names -for all printable characters from ! to ~: - - :include: ruby/short_range.rb - -Executions: - - $ ruby short_range.rb --help - Usage: short_range [options] - -[!-~] Short names in (very large) range - $ ruby short_range.rb -! - ["!-~", "!", nil] - $ ruby short_range.rb -! - ["!-~", "!", nil] - $ ruby short_range.rb -A - ["!-~", "A", nil] - $ ruby short_range.rb -z - ["!-~", "z", nil] - -==== Long Names - -===== Simple Long Names - -File +long_simple.rb+ defines two options: - -- One with long name -xxx. -- The other with two long names, in effect, aliases, - --y1% and --z2#. - - :include: ruby/long_simple.rb - -Executions: - - $ ruby long_simple.rb --help - Usage: long_simple [options] - --xxx One long name - --y1%, --z2# Two long names (aliases) - $ ruby long_simple.rb --xxx - ["--xxx", true] - $ ruby long_simple.rb --y1% --xxx --z2# - ["--y1% or --z2#", true] - ["--xxx", true] - ["--y1% or --z2#", true] - -===== Long Names with Required Arguments - -A long name followed (with whitespace) by a dummy word -defines an option that requires an argument. - -File +long_required.rb+ defines an option --xxx -that requires an argument. - - :include: ruby/long_required.rb - -Executions: - - $ ruby long_required.rb --help - Usage: long_required [options] - --xxx XXX Long name with required argument - $ ruby long_required.rb --xxx - long_required.rb:6:in `
': missing argument: --xxx (OptionParser::MissingArgument) - $ ruby long_required.rb --xxx FOO - ["--xxx", "FOO"] - -===== 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. - -File +long_optional.rb+ defines an option --xxx -that allows an optional argument. - - :include: ruby/long_optional.rb - -Executions: - - $ ruby long_optional.rb --help - Usage: long_optional [options] - --xxx [XXX] Long name with optional argument - $ ruby long_optional.rb --xxx - ["--xxx", nil] - $ ruby long_optional.rb --xxx FOO - ["--xxx", "FOO"] - -===== Long Names with Negation - -A long name may be defined with both positive and negative senses. - -File +long_with_negation.rb+ defines an option that has both senses. - - :include: ruby/long_with_negation.rb - -Executions: - - $ ruby long_with_negation.rb --help - Usage: long_with_negation [options] - --[no-]binary Long name with negation - $ ruby long_with_negation.rb --binary - [true, TrueClass] - $ ruby long_with_negation.rb --no-binary - [false, FalseClass] - -==== Mixed Names - -An option may have both short and long names. - -File +mixed_names.rb+ defines a mixture of short and long names. - - :include: ruby/mixed_names.rb - -Executions: - - $ ruby mixed_names.rb --help -Usage: mixed_names [options] - -x, --xxx Short and long, no argument - -y, --yyyYYY Short and long, required argument - -z, --zzz [ZZZ] Short and long, optional argument - $ ruby mixed_names.rb -x - ["--xxx", true] - $ ruby mixed_names.rb --xxx - ["--xxx", true] - $ ruby mixed_names.rb -y - mixed_names.rb:12:in `
': missing argument: -y (OptionParser::MissingArgument) - $ ruby mixed_names.rb -y FOO - ["--yyy", "FOO"] - $ ruby mixed_names.rb --yyy - mixed_names.rb:12:in `
': missing argument: --yyy (OptionParser::MissingArgument) - $ ruby mixed_names.rb --yyy BAR - ["--yyy", "BAR"] - $ ruby mixed_names.rb -z - ["--zzz", nil] - $ ruby mixed_names.rb -z BAZ - ["--zzz", "BAZ"] - $ ruby mixed_names.rb --zzz - ["--zzz", nil] - $ ruby mixed_names.rb --zzz BAT - ["--zzz", "BAT"] - -=== Argument Keywords - -As seen above, a given option name string may itself -indicate whether the option has no argument, a required argument, -or an optional argument. - -An alternative is to use a separate symbol keyword, -which is one of :NONE (the default), -:REQUIRED, :OPTIONAL. - -File +argument_keywords.rb+ defines an option with a required argument. - - :include: ruby/argument_keywords.rb - -Executions: - - $ ruby argument_keywords.rb --help - Usage: argument_keywords [options] - -x, --xxx Required argument - $ ruby argument_styles.rb --xxx - argument_styles.rb:6:in `
': missing argument: --xxx (OptionParser::MissingArgument) - $ ruby argument_styles.rb --xxx FOO - ["--xxx", "FOO"] - -=== Argument Strings - -Still another way to specify a required argument -is to define it in a string separate from the name string. - -File +argument_strings.rb+ defines an option with a required argument. - - :include: ruby/argument_strings.rb - -Executions: - - $ ruby argument_strings.rb --help - Usage: argument_strings [options] - -x, --xxx=XXX Required argument - $ ruby argument_strings.rb --xxx - argument_strings.rb:9:in `
': missing argument: --xxx (OptionParser::MissingArgument) - $ ruby argument_strings.rb --xxx FOO - ["--xxx", "FOO"] - -=== 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 - -You can specify argument values in either of two ways: - -- Specify values an array of strings. -- Specify values a hash. - -===== Explicit Values in Array - -You can specify explicit argument values in an array of strings. -The argument value must be one of those strings. - -File +explicit_array_values.rb+ defines options with explicit argument values. - - :include: ruby/explicit_array_values.rb - -Executions: - - $ ruby explicit_array_values.rb --help - Usage: explicit_array_values [options] - -xXXX Values for required argument - -y [YYY] Values for optional argument - $ ruby explicit_array_values.rb -x - explicit_array_values.rb:9:in `
': missing argument: -x (OptionParser::MissingArgument) - $ ruby explicit_array_values.rb -x foo - ["-x", "foo"] - $ ruby explicit_array_values.rb -x bar - ["-x", "bar"] - $ ruby explicit_array_values.rb -x baz - explicit_array_values.rb:9:in `
': invalid argument: -x baz (OptionParser::InvalidArgument) - - -===== 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, -and the value yielded will be the value for that key. - -File +explicit_hash_values.rb+ defines options with explicit argument values. - - :include: ruby/explicit_hash_values.rb - -Executions: - - $ ruby explicit_hash_values.rb --help - Usage: explicit_hash_values [options] - -xXXX Values for required argument - -y [YYY] Values for optional argument - $ ruby explicit_hash_values.rb -x - explicit_hash_values.rb:9:in `
': missing argument: -x (OptionParser::MissingArgument) - $ ruby explicit_hash_values.rb -x foo - ["-x", 0] - $ ruby explicit_hash_values.rb -x bar - ["-x", 1] - $ ruby explicit_hash_values.rb -x baz - explicit_hash_values.rb:9:in `
': invalid argument: -x baz (OptionParser::InvalidArgument) - $ ruby explicit_hash_values.rb -y - ["-y", nil] - $ ruby explicit_hash_values.rb -y baz - ["-y", 2] - $ ruby explicit_hash_values.rb -y bat - ["-y", 3] - $ ruby explicit_hash_values.rb -y bam - ["-y", nil] - -==== Argument Value Patterns - -You can restrict permissible argument values -by specifying a Regexp that the given argument must match. - -File +matched_values.rb+ defines options with matched argument values. - - :include: ruby/matched_values.rb - -Executions: - - $ ruby matched_values.rb --help - Usage: matched_values [options] - --xxx XXX Matched values - $ ruby matched_values.rb --xxx foo - ["--xxx", "foo"] - $ ruby matched_values.rb --xxx FOO - ["--xxx", "FOO"] - $ ruby matched_values.rb --xxx bar - matched_values.rb:6:in `
': invalid argument: --xxx bar (OptionParser::InvalidArgument) - -=== Argument Converters - -An option can specify that its argument is to be converted -from the default \String to an instance of another class. - -There are a number of built-in converters. -You can also define custom converters. - -See {Argument Converters}[./argument_converters_rdoc.html]. - -=== Descriptions - -A description parameter is any string parameter -that is not recognized as an -{option name}[#label-Option+Names] or a -{terminator}[#label-Terminators]; -in other words, it does not begin with a hypnen. - -You may give any number of description parameters; -each becomes a line in the text generated by option --help. - -File +descriptions.rb+ has six strings in its array +descriptions+. -These are all passed as parameters to OptionParser#on, so that they -all, line for line, become the option's description. - - :include: ruby/descriptions.rb - -Executions: - - $ ruby descriptions.rb --help - Usage: descriptions [options] - --xxx Lorem ipsum dolor sit amet, consectetuer - adipiscing elit. Aenean commodo ligula eget. - Aenean massa. Cum sociis natoque penatibus - et magnis dis parturient montes, nascetur - ridiculus mus. Donec quam felis, ultricies - nec, pellentesque eu, pretium quis, sem. - $ ruby descriptions.rb --xxx - ["--xxx", true] - -=== Option Handlers - -The handler for an option is an executable that will be called -when the option is encountered. The handler may be: - -- A block (this is most often seen). -- A proc. -- A method. - -==== Handler Blocks - -An option hadler may be a block. - -File +block.rb+ defines an option that has a handler block. - - :include: ruby/block.rb - -Executions: - - $ ruby block.rb --help - Usage: block [options] - --xxx Option with no argument - --yyy YYY Option with required argument - $ ruby block.rb --xxx - ["Handler block for -xxx called with value:", true] - $ ruby block.rb --yyy FOO - ["Handler block for -yyy called with value:", "FOO"] - -==== Handler Procs - -An option handler may be a Proc. - -File +proc.rb+ defines an option that has a handler proc. - - :include: ruby/proc.rb - -Executions: - - $ ruby proc.rb --help - Usage: proc [options] - --xxx Option with no argument - --yyy YYY Option with required argument - $ ruby proc.rb --xxx - ["Handler proc for -xxx called with value:", true] - $ ruby proc.rb --yyy FOO - ["Handler proc for -yyy called with value:", "FOO"] - -==== Handler Methods - -An option handler may be a Method. - -File +proc.rb+ defines an option that has a handler method. - - :include: ruby/method.rb - -Executions: - - $ ruby method.rb --help - Usage: method [options] - --xxx Option with no argument - --yyy YYY Option with required argument - $ ruby method.rb --xxx - ["Handler method for -xxx called with value:", true] - $ ruby method.rb --yyy FOO - ["Handler method for -yyy called with value:", "FOO"] - -=== Terminators - -And finally, the terminator parameter -- tells the options parser -to ignore any options farther to the right. -This can be useful if there are options not meant for the current program. - -File +terminator.rb+ defines one option --my_option. - - :include: ruby/terminator.rb - -The first execution fails because --nosuch is not a defined option; -the second succeeds because -- causes that option to be ignored: - - $ ruby terminator.rb --my_option FOO --other_option BAR - ["FOO", String] - terminator.rb:6:in `
': invalid option: --other_option (OptionParser::InvalidOption) - $ ruby terminator.rb --my_option FOO -- --other_option BAR - ["FOO", String] diff --git a/doc/optparse/argument_converters.rdoc b/doc/optparse/argument_converters.rdoc new file mode 100644 index 0000000000..fc5a0152a3 --- /dev/null +++ b/doc/optparse/argument_converters.rdoc @@ -0,0 +1,362 @@ +== Argument Converters + +An option can specify that its argument is to be converted +from the default \String to an instance of another class. + +=== Contents + +- {Built-In Argument Converters}[#label-Built-In+Argument+Converters] + - {Date}[#label-Date] + - {DateTime}[#label-DateTime] + - {Time}[#label-Time] + - {URI}[#label-URI] + - {Shellwords}[#label-Shellwords] + - {Integer}[#label-Integer] + - {Float}[#label-Float] + - {Numeric}[#label-Numeric] + - {DecimalInteger}[#label-DecimalInteger] + - {OctalInteger}[#label-OctalInteger] + - {DecimalNumeric}[#label-DecimalNumeric] + - {TrueClass}[#label-TrueClass] + - {FalseClass}[#label-FalseClass] + - {Object}[#label-Object] + - {String}[#label-String] + - {Array}[#label-Array] + - {Regexp}[#label-Regexp] +- {Custom Argument Converters}[#label-Custom+Argument+Converters] + +=== Built-In Argument Converters + +\OptionParser has a number of built-in argument converters, +which are demonstrated below. + +==== \Date + +File +date.rb+ +defines an option whose argument is to be converted to a \Date object. +The argument is converted by method Date#parse. + + :include: ruby/date.rb + +Executions: + + $ ruby date.rb --date 2001-02-03 + [#, Date] + $ ruby date.rb --date 20010203 + [#, Date] + $ ruby date.rb --date "3rd Feb 2001" + [#, Date] + +==== \DateTime + +File +datetime.rb+ +defines an option whose argument is to be converted to a \DateTime object. +The argument is converted by method DateTime#parse. + + :include: ruby/datetime.rb + +Executions: + + $ ruby datetime.rb --datetime 2001-02-03T04:05:06+07:00 + [#, DateTime] + $ ruby datetime.rb --datetime 20010203T040506+0700 + [#, DateTime] + $ ruby datetime.rb --datetime "3rd Feb 2001 04:05:06 PM" + [#, DateTime] + +==== \Time + +File +time.rb+ +defines an option whose argument is to be converted to a \Time object. +The argument is converted by method Time#httpdate or Time#parse. + + :include: ruby/time.rb + +Executions: + + $ ruby time.rb --time "Thu, 06 Oct 2011 02:26:12 GMT" + [2011-10-06 02:26:12 UTC, Time] + $ ruby time.rb --time 2010-10-31 + [2010-10-31 00:00:00 -0500, Time] + +==== \URI + +File +uri.rb+ +defines an option whose argument is to be converted to a \URI object. +The argument is converted by method URI#parse. + + :include: ruby/uri.rb + +Executions: + + $ ruby uri.rb --uri https://github.com + [#, URI::HTTPS] + $ ruby uri.rb --uri http://github.com + [#, URI::HTTP] + $ ruby uri.rb --uri file://~/var + [#, URI::File] + +==== \Shellwords + +File +shellwords.rb+ +defines an option whose argument is to be converted to an \Array object by method +Shellwords#shellwords. + + :include: ruby/shellwords.rb + +Executions: + + $ ruby shellwords.rb --shellwords "ruby my_prog.rb | less" + [["ruby", "my_prog.rb", "|", "less"], Array] + $ ruby shellwords.rb --shellwords "here are 'two words'" + [["here", "are", "two words"], Array] + +==== \Integer + +File +integer.rb+ +defines an option whose argument is to be converted to an \Integer object. +The argument is converted by method Kernel#Integer. + + :include: ruby/integer.rb + +Executions: + + $ ruby integer.rb --integer 100 + [100, Integer] + $ ruby integer.rb --integer -100 + [-100, Integer] + $ ruby integer.rb --integer 0100 + [64, Integer] + $ ruby integer.rb --integer 0x100 + [256, Integer] + $ ruby integer.rb --integer 0b100 + [4, Integer] + +==== \Float + +File +float.rb+ +defines an option whose argument is to be converted to a \Float object. +The argument is converted by method Kernel#Float. + + :include: ruby/float.rb + +Executions: + + $ ruby float.rb --float 1 + [1.0, Float] + $ ruby float.rb --float 3.14159 + [3.14159, Float] + $ ruby float.rb --float 1.234E2 + [123.4, Float] + $ ruby float.rb --float 1.234E-2 + [0.01234, Float] + +==== \Numeric + +File +numeric.rb+ +defines an option whose argument is to be converted to an instance +of \Rational, \Float, or \Integer. +The argument is converted by method Kernel#Rational, +Kernel#Float, or Kernel#Integer. + + :include: ruby/numeric.rb + +Executions: + + $ ruby numeric.rb --numeric 1/3 + [(1/3), Rational] + $ ruby numeric.rb --numeric 3.333E-1 + [0.3333, Float] + $ ruby numeric.rb --numeric 3 + [3, Integer] + +==== \DecimalInteger + +File +decimal_integer.rb+ +defines an option whose argument is to be converted to an \Integer object. +The argument is converted by method Kernel#Integer. + + :include: ruby/decimal_integer.rb + +The argument may not be in a binary or hexadecimal format; +a leading zero is ignored (not parsed as octal). + +Executions: + + $ ruby decimal_integer.rb --decimal_integer 100 + [100, Integer] + $ ruby decimal_integer.rb --decimal_integer -100 + [-100, Integer] + $ ruby decimal_integer.rb --decimal_integer 0100 + [100, Integer] + $ ruby decimal_integer.rb --decimal_integer -0100 + [-100, Integer] + +==== \OctalInteger + +File +octal_integer.rb+ +defines an option whose argument is to be converted to an \Integer object. +The argument is converted by method Kernel#Integer. + + :include: ruby/octal_integer.rb + +The argument may not be in a binary or hexadecimal format; +it is parsed as octal, regardless of whether it has a leading zero. + +Executions: + + $ ruby octal_integer.rb --octal_integer 100 + [64, Integer] + $ ruby octal_integer.rb --octal_integer -100 + [-64, Integer] + $ ruby octal_integer.rb --octal_integer 0100 + [64, Integer] + +==== \DecimalNumeric + +File +decimal_numeric.rb+ +defines an option whose argument is to be converted to an \Integer object. +The argument is converted by method {Kernel#Integer + + :include: ruby/decimal_numeric.rb + +The argument may not be in a binary or hexadecimal format; +a leading zero causes the argument to be parsed as octal. + +Executions: + + $ ruby decimal_numeric.rb --decimal_numeric 100 + [100, Integer] + $ ruby decimal_numeric.rb --decimal_numeric -100 + [-100, Integer] + $ ruby decimal_numeric.rb --decimal_numeric 0100 + [64, Integer] + +==== \TrueClass + +File +true_class.rb+ +defines an option whose argument is to be converted to +true+ or +false+. +The argument is evaluated by method Object#nil?. + + :include: ruby/true_class.rb + +The argument may be any of those shown in the examples below. + +Executions: + + $ ruby true_class.rb --true_class true + [true, TrueClass] + $ ruby true_class.rb --true_class yes + [true, TrueClass] + $ ruby true_class.rb --true_class + + [true, TrueClass] + $ ruby true_class.rb --true_class false + [false, FalseClass] + $ ruby true_class.rb --true_class no + [false, FalseClass] + $ ruby true_class.rb --true_class - + [false, FalseClass] + $ ruby true_class.rb --true_class nil + [false, FalseClass] + +==== \FalseClass + +File +false_class.rb+ +defines an option whose argument is to be converted to +true+ or +false+. +The argument is evaluated by method Object#nil?. + + :include: ruby/false_class.rb + +The argument may be any of those shown in the examples below. + +Executions: + + $ ruby false_class.rb --false_class false + [false, FalseClass] + $ ruby false_class.rb --false_class no + [false, FalseClass] + $ ruby false_class.rb --false_class - + [false, FalseClass] + $ ruby false_class.rb --false_class nil + [false, FalseClass] + $ ruby false_class.rb --false_class true + [true, TrueClass] + $ ruby false_class.rb --false_class yes + [true, TrueClass] + $ ruby false_class.rb --false_class + + [true, TrueClass] + +==== \Object + +File +object.rb+ +defines an option whose argument is not to be converted from \String. + + :include: ruby/object.rb + +Executions: + + $ ruby object.rb --object foo + ["foo", String] + $ ruby object.rb --object nil + ["nil", String] + +==== \String + +File +string.rb+ +defines an option whose argument is not to be converted from \String. + + :include: ruby/string.rb + +Executions: + + $ ruby string.rb --string foo + ["foo", String] + $ ruby string.rb --string nil + ["nil", String] + +==== \Array + +File +array.rb+ +defines an option whose argument is to be converted from \String +to an array of strings, based on comma-separated substrings. + + :include: ruby/array.rb + +Executions: + + $ ruby array.rb --array "" + [[], Array] + $ ruby array.rb --array foo,bar,baz + [["foo", "bar", "baz"], Array] + $ ruby array.rb --array "foo, bar, baz" + [["foo", " bar", " baz"], Array] + +==== \Regexp + +File +regexp.rb+ +defines an option whose argument is to be converted to a \Regexp object. + + :include: ruby/regexp.rb + +Executions: + + $ ruby regexp.rb --regexp foo + +=== Custom Argument Converters + +You can create custom argument converters. +To create a custom converter, call OptionParser#accept with a class argument, +along with a block that converts the argument and returns the converted value. + + :include: ruby/custom_converter.rb + +Executions: + + $ ruby custom_converter.rb --complex 0 + [(0+0i), Complex] + $ ruby custom_converter.rb --complex 1 + [(1+0i), Complex] + $ ruby custom_converter.rb --complex 1+2i + [(1+2i), Complex] + $ ruby custom_converter.rb --complex 0.3-0.5i + [(0.3-0.5i), Complex] diff --git a/doc/optparse/creates_option.rdoc b/doc/optparse/creates_option.rdoc new file mode 100644 index 0000000000..aaeb72d745 --- /dev/null +++ b/doc/optparse/creates_option.rdoc @@ -0,0 +1,7 @@ +Creates an option from the given parameters +params+. +See {Parameters for New Options}[./option_params_rdoc.html]. + +The block, if given, is the handler for the created option. +When the option is encountered during command-line parsing, +the block is called with the argument given for the option, if any. +See {Option Handlers}[./option_params_rdoc.html#label-Option+Handlers]. diff --git a/doc/optparse/option_params.rdoc b/doc/optparse/option_params.rdoc new file mode 100644 index 0000000000..0d06b41e7d --- /dev/null +++ b/doc/optparse/option_params.rdoc @@ -0,0 +1,521 @@ +== Parameters for New Options + +Option-creating methods in \OptionParser +accept arguments that determine the behavior of a new option: + +- OptionParser#on +- OptionParser#on_head +- OptionParser#on_tail +- OptionParser#define +- OptionParser#define_head +- OptionParser#define_tail +- OptionParser#make_switch + +The code examples on this page use: + +- OptionParser#on, to define options. +- OptionParser#parse!, to parse the command line. +- Built-in option --help, to display defined options. + +Contents: + +- {Option Names}[#label-Option+Names] + - {Short Names}[#label-Short+Names] + - {Simple Short Names}[#label-Simple+Short+Names] + - {Short Names with Required Arguments}[#label-Short+Names+with+Required+Arguments] + - {Short Names with Optional Arguments}[#label-Short+Names+with+Optional+Arguments] + - {Short Names from Range}[#label-Short+Names+from+Range] + - {Long Names}[#label-Long+Names] + - {Simple Long Names}[#label-Simple+Long+Names] + - {Long Names with Required Arguments}[#label-Long+Names+with+Required+Arguments] + - {Long Names with Optional Arguments}[#label-Long+Names+with+Optional+Arguments] + - {Long Names with Negation}[#label-Long+Names+with+Negation] + - {Mixed Names}[#label-Mixed+Names] +- {Argument Styles}[#label-Argument+Styles] +- {Argument Values}[#label-Argument+Values] + - {Explicit Argument Values}[#label-Explicit+Argument+Values] + - {Explicit Values in Array}[#label-Explicit+Values+in+Array] + - {Explicit Values in Hash}[#label-Explicit+Values+in+Hash] + - {Argument Value Patterns}[#label-Argument+Value+Patterns] +- {Argument Converters}[#label-Argument+Converters] +- {Descriptions}[#label-Descriptions] +- {Option Handlers}[#label-Option+Handlers] + - {Handler Blocks}[#label-Handler+Blocks] + - {Handler Procs}[#label-Handler+Procs] + - {Handler Methods}[#label-Handler+Methods] +- {Terminators}[#label-Terminators] + +=== 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 + +===== Simple Short Names + +File +short_simple.rb+ defines two options: + +- One with short name -x. +- The other with two short names, in effect, aliases, -1 and -%. + + :include: ruby/short_simple.rb + +Executions: + + $ ruby short_simple.rb --help + Usage: short_simple [options] + -x One short name + -1, -% Two short names (aliases) + $ ruby short_simple.rb -x + ["-x", true] + $ ruby short_simple.rb -1 -x -% + ["-1 or -%", true] + ["-x", true] + ["-1 or -%", true] + +===== Short Names with Required Arguments + +A short name followed (no whitespace) by a dummy word +defines an option that requires an argument. + +File +short_required.rb+ defines an option -x +that requires an argument. + + :include: ruby/short_required.rb + +Executions: + + $ ruby short_required.rb --help + Usage: short_required [options] + -xXXX Short name with required argument + $ ruby short_required.rb -x + short_required.rb:6:in `
': missing argument: -x (OptionParser::MissingArgument) + $ ruby short_required.rb -x FOO + ["-x", "FOO"] + +===== 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. + +File +short_optional.rb+ defines an option -x +that allows an optional argument. + + :include: ruby/short_optional.rb + +Executions: + + $ ruby short_optional.rb --help + Usage: short_optional [options] + -x [XXX] Short name with optional argument + $ ruby short_optional.rb -x + ["-x", nil] + $ ruby short_optional.rb -x FOO + ["-x", "FOO"] + +===== Short Names from Range + +You can define an option with multiple short names +taken from a range of characters. +The parser yields both the actual character cited and the value. + +File +short_range.rb+ defines an option with short names +for all printable characters from ! to ~: + + :include: ruby/short_range.rb + +Executions: + + $ ruby short_range.rb --help + Usage: short_range [options] + -[!-~] Short names in (very large) range + $ ruby short_range.rb -! + ["!-~", "!", nil] + $ ruby short_range.rb -! + ["!-~", "!", nil] + $ ruby short_range.rb -A + ["!-~", "A", nil] + $ ruby short_range.rb -z + ["!-~", "z", nil] + +==== Long Names + +===== Simple Long Names + +File +long_simple.rb+ defines two options: + +- One with long name -xxx. +- The other with two long names, in effect, aliases, + --y1% and --z2#. + + :include: ruby/long_simple.rb + +Executions: + + $ ruby long_simple.rb --help + Usage: long_simple [options] + --xxx One long name + --y1%, --z2# Two long names (aliases) + $ ruby long_simple.rb --xxx + ["--xxx", true] + $ ruby long_simple.rb --y1% --xxx --z2# + ["--y1% or --z2#", true] + ["--xxx", true] + ["--y1% or --z2#", true] + +===== Long Names with Required Arguments + +A long name followed (with whitespace) by a dummy word +defines an option that requires an argument. + +File +long_required.rb+ defines an option --xxx +that requires an argument. + + :include: ruby/long_required.rb + +Executions: + + $ ruby long_required.rb --help + Usage: long_required [options] + --xxx XXX Long name with required argument + $ ruby long_required.rb --xxx + long_required.rb:6:in `
': missing argument: --xxx (OptionParser::MissingArgument) + $ ruby long_required.rb --xxx FOO + ["--xxx", "FOO"] + +===== 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. + +File +long_optional.rb+ defines an option --xxx +that allows an optional argument. + + :include: ruby/long_optional.rb + +Executions: + + $ ruby long_optional.rb --help + Usage: long_optional [options] + --xxx [XXX] Long name with optional argument + $ ruby long_optional.rb --xxx + ["--xxx", nil] + $ ruby long_optional.rb --xxx FOO + ["--xxx", "FOO"] + +===== Long Names with Negation + +A long name may be defined with both positive and negative senses. + +File +long_with_negation.rb+ defines an option that has both senses. + + :include: ruby/long_with_negation.rb + +Executions: + + $ ruby long_with_negation.rb --help + Usage: long_with_negation [options] + --[no-]binary Long name with negation + $ ruby long_with_negation.rb --binary + [true, TrueClass] + $ ruby long_with_negation.rb --no-binary + [false, FalseClass] + +==== Mixed Names + +An option may have both short and long names. + +File +mixed_names.rb+ defines a mixture of short and long names. + + :include: ruby/mixed_names.rb + +Executions: + + $ ruby mixed_names.rb --help +Usage: mixed_names [options] + -x, --xxx Short and long, no argument + -y, --yyyYYY Short and long, required argument + -z, --zzz [ZZZ] Short and long, optional argument + $ ruby mixed_names.rb -x + ["--xxx", true] + $ ruby mixed_names.rb --xxx + ["--xxx", true] + $ ruby mixed_names.rb -y + mixed_names.rb:12:in `
': missing argument: -y (OptionParser::MissingArgument) + $ ruby mixed_names.rb -y FOO + ["--yyy", "FOO"] + $ ruby mixed_names.rb --yyy + mixed_names.rb:12:in `
': missing argument: --yyy (OptionParser::MissingArgument) + $ ruby mixed_names.rb --yyy BAR + ["--yyy", "BAR"] + $ ruby mixed_names.rb -z + ["--zzz", nil] + $ ruby mixed_names.rb -z BAZ + ["--zzz", "BAZ"] + $ ruby mixed_names.rb --zzz + ["--zzz", nil] + $ ruby mixed_names.rb --zzz BAT + ["--zzz", "BAT"] + +=== Argument Keywords + +As seen above, a given option name string may itself +indicate whether the option has no argument, a required argument, +or an optional argument. + +An alternative is to use a separate symbol keyword, +which is one of :NONE (the default), +:REQUIRED, :OPTIONAL. + +File +argument_keywords.rb+ defines an option with a required argument. + + :include: ruby/argument_keywords.rb + +Executions: + + $ ruby argument_keywords.rb --help + Usage: argument_keywords [options] + -x, --xxx Required argument + $ ruby argument_styles.rb --xxx + argument_styles.rb:6:in `
': missing argument: --xxx (OptionParser::MissingArgument) + $ ruby argument_styles.rb --xxx FOO + ["--xxx", "FOO"] + +=== Argument Strings + +Still another way to specify a required argument +is to define it in a string separate from the name string. + +File +argument_strings.rb+ defines an option with a required argument. + + :include: ruby/argument_strings.rb + +Executions: + + $ ruby argument_strings.rb --help + Usage: argument_strings [options] + -x, --xxx=XXX Required argument + $ ruby argument_strings.rb --xxx + argument_strings.rb:9:in `
': missing argument: --xxx (OptionParser::MissingArgument) + $ ruby argument_strings.rb --xxx FOO + ["--xxx", "FOO"] + +=== 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 + +You can specify argument values in either of two ways: + +- Specify values an array of strings. +- Specify values a hash. + +===== Explicit Values in Array + +You can specify explicit argument values in an array of strings. +The argument value must be one of those strings. + +File +explicit_array_values.rb+ defines options with explicit argument values. + + :include: ruby/explicit_array_values.rb + +Executions: + + $ ruby explicit_array_values.rb --help + Usage: explicit_array_values [options] + -xXXX Values for required argument + -y [YYY] Values for optional argument + $ ruby explicit_array_values.rb -x + explicit_array_values.rb:9:in `
': missing argument: -x (OptionParser::MissingArgument) + $ ruby explicit_array_values.rb -x foo + ["-x", "foo"] + $ ruby explicit_array_values.rb -x bar + ["-x", "bar"] + $ ruby explicit_array_values.rb -x baz + explicit_array_values.rb:9:in `
': invalid argument: -x baz (OptionParser::InvalidArgument) + + +===== 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, +and the value yielded will be the value for that key. + +File +explicit_hash_values.rb+ defines options with explicit argument values. + + :include: ruby/explicit_hash_values.rb + +Executions: + + $ ruby explicit_hash_values.rb --help + Usage: explicit_hash_values [options] + -xXXX Values for required argument + -y [YYY] Values for optional argument + $ ruby explicit_hash_values.rb -x + explicit_hash_values.rb:9:in `
': missing argument: -x (OptionParser::MissingArgument) + $ ruby explicit_hash_values.rb -x foo + ["-x", 0] + $ ruby explicit_hash_values.rb -x bar + ["-x", 1] + $ ruby explicit_hash_values.rb -x baz + explicit_hash_values.rb:9:in `
': invalid argument: -x baz (OptionParser::InvalidArgument) + $ ruby explicit_hash_values.rb -y + ["-y", nil] + $ ruby explicit_hash_values.rb -y baz + ["-y", 2] + $ ruby explicit_hash_values.rb -y bat + ["-y", 3] + $ ruby explicit_hash_values.rb -y bam + ["-y", nil] + +==== Argument Value Patterns + +You can restrict permissible argument values +by specifying a Regexp that the given argument must match. + +File +matched_values.rb+ defines options with matched argument values. + + :include: ruby/matched_values.rb + +Executions: + + $ ruby matched_values.rb --help + Usage: matched_values [options] + --xxx XXX Matched values + $ ruby matched_values.rb --xxx foo + ["--xxx", "foo"] + $ ruby matched_values.rb --xxx FOO + ["--xxx", "FOO"] + $ ruby matched_values.rb --xxx bar + matched_values.rb:6:in `
': invalid argument: --xxx bar (OptionParser::InvalidArgument) + +=== Argument Converters + +An option can specify that its argument is to be converted +from the default \String to an instance of another class. + +There are a number of built-in converters. +You can also define custom converters. + +See {Argument Converters}[./argument_converters_rdoc.html]. + +=== Descriptions + +A description parameter is any string parameter +that is not recognized as an +{option name}[#label-Option+Names] or a +{terminator}[#label-Terminators]; +in other words, it does not begin with a hypnen. + +You may give any number of description parameters; +each becomes a line in the text generated by option --help. + +File +descriptions.rb+ has six strings in its array +descriptions+. +These are all passed as parameters to OptionParser#on, so that they +all, line for line, become the option's description. + + :include: ruby/descriptions.rb + +Executions: + + $ ruby descriptions.rb --help + Usage: descriptions [options] + --xxx Lorem ipsum dolor sit amet, consectetuer + adipiscing elit. Aenean commodo ligula eget. + Aenean massa. Cum sociis natoque penatibus + et magnis dis parturient montes, nascetur + ridiculus mus. Donec quam felis, ultricies + nec, pellentesque eu, pretium quis, sem. + $ ruby descriptions.rb --xxx + ["--xxx", true] + +=== Option Handlers + +The handler for an option is an executable that will be called +when the option is encountered. The handler may be: + +- A block (this is most often seen). +- A proc. +- A method. + +==== Handler Blocks + +An option hadler may be a block. + +File +block.rb+ defines an option that has a handler block. + + :include: ruby/block.rb + +Executions: + + $ ruby block.rb --help + Usage: block [options] + --xxx Option with no argument + --yyy YYY Option with required argument + $ ruby block.rb --xxx + ["Handler block for -xxx called with value:", true] + $ ruby block.rb --yyy FOO + ["Handler block for -yyy called with value:", "FOO"] + +==== Handler Procs + +An option handler may be a Proc. + +File +proc.rb+ defines an option that has a handler proc. + + :include: ruby/proc.rb + +Executions: + + $ ruby proc.rb --help + Usage: proc [options] + --xxx Option with no argument + --yyy YYY Option with required argument + $ ruby proc.rb --xxx + ["Handler proc for -xxx called with value:", true] + $ ruby proc.rb --yyy FOO + ["Handler proc for -yyy called with value:", "FOO"] + +==== Handler Methods + +An option handler may be a Method. + +File +proc.rb+ defines an option that has a handler method. + + :include: ruby/method.rb + +Executions: + + $ ruby method.rb --help + Usage: method [options] + --xxx Option with no argument + --yyy YYY Option with required argument + $ ruby method.rb --xxx + ["Handler method for -xxx called with value:", true] + $ ruby method.rb --yyy FOO + ["Handler method for -yyy called with value:", "FOO"] + +=== Terminators + +And finally, the terminator parameter -- tells the options parser +to ignore any options farther to the right. +This can be useful if there are options not meant for the current program. + +File +terminator.rb+ defines one option --my_option. + + :include: ruby/terminator.rb + +The first execution fails because --nosuch is not a defined option; +the second succeeds because -- causes that option to be ignored: + + $ ruby terminator.rb --my_option FOO --other_option BAR + ["FOO", String] + terminator.rb:6:in `
': invalid option: --other_option (OptionParser::InvalidOption) + $ ruby terminator.rb --my_option FOO -- --other_option BAR + ["FOO", String] diff --git a/doc/optparse/ruby/abbreviation.rb b/doc/optparse/ruby/abbreviation.rb new file mode 100644 index 0000000000..b438c1b3dd --- /dev/null +++ b/doc/optparse/ruby/abbreviation.rb @@ -0,0 +1,9 @@ +require 'optparse' +parser = OptionParser.new +parser.on('-n', '--dry-run',) do |value| + p ['--dry-run', value] +end +parser.on('-d', '--draft',) do |value| + p ['--draft', value] +end +parser.parse! diff --git a/doc/optparse/ruby/argument_keywords.rb b/doc/optparse/ruby/argument_keywords.rb new file mode 100644 index 0000000000..8533257c67 --- /dev/null +++ b/doc/optparse/ruby/argument_keywords.rb @@ -0,0 +1,6 @@ +require 'optparse' +parser = OptionParser.new +parser.on('-x', '--xxx', :REQUIRED, 'Required argument') do |value| + p ['--xxx', value] +end +parser.parse! diff --git a/doc/optparse/ruby/argument_strings.rb b/doc/optparse/ruby/argument_strings.rb new file mode 100644 index 0000000000..77861dda30 --- /dev/null +++ b/doc/optparse/ruby/argument_strings.rb @@ -0,0 +1,6 @@ +require 'optparse' +parser = OptionParser.new +parser.on('-x', '--xxx', '=XXX', 'Required argument') do |value| + p ['--xxx', value] +end +parser.parse! diff --git a/doc/optparse/ruby/argv.rb b/doc/optparse/ruby/argv.rb new file mode 100644 index 0000000000..12495cfa1f --- /dev/null +++ b/doc/optparse/ruby/argv.rb @@ -0,0 +1,2 @@ +p ARGV + diff --git a/doc/optparse/ruby/array.rb b/doc/optparse/ruby/array.rb new file mode 100644 index 0000000000..7c6c14fad4 --- /dev/null +++ b/doc/optparse/ruby/array.rb @@ -0,0 +1,6 @@ +require 'optparse' +parser = OptionParser.new +parser.on('--array=ARRAY', Array) do |value| + p [value, value.class] +end +parser.parse! diff --git a/doc/optparse/ruby/block.rb b/doc/optparse/ruby/block.rb new file mode 100644 index 0000000000..c4dfdeb31e --- /dev/null +++ b/doc/optparse/ruby/block.rb @@ -0,0 +1,9 @@ +require 'optparse' +parser = OptionParser.new +parser.on('--xxx', 'Option with no argument') do |value| + p ['Handler block for -xxx called with value:', value] +end +parser.on('--yyy YYY', 'Option with required argument') do |value| + p ['Handler block for -yyy called with value:', value] +end +parser.parse! diff --git a/doc/optparse/ruby/collected_options.rb b/doc/optparse/ruby/collected_options.rb new file mode 100644 index 0000000000..2115e03a9a --- /dev/null +++ b/doc/optparse/ruby/collected_options.rb @@ -0,0 +1,8 @@ +require 'optparse' +parser = OptionParser.new +parser.on('-x', '--xxx', 'Short and long, no argument') +parser.on('-yYYY', '--yyy', 'Short and long, required argument') +parser.on('-z [ZZZ]', '--zzz', 'Short and long, optional argument') +options = {} +parser.parse!(into: options) +p options diff --git a/doc/optparse/ruby/custom_converter.rb b/doc/optparse/ruby/custom_converter.rb new file mode 100644 index 0000000000..029da08c46 --- /dev/null +++ b/doc/optparse/ruby/custom_converter.rb @@ -0,0 +1,9 @@ +require 'optparse/date' +parser = OptionParser.new +parser.accept(Complex) do |value| + value.to_c +end +parser.on('--complex COMPLEX', Complex) do |value| + p [value, value.class] +end +parser.parse! diff --git a/doc/optparse/ruby/date.rb b/doc/optparse/ruby/date.rb new file mode 100644 index 0000000000..5994ad6a85 --- /dev/null +++ b/doc/optparse/ruby/date.rb @@ -0,0 +1,6 @@ +require 'optparse/date' +parser = OptionParser.new +parser.on('--date=DATE', Date) do |value| + p [value, value.class] +end +parser.parse! diff --git a/doc/optparse/ruby/datetime.rb b/doc/optparse/ruby/datetime.rb new file mode 100644 index 0000000000..b9b591d5f6 --- /dev/null +++ b/doc/optparse/ruby/datetime.rb @@ -0,0 +1,6 @@ +require 'optparse/date' +parser = OptionParser.new +parser.on('--datetime=DATETIME', DateTime) do |value| + p [value, value.class] +end +parser.parse! diff --git a/doc/optparse/ruby/decimal_integer.rb b/doc/optparse/ruby/decimal_integer.rb new file mode 100644 index 0000000000..360bd284f8 --- /dev/null +++ b/doc/optparse/ruby/decimal_integer.rb @@ -0,0 +1,7 @@ +require 'optparse' +include OptionParser::Acceptables +parser = OptionParser.new +parser.on('--decimal_integer=DECIMAL_INTEGER', DecimalInteger) do |value| + p [value, value.class] +end +parser.parse! diff --git a/doc/optparse/ruby/decimal_numeric.rb b/doc/optparse/ruby/decimal_numeric.rb new file mode 100644 index 0000000000..954da13561 --- /dev/null +++ b/doc/optparse/ruby/decimal_numeric.rb @@ -0,0 +1,7 @@ +require 'optparse' +include OptionParser::Acceptables +parser = OptionParser.new +parser.on('--decimal_numeric=DECIMAL_NUMERIC', DecimalNumeric) do |value| + p [value, value.class] +end +parser.parse! diff --git a/doc/optparse/ruby/default_values.rb b/doc/optparse/ruby/default_values.rb new file mode 100644 index 0000000000..24c26faea2 --- /dev/null +++ b/doc/optparse/ruby/default_values.rb @@ -0,0 +1,8 @@ +require 'optparse' +parser = OptionParser.new +parser.on('-x', '--xxx', 'Short and long, no argument') +parser.on('-yYYY', '--yyy', 'Short and long, required argument') +parser.on('-z [ZZZ]', '--zzz', 'Short and long, optional argument') +options = {yyy: 'AAA', zzz: 'BBB'} +parser.parse!(into: options) +p options diff --git a/doc/optparse/ruby/descriptions.rb b/doc/optparse/ruby/descriptions.rb new file mode 100644 index 0000000000..9aec80aae2 --- /dev/null +++ b/doc/optparse/ruby/descriptions.rb @@ -0,0 +1,15 @@ +require 'optparse' +parser = OptionParser.new +description = <<-EOT +Lorem ipsum dolor sit amet, consectetuer +adipiscing elit. Aenean commodo ligula eget. +Aenean massa. Cum sociis natoque penatibus +et magnis dis parturient montes, nascetur +ridiculus mus. Donec quam felis, ultricies +nec, pellentesque eu, pretium quis, sem. +EOT +descriptions = description.split($/) +parser.on('--xxx', *descriptions) do |value| + p ['--xxx', value] +end +parser.parse! diff --git a/doc/optparse/ruby/explicit_array_values.rb b/doc/optparse/ruby/explicit_array_values.rb new file mode 100644 index 0000000000..64f930a4bc --- /dev/null +++ b/doc/optparse/ruby/explicit_array_values.rb @@ -0,0 +1,9 @@ +require 'optparse' +parser = OptionParser.new +parser.on('-xXXX', ['foo', 'bar'], 'Values for required argument' ) do |value| + p ['-x', value] +end +parser.on('-y [YYY]', ['baz', 'bat'], 'Values for optional argument') do |value| + p ['-y', value] +end +parser.parse! diff --git a/doc/optparse/ruby/explicit_hash_values.rb b/doc/optparse/ruby/explicit_hash_values.rb new file mode 100644 index 0000000000..9c9e6a48ed --- /dev/null +++ b/doc/optparse/ruby/explicit_hash_values.rb @@ -0,0 +1,9 @@ +require 'optparse' +parser = OptionParser.new +parser.on('-xXXX', {foo: 0, bar: 1}, 'Values for required argument' ) do |value| + p ['-x', value] +end +parser.on('-y [YYY]', {baz: 2, bat: 3}, 'Values for optional argument') do |value| + p ['-y', value] +end +parser.parse! diff --git a/doc/optparse/ruby/false_class.rb b/doc/optparse/ruby/false_class.rb new file mode 100644 index 0000000000..04fe335ede --- /dev/null +++ b/doc/optparse/ruby/false_class.rb @@ -0,0 +1,6 @@ +require 'optparse' +parser = OptionParser.new +parser.on('--false_class=FALSE_CLASS', FalseClass) do |value| + p [value, value.class] +end +parser.parse! diff --git a/doc/optparse/ruby/float.rb b/doc/optparse/ruby/float.rb new file mode 100644 index 0000000000..390df7f7bd --- /dev/null +++ b/doc/optparse/ruby/float.rb @@ -0,0 +1,6 @@ +require 'optparse' +parser = OptionParser.new +parser.on('--float=FLOAT', Float) do |value| + p [value, value.class] +end +parser.parse! diff --git a/doc/optparse/ruby/integer.rb b/doc/optparse/ruby/integer.rb new file mode 100644 index 0000000000..f10656ff1a --- /dev/null +++ b/doc/optparse/ruby/integer.rb @@ -0,0 +1,6 @@ +require 'optparse' +parser = OptionParser.new +parser.on('--integer=INTEGER', Integer) do |value| + p [value, value.class] +end +parser.parse! diff --git a/doc/optparse/ruby/long_names.rb b/doc/optparse/ruby/long_names.rb new file mode 100644 index 0000000000..a49dbda69f --- /dev/null +++ b/doc/optparse/ruby/long_names.rb @@ -0,0 +1,9 @@ +require 'optparse' +parser = OptionParser.new +parser.on('--xxx', 'Long name') do |value| + p ['-xxx', value] +end +parser.on('--y1%', '--z2#', "Two long names") do |value| + p ['--y1% or --z2#', value] +end +parser.parse! diff --git a/doc/optparse/ruby/long_optional.rb b/doc/optparse/ruby/long_optional.rb new file mode 100644 index 0000000000..38dd82166b --- /dev/null +++ b/doc/optparse/ruby/long_optional.rb @@ -0,0 +1,6 @@ +require 'optparse' +parser = OptionParser.new +parser.on('--xxx [XXX]', 'Long name with optional argument') do |value| + p ['--xxx', value] +end +parser.parse! diff --git a/doc/optparse/ruby/long_required.rb b/doc/optparse/ruby/long_required.rb new file mode 100644 index 0000000000..b76c997339 --- /dev/null +++ b/doc/optparse/ruby/long_required.rb @@ -0,0 +1,6 @@ +require 'optparse' +parser = OptionParser.new +parser.on('--xxx XXX', 'Long name with required argument') do |value| + p ['--xxx', value] +end +parser.parse! diff --git a/doc/optparse/ruby/long_simple.rb b/doc/optparse/ruby/long_simple.rb new file mode 100644 index 0000000000..4e489c43ed --- /dev/null +++ b/doc/optparse/ruby/long_simple.rb @@ -0,0 +1,9 @@ +require 'optparse' +parser = OptionParser.new +parser.on('--xxx', 'One long name') do |value| + p ['--xxx', value] +end +parser.on('--y1%', '--z2#', 'Two long names (aliases)') do |value| + p ['--y1% or --z2#', value] +end +parser.parse! diff --git a/doc/optparse/ruby/long_with_negation.rb b/doc/optparse/ruby/long_with_negation.rb new file mode 100644 index 0000000000..3f2913c361 --- /dev/null +++ b/doc/optparse/ruby/long_with_negation.rb @@ -0,0 +1,6 @@ +require 'optparse' +parser = OptionParser.new +parser.on('--[no-]binary', 'Long name with negation') do |value| + p [value, value.class] +end +parser.parse! diff --git a/doc/optparse/ruby/matched_values.rb b/doc/optparse/ruby/matched_values.rb new file mode 100644 index 0000000000..f184ca8474 --- /dev/null +++ b/doc/optparse/ruby/matched_values.rb @@ -0,0 +1,6 @@ +require 'optparse' +parser = OptionParser.new +parser.on('--xxx XXX', /foo/i, 'Matched values') do |value| + p ['--xxx', value] +end +parser.parse! diff --git a/doc/optparse/ruby/method.rb b/doc/optparse/ruby/method.rb new file mode 100644 index 0000000000..3f02ff5798 --- /dev/null +++ b/doc/optparse/ruby/method.rb @@ -0,0 +1,11 @@ +require 'optparse' +parser = OptionParser.new +def xxx_handler(value) + p ['Handler method for -xxx called with value:', value] +end +parser.on('--xxx', 'Option with no argument', method(:xxx_handler)) +def yyy_handler(value) + p ['Handler method for -yyy called with value:', value] +end +parser.on('--yyy YYY', 'Option with required argument', method(:yyy_handler)) +parser.parse! diff --git a/doc/optparse/ruby/missing_options.rb b/doc/optparse/ruby/missing_options.rb new file mode 100644 index 0000000000..9428463cfd --- /dev/null +++ b/doc/optparse/ruby/missing_options.rb @@ -0,0 +1,12 @@ +require 'optparse' +parser = OptionParser.new +parser.on('-x', '--xxx', 'Short and long, no argument') +parser.on('-yYYY', '--yyy', 'Short and long, required argument') +parser.on('-z [ZZZ]', '--zzz', 'Short and long, optional argument') +options = {} +parser.parse!(into: options) +required_options = [:xxx, :zzz] +missing_options = required_options - options.keys +unless missing_options.empty? + fail "Missing required options: #{missing_options}" +end diff --git a/doc/optparse/ruby/mixed_names.rb b/doc/optparse/ruby/mixed_names.rb new file mode 100644 index 0000000000..67f81e7e8d --- /dev/null +++ b/doc/optparse/ruby/mixed_names.rb @@ -0,0 +1,12 @@ +require 'optparse' +parser = OptionParser.new +parser.on('-x', '--xxx', 'Short and long, no argument') do |value| + p ['--xxx', value] +end +parser.on('-yYYY', '--yyy', 'Short and long, required argument') do |value| + p ['--yyy', value] +end +parser.on('-z [ZZZ]', '--zzz', 'Short and long, optional argument') do |value| + p ['--zzz', value] +end +parser.parse! diff --git a/doc/optparse/ruby/no_abbreviation.rb b/doc/optparse/ruby/no_abbreviation.rb new file mode 100644 index 0000000000..5464492705 --- /dev/null +++ b/doc/optparse/ruby/no_abbreviation.rb @@ -0,0 +1,10 @@ +require 'optparse' +parser = OptionParser.new +parser.on('-n', '--dry-run',) do |value| + p ['--dry-run', value] +end +parser.on('-d', '--draft',) do |value| + p ['--draft', value] +end +parser.require_exact = true +parser.parse! diff --git a/doc/optparse/ruby/numeric.rb b/doc/optparse/ruby/numeric.rb new file mode 100644 index 0000000000..d7021f154a --- /dev/null +++ b/doc/optparse/ruby/numeric.rb @@ -0,0 +1,6 @@ +require 'optparse' +parser = OptionParser.new +parser.on('--numeric=NUMERIC', Numeric) do |value| + p [value, value.class] +end +parser.parse! diff --git a/doc/optparse/ruby/object.rb b/doc/optparse/ruby/object.rb new file mode 100644 index 0000000000..0f5ae8b922 --- /dev/null +++ b/doc/optparse/ruby/object.rb @@ -0,0 +1,6 @@ +require 'optparse' +parser = OptionParser.new +parser.on('--object=OBJECT', Object) do |value| + p [value, value.class] +end +parser.parse! diff --git a/doc/optparse/ruby/octal_integer.rb b/doc/optparse/ruby/octal_integer.rb new file mode 100644 index 0000000000..b9644a076b --- /dev/null +++ b/doc/optparse/ruby/octal_integer.rb @@ -0,0 +1,7 @@ +require 'optparse' +include OptionParser::Acceptables +parser = OptionParser.new +parser.on('--octal_integer=OCTAL_INTEGER', OctalInteger) do |value| + p [value, value.class] +end +parser.parse! diff --git a/doc/optparse/ruby/optional_argument.rb b/doc/optparse/ruby/optional_argument.rb new file mode 100644 index 0000000000..456368a8ba --- /dev/null +++ b/doc/optparse/ruby/optional_argument.rb @@ -0,0 +1,9 @@ +require 'optparse' +parser = OptionParser.new +parser.on('-x [XXX]', '--xxx', 'Optional argument via short name') do |value| + p ['--xxx', value] +end +parser.on('-y', '--yyy [YYY]', 'Optional argument via long name') do |value| + p ['--yyy', value] +end +parser.parse! diff --git a/doc/optparse/ruby/proc.rb b/doc/optparse/ruby/proc.rb new file mode 100644 index 0000000000..9c669fdc92 --- /dev/null +++ b/doc/optparse/ruby/proc.rb @@ -0,0 +1,13 @@ +require 'optparse' +parser = OptionParser.new +parser.on( + '--xxx', + 'Option with no argument', + ->(value) {p ['Handler proc for -xxx called with value:', value]} +) +parser.on( + '--yyy YYY', + 'Option with required argument', + ->(value) {p ['Handler proc for -yyy called with value:', value]} +) +parser.parse! diff --git a/doc/optparse/ruby/regexp.rb b/doc/optparse/ruby/regexp.rb new file mode 100644 index 0000000000..6aba45ce76 --- /dev/null +++ b/doc/optparse/ruby/regexp.rb @@ -0,0 +1,6 @@ +require 'optparse' +parser = OptionParser.new +parser.on('--regexp=REGEXP', Regexp) do |value| + p [value, value.class] +end +parser.parse! diff --git a/doc/optparse/ruby/required_argument.rb b/doc/optparse/ruby/required_argument.rb new file mode 100644 index 0000000000..228a492c3c --- /dev/null +++ b/doc/optparse/ruby/required_argument.rb @@ -0,0 +1,9 @@ +require 'optparse' +parser = OptionParser.new +parser.on('-x XXX', '--xxx', 'Required argument via short name') do |value| + p ['--xxx', value] +end +parser.on('-y', '--y YYY', 'Required argument via long name') do |value| + p ['--yyy', value] +end +parser.parse! diff --git a/doc/optparse/ruby/shellwords.rb b/doc/optparse/ruby/shellwords.rb new file mode 100644 index 0000000000..d181d4a4f6 --- /dev/null +++ b/doc/optparse/ruby/shellwords.rb @@ -0,0 +1,6 @@ +require 'optparse/shellwords' +parser = OptionParser.new +parser.on('--shellwords=SHELLWORDS', Shellwords) do |value| + p [value, value.class] +end +parser.parse! diff --git a/doc/optparse/ruby/short_names.rb b/doc/optparse/ruby/short_names.rb new file mode 100644 index 0000000000..4a756518fa --- /dev/null +++ b/doc/optparse/ruby/short_names.rb @@ -0,0 +1,9 @@ +require 'optparse' +parser = OptionParser.new +parser.on('-x', 'Short name') do |value| + p ['x', value] +end +parser.on('-1', '-%', 'Two short names') do |value| + p ['-1 or -%', value] +end +parser.parse! diff --git a/doc/optparse/ruby/short_optional.rb b/doc/optparse/ruby/short_optional.rb new file mode 100644 index 0000000000..6eebf01c5f --- /dev/null +++ b/doc/optparse/ruby/short_optional.rb @@ -0,0 +1,6 @@ +require 'optparse' +parser = OptionParser.new +parser.on('-x [XXX]', 'Short name with optional argument') do |value| + p ['-x', value] +end +parser.parse! diff --git a/doc/optparse/ruby/short_range.rb b/doc/optparse/ruby/short_range.rb new file mode 100644 index 0000000000..f5b870a4bd --- /dev/null +++ b/doc/optparse/ruby/short_range.rb @@ -0,0 +1,6 @@ +require 'optparse' +parser = OptionParser.new +parser.on('-[!-~]', 'Short names in (very large) range') do |name, value| + p ['!-~', name, value] +end +parser.parse! diff --git a/doc/optparse/ruby/short_required.rb b/doc/optparse/ruby/short_required.rb new file mode 100644 index 0000000000..867c02c9f5 --- /dev/null +++ b/doc/optparse/ruby/short_required.rb @@ -0,0 +1,6 @@ +require 'optparse' +parser = OptionParser.new +parser.on('-xXXX', 'Short name with required argument') do |value| + p ['-x', value] +end +parser.parse! diff --git a/doc/optparse/ruby/short_simple.rb b/doc/optparse/ruby/short_simple.rb new file mode 100644 index 0000000000..d3d489e2dc --- /dev/null +++ b/doc/optparse/ruby/short_simple.rb @@ -0,0 +1,9 @@ +require 'optparse' +parser = OptionParser.new +parser.on('-x', 'One short name') do |value| + p ['-x', value] +end +parser.on('-1', '-%', 'Two short names (aliases)') do |value| + p ['-1 or -%', value] +end +parser.parse! diff --git a/doc/optparse/ruby/string.rb b/doc/optparse/ruby/string.rb new file mode 100644 index 0000000000..fee84a17ea --- /dev/null +++ b/doc/optparse/ruby/string.rb @@ -0,0 +1,6 @@ +require 'optparse' +parser = OptionParser.new +parser.on('--string=STRING', String) do |value| + p [value, value.class] +end +parser.parse! diff --git a/doc/optparse/ruby/terminator.rb b/doc/optparse/ruby/terminator.rb new file mode 100644 index 0000000000..c718ac1a97 --- /dev/null +++ b/doc/optparse/ruby/terminator.rb @@ -0,0 +1,6 @@ +require 'optparse' +parser = OptionParser.new +parser.on('--my_option XXX') do |value| + p [value, value.class] +end +parser.parse! diff --git a/doc/optparse/ruby/time.rb b/doc/optparse/ruby/time.rb new file mode 100644 index 0000000000..aa8b0cfa16 --- /dev/null +++ b/doc/optparse/ruby/time.rb @@ -0,0 +1,6 @@ +require 'optparse/time' +parser = OptionParser.new +parser.on('--time=TIME', Time) do |value| + p [value, value.class] +end +parser.parse! diff --git a/doc/optparse/ruby/true_class.rb b/doc/optparse/ruby/true_class.rb new file mode 100644 index 0000000000..40db9d07c5 --- /dev/null +++ b/doc/optparse/ruby/true_class.rb @@ -0,0 +1,6 @@ +require 'optparse' +parser = OptionParser.new +parser.on('--true_class=TRUE_CLASS', TrueClass) do |value| + p [value, value.class] +end +parser.parse! diff --git a/doc/optparse/ruby/uri.rb b/doc/optparse/ruby/uri.rb new file mode 100644 index 0000000000..b492835b4f --- /dev/null +++ b/doc/optparse/ruby/uri.rb @@ -0,0 +1,6 @@ +require 'optparse/uri' +parser = OptionParser.new +parser.on('--uri=URI', URI) do |value| + p [value, value.class] +end +parser.parse! diff --git a/doc/optparse/tutorial.rdoc b/doc/optparse/tutorial.rdoc new file mode 100644 index 0000000000..7721dc60a2 --- /dev/null +++ b/doc/optparse/tutorial.rdoc @@ -0,0 +1,388 @@ +== Tutorial + +=== Why OptionParser? + +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 + +Execution, with arguments and options: + + $ ruby argv.rb foo --bar --baz bat bam + ["foo", "--bar", "--baz", "bat", "bam"] + +The executing program is responsible for parsing and handling +the command-line options. + +OptionParser offers methods for parsing and handling those options. + +With \OptionParser, you can define options so that for each option: + +- The code that defines the option and code that handles that option + are in the same place. +- The option may take no argument, a required argument, or an optional argument. +- The argument may be automatically converted to a specified class. +- The argument may be restricted to specified _forms_. +- The argument may be restricted to specified _values_. + +The class also has: + +- Method #summarize: returns a text summary of the options. +- Method #help: displays automatically-generated help text. + +=== Contents + +- {Defining Options}[#label-Defining+Options] +- {Option Names}[#label-Option+Names] + - {Short Option Names}[#label-Short+Option+Names] + - {Long Option Names}[#label-Long+Option+Names] + - {Mixing Option Names}[#label-Mixing+Option+Names] + - {Command-Line Abbreviations}[#label-Command-Line+Abbreviations] +- {Option Arguments}[#label-Option+Arguments] + - {Option with No Argument}[#label-Option+with+No+Argument] + - {Option with Required Argument}[#label-Option+with+Required+Argument] + - {Option with Optional Argument}[#label-Option+with+Optional+Argument] +- {Keyword Argument into}[#label-Keyword+Argument+into] + - {Collecting Options}[#label-Collecting+Options] + - {Checking for Missing Options}[#label-Checking+for+Missing+Options] + - {Default Values for Options}[#label-Default+Values+for+Options] +- {Argument Converters}[#label-Argument+Converters] + +=== Defining Options + +A common way to define an option in \OptionParser +is with instance method OptionParser#on. + +The method may be called with any number of arguments +(whose order does not matter), +and may also have a trailing optional keyword argument +into+. + +The given arguments determine the characteristics of the new option. +These may include: + +- One or more short option names. +- One or more long option names. +- Whether the option takes no argument, an optional argument, or a required argument. +- Acceptable _forms_ for the argument. +- Acceptable _values_ for the argument. +- A proc or method to be called when the parser encounters the option. +- String descriptions for the option. + +=== Option Names + +You can give an option one or more names of two types: + +- Short (1-character) name, beginning with one hyphen (-). +- Long (multi-character) name, beginning with two hyphens (--). + +==== Short Option Names + +A short option name consists of a hyphen and a single character. + +File +short_names.rb+ +defines an option with a short name, -x, +and an option with two short names (aliases, in effect) -y and -z. + + :include: ruby/short_names.rb + +Executions: + + $ ruby short_names.rb --help + Usage: short_names [options] + -x Short name + -1, -% Two short names + $ 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] + +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 `
': invalid option: -z (OptionParser::InvalidOption) + +==== Long Option Names + +A long option name consists of two hyphens and a one or more characters +(usually two or more characters). + +File +long_names.rb+ +defines an option with a long name, --xxx, +and an option with two long names (aliases, in effect) --y1% and --z2#. + + :include: ruby/long_names.rb + +Executions: + + $ ruby long_names.rb --help + Usage: long_names [options] + --xxx Long name + --y1%, --z2# Two long names + $ ruby long_names.rb --xxx + ["-xxx", true] + $ ruby long_names.rb --y1% + ["--y1% or --z2#", true] + $ ruby long_names.rb --z2# + ["--y1% or --z2#", true] + +A long name may be defined with both positive and negative senses. + +File +long_with_negation.rb+ defines an option that has both senses. + + :include: ruby/long_with_negation.rb + +Executions: + + $ ruby long_with_negation.rb --help + Usage: long_with_negation [options] + --[no-]binary Long name with negation + $ ruby long_with_negation.rb --binary + [true, TrueClass] + $ ruby long_with_negation.rb --no-binary + [false, FalseClass] + +==== Mixing Option Names + +Many developers like to mix short and long option names, +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 + +Executions: + + $ ruby mixed_names.rb --help + Usage: mixed_names [options] + -x, --xxx Short and long, no argument + -y, --yyyYYY Short and long, required argument + -z, --zzz [ZZZ] Short and long, optional argument + $ ruby mixed_names.rb -x + ["--xxx", true] + $ ruby mixed_names.rb --xxx + ["--xxx", true] + $ ruby mixed_names.rb -y + mixed_names.rb:12:in `
': missing argument: -y (OptionParser::MissingArgument) + $ ruby mixed_names.rb -y FOO + ["--yyy", "FOO"] + $ ruby mixed_names.rb --yyy + mixed_names.rb:12:in `
': missing argument: --yyy (OptionParser::MissingArgument) + $ ruby mixed_names.rb --yyy BAR + ["--yyy", "BAR"] + $ ruby mixed_names.rb -z + ["--zzz", nil] + $ ruby mixed_names.rb -z BAZ + ["--zzz", "BAZ"] + $ ruby mixed_names.rb --zzz + ["--zzz", nil] + $ ruby mixed_names.rb --zzz BAT + ["--zzz", "BAT"] + +==== Command-Line Abbreviations + +By default, abbreviations for command-line option names are allowed. +An abbreviated option is valid if it is unique among abbreviated option names. + + :include: ruby/abbreviation.rb + +Executions: + + $ ruby abbreviation.rb --help + Usage: abbreviation [options] + -n, --dry-run + -d, --draft + $ ruby abbreviation.rb -n + ["--dry-run", true] + $ ruby abbreviation.rb --dry-run + ["--dry-run", true] + $ ruby abbreviation.rb -d + ["--draft", true] + $ ruby abbreviation.rb --draft + ["--draft", true] + $ ruby abbreviation.rb --d + abbreviation.rb:9:in `
': ambiguous option: --d (OptionParser::AmbiguousOption) + $ ruby abbreviation.rb --dr + abbreviation.rb:9:in `
': ambiguous option: --dr (OptionParser::AmbiguousOption) + $ ruby abbreviation.rb --dry + ["--dry-run", true] + $ ruby abbreviation.rb --dra + ["--draft", true] + +You can disable abbreviation using method +require_exact+. + + :include: ruby/no_abbreviation.rb + +Executions: + + $ ruby no_abbreviation.rb --dry-ru + no_abbreviation.rb:10:in `
': invalid option: --dry-ru (OptionParser::InvalidOption) + $ ruby no_abbreviation.rb --dry-run + ["--dry-run", 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: ruby/required_argument.rb + +When an option is found, the given argument is yielded. + +Executions: + + $ ruby required_argument.rb --help + Usage: required_argument [options] + -x, --xxx XXX Required argument via short name + -y, --y YYY Required argument via long name + $ 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 `
': 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: ruby/optional_argument.rb + +When an option with an argument is found, the given argument yielded. + +Executions: + + $ ruby optional_argument.rb --help + Usage: optional_argument [options] + -x, --xxx [XXX] Optional argument via short name + -y, --yyy [YYY] Optional argument via long name + $ 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. + +=== Keyword Argument +into+ + +In parsing options, you can add keyword option +into+ with a hash-like argument; +each parsed option will be added as a name/value pair. + +This is useful for: + +- Collecting options. +- Checking for missing options. +- Providing default values for options. + +==== Collecting Options + +Use keyword argument +into+ to collect options. + + :include: ruby/collected_options.rb + +Executions: + + $ ruby collected_options.rb --help + Usage: into [options] + -x, --xxx Short and long, no argument + -y, --yyyYYY Short and long, required argument + -z, --zzz [ZZZ] Short and long, optional argument + $ ruby collected_options.rb --xxx + {:xxx=>true} + $ ruby collected_options.rb --xxx --yyy FOO + {:xxx=>true, :yyy=>"FOO"} + $ ruby collected_options.rb --xxx --yyy FOO --zzz Bar + {:xxx=>true, :yyy=>"FOO", :zzz=>"Bar"} + $ ruby collected_options.rb --xxx --yyy FOO --yyy BAR + {:xxx=>true, :yyy=>"BAR"} + +Note in the last execution that the argument value for option --yyy +was overwritten. + +==== Checking for Missing Options + +Use the collected options to check for missing options. + + :include: ruby/missing_options.rb + +Executions: + + $ ruby missing_options.rb --help + Usage: missing_options [options] + -x, --xxx Short and long, no argument + -y, --yyyYYY Short and long, required argument + -z, --zzz [ZZZ] Short and long, optional argument + $ ruby missing_options.rb --yyy FOO + missing_options.rb:11:in `
': Missing required options: [:xxx, :zzz] (RuntimeError) + +==== Default Values for Options + +Initialize the +into+ argument to define default values for options. + + :include: ruby/default_values.rb + +Executions: + + $ ruby default_values.rb --help + Usage: default_values [options] + -x, --xxx Short and long, no argument + -y, --yyyYYY Short and long, required argument + -z, --zzz [ZZZ] Short and long, optional argument + $ ruby default_values.rb --yyy FOO + {:yyy=>"FOO", :zzz=>"BBB"} + + +=== Argument Converters + +An option can specify that its argument is to be converted +from the default \String to an instance of another class. +There are a number of built-in converters. + +Example: File +date.rb+ +defines an option whose argument is to be converted to a \Date object. +The argument is converted by method Date#parse. + + :include: ruby/date.rb + +Executions: + + $ ruby date.rb --date 2001-02-03 + [#, Date] + $ ruby date.rb --date 20010203 + [#, Date] + $ ruby date.rb --date "3rd Feb 2001" + [#, Date] + +You can also define custom converters. +See {Argument Converters}[./argument_converters_rdoc.html] +for both built-in and custom converters. diff --git a/doc/ruby/abbreviation.rb b/doc/ruby/abbreviation.rb deleted file mode 100644 index b438c1b3dd..0000000000 --- a/doc/ruby/abbreviation.rb +++ /dev/null @@ -1,9 +0,0 @@ -require 'optparse' -parser = OptionParser.new -parser.on('-n', '--dry-run',) do |value| - p ['--dry-run', value] -end -parser.on('-d', '--draft',) do |value| - p ['--draft', value] -end -parser.parse! diff --git a/doc/ruby/argument_keywords.rb b/doc/ruby/argument_keywords.rb deleted file mode 100644 index 8533257c67..0000000000 --- a/doc/ruby/argument_keywords.rb +++ /dev/null @@ -1,6 +0,0 @@ -require 'optparse' -parser = OptionParser.new -parser.on('-x', '--xxx', :REQUIRED, 'Required argument') do |value| - p ['--xxx', value] -end -parser.parse! diff --git a/doc/ruby/argument_strings.rb b/doc/ruby/argument_strings.rb deleted file mode 100644 index 77861dda30..0000000000 --- a/doc/ruby/argument_strings.rb +++ /dev/null @@ -1,6 +0,0 @@ -require 'optparse' -parser = OptionParser.new -parser.on('-x', '--xxx', '=XXX', 'Required argument') do |value| - p ['--xxx', value] -end -parser.parse! diff --git a/doc/ruby/argv.rb b/doc/ruby/argv.rb deleted file mode 100644 index 12495cfa1f..0000000000 --- a/doc/ruby/argv.rb +++ /dev/null @@ -1,2 +0,0 @@ -p ARGV - diff --git a/doc/ruby/array.rb b/doc/ruby/array.rb deleted file mode 100644 index 7c6c14fad4..0000000000 --- a/doc/ruby/array.rb +++ /dev/null @@ -1,6 +0,0 @@ -require 'optparse' -parser = OptionParser.new -parser.on('--array=ARRAY', Array) do |value| - p [value, value.class] -end -parser.parse! diff --git a/doc/ruby/block.rb b/doc/ruby/block.rb deleted file mode 100644 index c4dfdeb31e..0000000000 --- a/doc/ruby/block.rb +++ /dev/null @@ -1,9 +0,0 @@ -require 'optparse' -parser = OptionParser.new -parser.on('--xxx', 'Option with no argument') do |value| - p ['Handler block for -xxx called with value:', value] -end -parser.on('--yyy YYY', 'Option with required argument') do |value| - p ['Handler block for -yyy called with value:', value] -end -parser.parse! diff --git a/doc/ruby/collected_options.rb b/doc/ruby/collected_options.rb deleted file mode 100644 index 2115e03a9a..0000000000 --- a/doc/ruby/collected_options.rb +++ /dev/null @@ -1,8 +0,0 @@ -require 'optparse' -parser = OptionParser.new -parser.on('-x', '--xxx', 'Short and long, no argument') -parser.on('-yYYY', '--yyy', 'Short and long, required argument') -parser.on('-z [ZZZ]', '--zzz', 'Short and long, optional argument') -options = {} -parser.parse!(into: options) -p options diff --git a/doc/ruby/custom_converter.rb b/doc/ruby/custom_converter.rb deleted file mode 100644 index 029da08c46..0000000000 --- a/doc/ruby/custom_converter.rb +++ /dev/null @@ -1,9 +0,0 @@ -require 'optparse/date' -parser = OptionParser.new -parser.accept(Complex) do |value| - value.to_c -end -parser.on('--complex COMPLEX', Complex) do |value| - p [value, value.class] -end -parser.parse! diff --git a/doc/ruby/date.rb b/doc/ruby/date.rb deleted file mode 100644 index 5994ad6a85..0000000000 --- a/doc/ruby/date.rb +++ /dev/null @@ -1,6 +0,0 @@ -require 'optparse/date' -parser = OptionParser.new -parser.on('--date=DATE', Date) do |value| - p [value, value.class] -end -parser.parse! diff --git a/doc/ruby/datetime.rb b/doc/ruby/datetime.rb deleted file mode 100644 index b9b591d5f6..0000000000 --- a/doc/ruby/datetime.rb +++ /dev/null @@ -1,6 +0,0 @@ -require 'optparse/date' -parser = OptionParser.new -parser.on('--datetime=DATETIME', DateTime) do |value| - p [value, value.class] -end -parser.parse! diff --git a/doc/ruby/decimal_integer.rb b/doc/ruby/decimal_integer.rb deleted file mode 100644 index 360bd284f8..0000000000 --- a/doc/ruby/decimal_integer.rb +++ /dev/null @@ -1,7 +0,0 @@ -require 'optparse' -include OptionParser::Acceptables -parser = OptionParser.new -parser.on('--decimal_integer=DECIMAL_INTEGER', DecimalInteger) do |value| - p [value, value.class] -end -parser.parse! diff --git a/doc/ruby/decimal_numeric.rb b/doc/ruby/decimal_numeric.rb deleted file mode 100644 index 954da13561..0000000000 --- a/doc/ruby/decimal_numeric.rb +++ /dev/null @@ -1,7 +0,0 @@ -require 'optparse' -include OptionParser::Acceptables -parser = OptionParser.new -parser.on('--decimal_numeric=DECIMAL_NUMERIC', DecimalNumeric) do |value| - p [value, value.class] -end -parser.parse! diff --git a/doc/ruby/default_values.rb b/doc/ruby/default_values.rb deleted file mode 100644 index 24c26faea2..0000000000 --- a/doc/ruby/default_values.rb +++ /dev/null @@ -1,8 +0,0 @@ -require 'optparse' -parser = OptionParser.new -parser.on('-x', '--xxx', 'Short and long, no argument') -parser.on('-yYYY', '--yyy', 'Short and long, required argument') -parser.on('-z [ZZZ]', '--zzz', 'Short and long, optional argument') -options = {yyy: 'AAA', zzz: 'BBB'} -parser.parse!(into: options) -p options diff --git a/doc/ruby/descriptions.rb b/doc/ruby/descriptions.rb deleted file mode 100644 index 9aec80aae2..0000000000 --- a/doc/ruby/descriptions.rb +++ /dev/null @@ -1,15 +0,0 @@ -require 'optparse' -parser = OptionParser.new -description = <<-EOT -Lorem ipsum dolor sit amet, consectetuer -adipiscing elit. Aenean commodo ligula eget. -Aenean massa. Cum sociis natoque penatibus -et magnis dis parturient montes, nascetur -ridiculus mus. Donec quam felis, ultricies -nec, pellentesque eu, pretium quis, sem. -EOT -descriptions = description.split($/) -parser.on('--xxx', *descriptions) do |value| - p ['--xxx', value] -end -parser.parse! diff --git a/doc/ruby/explicit_array_values.rb b/doc/ruby/explicit_array_values.rb deleted file mode 100644 index 64f930a4bc..0000000000 --- a/doc/ruby/explicit_array_values.rb +++ /dev/null @@ -1,9 +0,0 @@ -require 'optparse' -parser = OptionParser.new -parser.on('-xXXX', ['foo', 'bar'], 'Values for required argument' ) do |value| - p ['-x', value] -end -parser.on('-y [YYY]', ['baz', 'bat'], 'Values for optional argument') do |value| - p ['-y', value] -end -parser.parse! diff --git a/doc/ruby/explicit_hash_values.rb b/doc/ruby/explicit_hash_values.rb deleted file mode 100644 index 9c9e6a48ed..0000000000 --- a/doc/ruby/explicit_hash_values.rb +++ /dev/null @@ -1,9 +0,0 @@ -require 'optparse' -parser = OptionParser.new -parser.on('-xXXX', {foo: 0, bar: 1}, 'Values for required argument' ) do |value| - p ['-x', value] -end -parser.on('-y [YYY]', {baz: 2, bat: 3}, 'Values for optional argument') do |value| - p ['-y', value] -end -parser.parse! diff --git a/doc/ruby/false_class.rb b/doc/ruby/false_class.rb deleted file mode 100644 index 04fe335ede..0000000000 --- a/doc/ruby/false_class.rb +++ /dev/null @@ -1,6 +0,0 @@ -require 'optparse' -parser = OptionParser.new -parser.on('--false_class=FALSE_CLASS', FalseClass) do |value| - p [value, value.class] -end -parser.parse! diff --git a/doc/ruby/float.rb b/doc/ruby/float.rb deleted file mode 100644 index 390df7f7bd..0000000000 --- a/doc/ruby/float.rb +++ /dev/null @@ -1,6 +0,0 @@ -require 'optparse' -parser = OptionParser.new -parser.on('--float=FLOAT', Float) do |value| - p [value, value.class] -end -parser.parse! diff --git a/doc/ruby/integer.rb b/doc/ruby/integer.rb deleted file mode 100644 index f10656ff1a..0000000000 --- a/doc/ruby/integer.rb +++ /dev/null @@ -1,6 +0,0 @@ -require 'optparse' -parser = OptionParser.new -parser.on('--integer=INTEGER', Integer) do |value| - p [value, value.class] -end -parser.parse! diff --git a/doc/ruby/long_names.rb b/doc/ruby/long_names.rb deleted file mode 100644 index a49dbda69f..0000000000 --- a/doc/ruby/long_names.rb +++ /dev/null @@ -1,9 +0,0 @@ -require 'optparse' -parser = OptionParser.new -parser.on('--xxx', 'Long name') do |value| - p ['-xxx', value] -end -parser.on('--y1%', '--z2#', "Two long names") do |value| - p ['--y1% or --z2#', value] -end -parser.parse! diff --git a/doc/ruby/long_optional.rb b/doc/ruby/long_optional.rb deleted file mode 100644 index 38dd82166b..0000000000 --- a/doc/ruby/long_optional.rb +++ /dev/null @@ -1,6 +0,0 @@ -require 'optparse' -parser = OptionParser.new -parser.on('--xxx [XXX]', 'Long name with optional argument') do |value| - p ['--xxx', value] -end -parser.parse! diff --git a/doc/ruby/long_required.rb b/doc/ruby/long_required.rb deleted file mode 100644 index b76c997339..0000000000 --- a/doc/ruby/long_required.rb +++ /dev/null @@ -1,6 +0,0 @@ -require 'optparse' -parser = OptionParser.new -parser.on('--xxx XXX', 'Long name with required argument') do |value| - p ['--xxx', value] -end -parser.parse! diff --git a/doc/ruby/long_simple.rb b/doc/ruby/long_simple.rb deleted file mode 100644 index 4e489c43ed..0000000000 --- a/doc/ruby/long_simple.rb +++ /dev/null @@ -1,9 +0,0 @@ -require 'optparse' -parser = OptionParser.new -parser.on('--xxx', 'One long name') do |value| - p ['--xxx', value] -end -parser.on('--y1%', '--z2#', 'Two long names (aliases)') do |value| - p ['--y1% or --z2#', value] -end -parser.parse! diff --git a/doc/ruby/long_with_negation.rb b/doc/ruby/long_with_negation.rb deleted file mode 100644 index 3f2913c361..0000000000 --- a/doc/ruby/long_with_negation.rb +++ /dev/null @@ -1,6 +0,0 @@ -require 'optparse' -parser = OptionParser.new -parser.on('--[no-]binary', 'Long name with negation') do |value| - p [value, value.class] -end -parser.parse! diff --git a/doc/ruby/matched_values.rb b/doc/ruby/matched_values.rb deleted file mode 100644 index f184ca8474..0000000000 --- a/doc/ruby/matched_values.rb +++ /dev/null @@ -1,6 +0,0 @@ -require 'optparse' -parser = OptionParser.new -parser.on('--xxx XXX', /foo/i, 'Matched values') do |value| - p ['--xxx', value] -end -parser.parse! diff --git a/doc/ruby/method.rb b/doc/ruby/method.rb deleted file mode 100644 index 3f02ff5798..0000000000 --- a/doc/ruby/method.rb +++ /dev/null @@ -1,11 +0,0 @@ -require 'optparse' -parser = OptionParser.new -def xxx_handler(value) - p ['Handler method for -xxx called with value:', value] -end -parser.on('--xxx', 'Option with no argument', method(:xxx_handler)) -def yyy_handler(value) - p ['Handler method for -yyy called with value:', value] -end -parser.on('--yyy YYY', 'Option with required argument', method(:yyy_handler)) -parser.parse! diff --git a/doc/ruby/missing_options.rb b/doc/ruby/missing_options.rb deleted file mode 100644 index 9428463cfd..0000000000 --- a/doc/ruby/missing_options.rb +++ /dev/null @@ -1,12 +0,0 @@ -require 'optparse' -parser = OptionParser.new -parser.on('-x', '--xxx', 'Short and long, no argument') -parser.on('-yYYY', '--yyy', 'Short and long, required argument') -parser.on('-z [ZZZ]', '--zzz', 'Short and long, optional argument') -options = {} -parser.parse!(into: options) -required_options = [:xxx, :zzz] -missing_options = required_options - options.keys -unless missing_options.empty? - fail "Missing required options: #{missing_options}" -end diff --git a/doc/ruby/mixed_names.rb b/doc/ruby/mixed_names.rb deleted file mode 100644 index 67f81e7e8d..0000000000 --- a/doc/ruby/mixed_names.rb +++ /dev/null @@ -1,12 +0,0 @@ -require 'optparse' -parser = OptionParser.new -parser.on('-x', '--xxx', 'Short and long, no argument') do |value| - p ['--xxx', value] -end -parser.on('-yYYY', '--yyy', 'Short and long, required argument') do |value| - p ['--yyy', value] -end -parser.on('-z [ZZZ]', '--zzz', 'Short and long, optional argument') do |value| - p ['--zzz', value] -end -parser.parse! diff --git a/doc/ruby/no_abbreviation.rb b/doc/ruby/no_abbreviation.rb deleted file mode 100644 index 5464492705..0000000000 --- a/doc/ruby/no_abbreviation.rb +++ /dev/null @@ -1,10 +0,0 @@ -require 'optparse' -parser = OptionParser.new -parser.on('-n', '--dry-run',) do |value| - p ['--dry-run', value] -end -parser.on('-d', '--draft',) do |value| - p ['--draft', value] -end -parser.require_exact = true -parser.parse! diff --git a/doc/ruby/numeric.rb b/doc/ruby/numeric.rb deleted file mode 100644 index d7021f154a..0000000000 --- a/doc/ruby/numeric.rb +++ /dev/null @@ -1,6 +0,0 @@ -require 'optparse' -parser = OptionParser.new -parser.on('--numeric=NUMERIC', Numeric) do |value| - p [value, value.class] -end -parser.parse! diff --git a/doc/ruby/object.rb b/doc/ruby/object.rb deleted file mode 100644 index 0f5ae8b922..0000000000 --- a/doc/ruby/object.rb +++ /dev/null @@ -1,6 +0,0 @@ -require 'optparse' -parser = OptionParser.new -parser.on('--object=OBJECT', Object) do |value| - p [value, value.class] -end -parser.parse! diff --git a/doc/ruby/octal_integer.rb b/doc/ruby/octal_integer.rb deleted file mode 100644 index b9644a076b..0000000000 --- a/doc/ruby/octal_integer.rb +++ /dev/null @@ -1,7 +0,0 @@ -require 'optparse' -include OptionParser::Acceptables -parser = OptionParser.new -parser.on('--octal_integer=OCTAL_INTEGER', OctalInteger) do |value| - p [value, value.class] -end -parser.parse! diff --git a/doc/ruby/optional_argument.rb b/doc/ruby/optional_argument.rb deleted file mode 100644 index 456368a8ba..0000000000 --- a/doc/ruby/optional_argument.rb +++ /dev/null @@ -1,9 +0,0 @@ -require 'optparse' -parser = OptionParser.new -parser.on('-x [XXX]', '--xxx', 'Optional argument via short name') do |value| - p ['--xxx', value] -end -parser.on('-y', '--yyy [YYY]', 'Optional argument via long name') do |value| - p ['--yyy', value] -end -parser.parse! diff --git a/doc/ruby/proc.rb b/doc/ruby/proc.rb deleted file mode 100644 index 9c669fdc92..0000000000 --- a/doc/ruby/proc.rb +++ /dev/null @@ -1,13 +0,0 @@ -require 'optparse' -parser = OptionParser.new -parser.on( - '--xxx', - 'Option with no argument', - ->(value) {p ['Handler proc for -xxx called with value:', value]} -) -parser.on( - '--yyy YYY', - 'Option with required argument', - ->(value) {p ['Handler proc for -yyy called with value:', value]} -) -parser.parse! diff --git a/doc/ruby/regexp.rb b/doc/ruby/regexp.rb deleted file mode 100644 index 6aba45ce76..0000000000 --- a/doc/ruby/regexp.rb +++ /dev/null @@ -1,6 +0,0 @@ -require 'optparse' -parser = OptionParser.new -parser.on('--regexp=REGEXP', Regexp) do |value| - p [value, value.class] -end -parser.parse! diff --git a/doc/ruby/required_argument.rb b/doc/ruby/required_argument.rb deleted file mode 100644 index 228a492c3c..0000000000 --- a/doc/ruby/required_argument.rb +++ /dev/null @@ -1,9 +0,0 @@ -require 'optparse' -parser = OptionParser.new -parser.on('-x XXX', '--xxx', 'Required argument via short name') do |value| - p ['--xxx', value] -end -parser.on('-y', '--y YYY', 'Required argument via long name') do |value| - p ['--yyy', value] -end -parser.parse! diff --git a/doc/ruby/shellwords.rb b/doc/ruby/shellwords.rb deleted file mode 100644 index d181d4a4f6..0000000000 --- a/doc/ruby/shellwords.rb +++ /dev/null @@ -1,6 +0,0 @@ -require 'optparse/shellwords' -parser = OptionParser.new -parser.on('--shellwords=SHELLWORDS', Shellwords) do |value| - p [value, value.class] -end -parser.parse! diff --git a/doc/ruby/short_names.rb b/doc/ruby/short_names.rb deleted file mode 100644 index 4a756518fa..0000000000 --- a/doc/ruby/short_names.rb +++ /dev/null @@ -1,9 +0,0 @@ -require 'optparse' -parser = OptionParser.new -parser.on('-x', 'Short name') do |value| - p ['x', value] -end -parser.on('-1', '-%', 'Two short names') do |value| - p ['-1 or -%', value] -end -parser.parse! diff --git a/doc/ruby/short_optional.rb b/doc/ruby/short_optional.rb deleted file mode 100644 index 6eebf01c5f..0000000000 --- a/doc/ruby/short_optional.rb +++ /dev/null @@ -1,6 +0,0 @@ -require 'optparse' -parser = OptionParser.new -parser.on('-x [XXX]', 'Short name with optional argument') do |value| - p ['-x', value] -end -parser.parse! diff --git a/doc/ruby/short_range.rb b/doc/ruby/short_range.rb deleted file mode 100644 index f5b870a4bd..0000000000 --- a/doc/ruby/short_range.rb +++ /dev/null @@ -1,6 +0,0 @@ -require 'optparse' -parser = OptionParser.new -parser.on('-[!-~]', 'Short names in (very large) range') do |name, value| - p ['!-~', name, value] -end -parser.parse! diff --git a/doc/ruby/short_required.rb b/doc/ruby/short_required.rb deleted file mode 100644 index 867c02c9f5..0000000000 --- a/doc/ruby/short_required.rb +++ /dev/null @@ -1,6 +0,0 @@ -require 'optparse' -parser = OptionParser.new -parser.on('-xXXX', 'Short name with required argument') do |value| - p ['-x', value] -end -parser.parse! diff --git a/doc/ruby/short_simple.rb b/doc/ruby/short_simple.rb deleted file mode 100644 index d3d489e2dc..0000000000 --- a/doc/ruby/short_simple.rb +++ /dev/null @@ -1,9 +0,0 @@ -require 'optparse' -parser = OptionParser.new -parser.on('-x', 'One short name') do |value| - p ['-x', value] -end -parser.on('-1', '-%', 'Two short names (aliases)') do |value| - p ['-1 or -%', value] -end -parser.parse! diff --git a/doc/ruby/string.rb b/doc/ruby/string.rb deleted file mode 100644 index fee84a17ea..0000000000 --- a/doc/ruby/string.rb +++ /dev/null @@ -1,6 +0,0 @@ -require 'optparse' -parser = OptionParser.new -parser.on('--string=STRING', String) do |value| - p [value, value.class] -end -parser.parse! diff --git a/doc/ruby/terminator.rb b/doc/ruby/terminator.rb deleted file mode 100644 index c718ac1a97..0000000000 --- a/doc/ruby/terminator.rb +++ /dev/null @@ -1,6 +0,0 @@ -require 'optparse' -parser = OptionParser.new -parser.on('--my_option XXX') do |value| - p [value, value.class] -end -parser.parse! diff --git a/doc/ruby/time.rb b/doc/ruby/time.rb deleted file mode 100644 index aa8b0cfa16..0000000000 --- a/doc/ruby/time.rb +++ /dev/null @@ -1,6 +0,0 @@ -require 'optparse/time' -parser = OptionParser.new -parser.on('--time=TIME', Time) do |value| - p [value, value.class] -end -parser.parse! diff --git a/doc/ruby/true_class.rb b/doc/ruby/true_class.rb deleted file mode 100644 index 40db9d07c5..0000000000 --- a/doc/ruby/true_class.rb +++ /dev/null @@ -1,6 +0,0 @@ -require 'optparse' -parser = OptionParser.new -parser.on('--true_class=TRUE_CLASS', TrueClass) do |value| - p [value, value.class] -end -parser.parse! diff --git a/doc/ruby/uri.rb b/doc/ruby/uri.rb deleted file mode 100644 index b492835b4f..0000000000 --- a/doc/ruby/uri.rb +++ /dev/null @@ -1,6 +0,0 @@ -require 'optparse/uri' -parser = OptionParser.new -parser.on('--uri=URI', URI) do |value| - p [value, value.class] -end -parser.parse! diff --git a/doc/tutorial.rdoc b/doc/tutorial.rdoc deleted file mode 100644 index 7721dc60a2..0000000000 --- a/doc/tutorial.rdoc +++ /dev/null @@ -1,388 +0,0 @@ -== Tutorial - -=== Why OptionParser? - -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 - -Execution, with arguments and options: - - $ ruby argv.rb foo --bar --baz bat bam - ["foo", "--bar", "--baz", "bat", "bam"] - -The executing program is responsible for parsing and handling -the command-line options. - -OptionParser offers methods for parsing and handling those options. - -With \OptionParser, you can define options so that for each option: - -- The code that defines the option and code that handles that option - are in the same place. -- The option may take no argument, a required argument, or an optional argument. -- The argument may be automatically converted to a specified class. -- The argument may be restricted to specified _forms_. -- The argument may be restricted to specified _values_. - -The class also has: - -- Method #summarize: returns a text summary of the options. -- Method #help: displays automatically-generated help text. - -=== Contents - -- {Defining Options}[#label-Defining+Options] -- {Option Names}[#label-Option+Names] - - {Short Option Names}[#label-Short+Option+Names] - - {Long Option Names}[#label-Long+Option+Names] - - {Mixing Option Names}[#label-Mixing+Option+Names] - - {Command-Line Abbreviations}[#label-Command-Line+Abbreviations] -- {Option Arguments}[#label-Option+Arguments] - - {Option with No Argument}[#label-Option+with+No+Argument] - - {Option with Required Argument}[#label-Option+with+Required+Argument] - - {Option with Optional Argument}[#label-Option+with+Optional+Argument] -- {Keyword Argument into}[#label-Keyword+Argument+into] - - {Collecting Options}[#label-Collecting+Options] - - {Checking for Missing Options}[#label-Checking+for+Missing+Options] - - {Default Values for Options}[#label-Default+Values+for+Options] -- {Argument Converters}[#label-Argument+Converters] - -=== Defining Options - -A common way to define an option in \OptionParser -is with instance method OptionParser#on. - -The method may be called with any number of arguments -(whose order does not matter), -and may also have a trailing optional keyword argument +into+. - -The given arguments determine the characteristics of the new option. -These may include: - -- One or more short option names. -- One or more long option names. -- Whether the option takes no argument, an optional argument, or a required argument. -- Acceptable _forms_ for the argument. -- Acceptable _values_ for the argument. -- A proc or method to be called when the parser encounters the option. -- String descriptions for the option. - -=== Option Names - -You can give an option one or more names of two types: - -- Short (1-character) name, beginning with one hyphen (-). -- Long (multi-character) name, beginning with two hyphens (--). - -==== Short Option Names - -A short option name consists of a hyphen and a single character. - -File +short_names.rb+ -defines an option with a short name, -x, -and an option with two short names (aliases, in effect) -y and -z. - - :include: ruby/short_names.rb - -Executions: - - $ ruby short_names.rb --help - Usage: short_names [options] - -x Short name - -1, -% Two short names - $ 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] - -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 `
': invalid option: -z (OptionParser::InvalidOption) - -==== Long Option Names - -A long option name consists of two hyphens and a one or more characters -(usually two or more characters). - -File +long_names.rb+ -defines an option with a long name, --xxx, -and an option with two long names (aliases, in effect) --y1% and --z2#. - - :include: ruby/long_names.rb - -Executions: - - $ ruby long_names.rb --help - Usage: long_names [options] - --xxx Long name - --y1%, --z2# Two long names - $ ruby long_names.rb --xxx - ["-xxx", true] - $ ruby long_names.rb --y1% - ["--y1% or --z2#", true] - $ ruby long_names.rb --z2# - ["--y1% or --z2#", true] - -A long name may be defined with both positive and negative senses. - -File +long_with_negation.rb+ defines an option that has both senses. - - :include: ruby/long_with_negation.rb - -Executions: - - $ ruby long_with_negation.rb --help - Usage: long_with_negation [options] - --[no-]binary Long name with negation - $ ruby long_with_negation.rb --binary - [true, TrueClass] - $ ruby long_with_negation.rb --no-binary - [false, FalseClass] - -==== Mixing Option Names - -Many developers like to mix short and long option names, -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 - -Executions: - - $ ruby mixed_names.rb --help - Usage: mixed_names [options] - -x, --xxx Short and long, no argument - -y, --yyyYYY Short and long, required argument - -z, --zzz [ZZZ] Short and long, optional argument - $ ruby mixed_names.rb -x - ["--xxx", true] - $ ruby mixed_names.rb --xxx - ["--xxx", true] - $ ruby mixed_names.rb -y - mixed_names.rb:12:in `
': missing argument: -y (OptionParser::MissingArgument) - $ ruby mixed_names.rb -y FOO - ["--yyy", "FOO"] - $ ruby mixed_names.rb --yyy - mixed_names.rb:12:in `
': missing argument: --yyy (OptionParser::MissingArgument) - $ ruby mixed_names.rb --yyy BAR - ["--yyy", "BAR"] - $ ruby mixed_names.rb -z - ["--zzz", nil] - $ ruby mixed_names.rb -z BAZ - ["--zzz", "BAZ"] - $ ruby mixed_names.rb --zzz - ["--zzz", nil] - $ ruby mixed_names.rb --zzz BAT - ["--zzz", "BAT"] - -==== Command-Line Abbreviations - -By default, abbreviations for command-line option names are allowed. -An abbreviated option is valid if it is unique among abbreviated option names. - - :include: ruby/abbreviation.rb - -Executions: - - $ ruby abbreviation.rb --help - Usage: abbreviation [options] - -n, --dry-run - -d, --draft - $ ruby abbreviation.rb -n - ["--dry-run", true] - $ ruby abbreviation.rb --dry-run - ["--dry-run", true] - $ ruby abbreviation.rb -d - ["--draft", true] - $ ruby abbreviation.rb --draft - ["--draft", true] - $ ruby abbreviation.rb --d - abbreviation.rb:9:in `
': ambiguous option: --d (OptionParser::AmbiguousOption) - $ ruby abbreviation.rb --dr - abbreviation.rb:9:in `
': ambiguous option: --dr (OptionParser::AmbiguousOption) - $ ruby abbreviation.rb --dry - ["--dry-run", true] - $ ruby abbreviation.rb --dra - ["--draft", true] - -You can disable abbreviation using method +require_exact+. - - :include: ruby/no_abbreviation.rb - -Executions: - - $ ruby no_abbreviation.rb --dry-ru - no_abbreviation.rb:10:in `
': invalid option: --dry-ru (OptionParser::InvalidOption) - $ ruby no_abbreviation.rb --dry-run - ["--dry-run", 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: ruby/required_argument.rb - -When an option is found, the given argument is yielded. - -Executions: - - $ ruby required_argument.rb --help - Usage: required_argument [options] - -x, --xxx XXX Required argument via short name - -y, --y YYY Required argument via long name - $ 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 `
': 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: ruby/optional_argument.rb - -When an option with an argument is found, the given argument yielded. - -Executions: - - $ ruby optional_argument.rb --help - Usage: optional_argument [options] - -x, --xxx [XXX] Optional argument via short name - -y, --yyy [YYY] Optional argument via long name - $ 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. - -=== Keyword Argument +into+ - -In parsing options, you can add keyword option +into+ with a hash-like argument; -each parsed option will be added as a name/value pair. - -This is useful for: - -- Collecting options. -- Checking for missing options. -- Providing default values for options. - -==== Collecting Options - -Use keyword argument +into+ to collect options. - - :include: ruby/collected_options.rb - -Executions: - - $ ruby collected_options.rb --help - Usage: into [options] - -x, --xxx Short and long, no argument - -y, --yyyYYY Short and long, required argument - -z, --zzz [ZZZ] Short and long, optional argument - $ ruby collected_options.rb --xxx - {:xxx=>true} - $ ruby collected_options.rb --xxx --yyy FOO - {:xxx=>true, :yyy=>"FOO"} - $ ruby collected_options.rb --xxx --yyy FOO --zzz Bar - {:xxx=>true, :yyy=>"FOO", :zzz=>"Bar"} - $ ruby collected_options.rb --xxx --yyy FOO --yyy BAR - {:xxx=>true, :yyy=>"BAR"} - -Note in the last execution that the argument value for option --yyy -was overwritten. - -==== Checking for Missing Options - -Use the collected options to check for missing options. - - :include: ruby/missing_options.rb - -Executions: - - $ ruby missing_options.rb --help - Usage: missing_options [options] - -x, --xxx Short and long, no argument - -y, --yyyYYY Short and long, required argument - -z, --zzz [ZZZ] Short and long, optional argument - $ ruby missing_options.rb --yyy FOO - missing_options.rb:11:in `
': Missing required options: [:xxx, :zzz] (RuntimeError) - -==== Default Values for Options - -Initialize the +into+ argument to define default values for options. - - :include: ruby/default_values.rb - -Executions: - - $ ruby default_values.rb --help - Usage: default_values [options] - -x, --xxx Short and long, no argument - -y, --yyyYYY Short and long, required argument - -z, --zzz [ZZZ] Short and long, optional argument - $ ruby default_values.rb --yyy FOO - {:yyy=>"FOO", :zzz=>"BBB"} - - -=== Argument Converters - -An option can specify that its argument is to be converted -from the default \String to an instance of another class. -There are a number of built-in converters. - -Example: File +date.rb+ -defines an option whose argument is to be converted to a \Date object. -The argument is converted by method Date#parse. - - :include: ruby/date.rb - -Executions: - - $ ruby date.rb --date 2001-02-03 - [#, Date] - $ ruby date.rb --date 20010203 - [#, Date] - $ ruby date.rb --date "3rd Feb 2001" - [#, Date] - -You can also define custom converters. -See {Argument Converters}[./argument_converters_rdoc.html] -for both built-in and custom converters. -- cgit v1.2.3