optparse.rb

Path: lib/optparse.rb
Last Update: Tue Jun 30 13:22:25 -0700 2009

optparse.rb - command-line option analysis with the OptionParser class.

Author:Nobu Nakada
Documentation:Nobu Nakada and Gavin Sinclair.

See OptionParser for documentation.

Required files

shellwords  

Methods

abort   accept   accept   banner   base   def_head_option   def_option   def_tail_option   define   define_head   define_tail   environment   getopts   getopts   help   inc   inc   load   make_switch   new   new   on   on_head   on_tail   order   order!   parse   parse!   permute   permute!   program_name   reject   reject   release   remove   separator   summarize   terminate   terminate   to_a   to_s   top   top   ver   version   warn   with  

Constants

DecimalInteger = /\A[-+]?#{decimal}/io   Decimal integer format, to be converted to Integer.
OctalInteger = /\A[-+]?(?:[0-7]+(?:_[0-7]+)*|0(?:#{binary}|#{hex}))/io   Ruby/C like octal/hexadecimal/binary integer format, to be converted to Integer.
DecimalNumeric = floatpat # decimal integer is allowed as float also.   Decimal integer/float number format, to be converted to Integer for integer format, Float for float format.

External Aliases

banner= -> set_banner
  for experimental cascading :-)
program_name= -> set_program_name
summary_width= -> set_summary_width
summary_indent= -> set_summary_indent

Public Class methods

Returns an incremented value of default according to arg.

Initializes the instance and yields itself if called with a block.

banner:Banner message.
width:Summary width.
indent:Summary indent.

Initializes a new instance and evaluates the optional block in context of the instance. Arguments args are passed to new, see there for description of parameters.

This method is deprecated, its behavior corresponds to the older new method.

Public Instance methods

Directs to accept specified class t. The argument string is passed to the block in which it should be converted to the desired class.

t:Argument class specifier, any object including Class.
pat:Pattern for argument, defaults to t if it responds to match.
  accept(t, pat, &block)

Heading banner preceding summary.

Subject of on_tail.

def_head_option(*opts, &block)

Alias for define_head

def_option(*opts, &block)

Alias for define

def_tail_option(*opts, &block)

Alias for define_tail

Parses environment variable env or its uppercase with splitting like a shell.

env defaults to the basename of the program.

Wrapper method for getopts.rb.

  params = ARGV.getopts("ab:", "foo", "bar:")
  # params[:a] = true   # -a
  # params[:b] = "1"    # -b1
  # params[:foo] = "1"  # --foo
  # params[:bar] = "x"  # --bar x

Returns option summary string.

Loads options from file names as filename. Does nothing when the file is not present. Returns whether successfully loaded.

filename defaults to basename of the program without suffix in a directory ~/.options.

Creates an OptionParser::Switch from the parameters. The parsed argument value is passed to the given block, where it can be processed.

See at the beginning of OptionParser for some full examples.

opts can include the following elements:

Argument style:
One of the following:
  :NONE, :REQUIRED, :OPTIONAL
Argument pattern:
Acceptable option argument format, must be pre-defined with OptionParser.accept or OptionParser#accept, or Regexp. This can appear once or assigned as String if not present, otherwise causes an ArgumentError. Examples:
  Float, Time, Array
Possible argument values:
Hash or Array.
  [:text, :binary, :auto]
  %w[iso-2022-jp shift_jis euc-jp utf8 binary]
  { "jis" => "iso-2022-jp", "sjis" => "shift_jis" }
Long style switch:
Specifies a long style switch which takes a mandatory, optional or no argument. It‘s a string of the following form:
  "--switch=MANDATORY" or "--switch MANDATORY"
  "--switch[=OPTIONAL]"
  "--switch"
Short style switch:
Specifies short style switch which takes a mandatory, optional or no argument. It‘s a string of the following form:
  "-xMANDATORY"
  "-x[OPTIONAL]"
  "-x"

There is also a special form which matches character range (not full set of regural expression):

  "-[a-z]MANDATORY"
  "-[a-z][OPTIONAL]"
  "-[a-z]"
Argument style and description:
Instead of specifying mandatory or optional orguments directly in the switch parameter, this separate parameter can be used.
  "=MANDATORY"
  "=[OPTIONAL]"
Description:
Description string for the option.
  "Run verbosely"
Handler:
Handler for the parsed argument value. Either give a block or pass a Proc or Method as an argument.

Add option switch and handler. See make_switch for an explanation of parameters.

Add option switch like with on, but at head of summary.

Add option switch like with on, but at tail of summary.

Parses command line arguments argv in order. When a block is given, each non-option argument is yielded.

Returns the rest of argv left unparsed.

Same as order, but removes switches destructively.

Parses command line arguments argv in order when environment variable POSIXLY_CORRECT is set, and in permutation mode otherwise.

Same as parse, but removes switches destructively.

Parses command line arguments argv in permutation mode and returns list of non-option arguments.

Same as permute, but removes switches destructively.

Program name to be emitted in error message and default banner, defaults to $0.

Directs to reject specified class argument.

t:Argument class speficier, any object including Class.
  reject(t)

Release code

Removes the last List.

Puts option summary into to and returns to. Yields each line if a block is given.

to:Output destination, which must have method <<. Defaults to [].
width:Width of left side, defaults to @summary_width.
max:Maximum length allowed for left side, defaults to width - 1.
indent:Indentation, defaults to @summary_indent.

Terminates option parsing. Optional parameter arg is a string pushed back to be the first non-option argument.

Returns option summary list.

to_s()

Alias for help

Subject of on / on_head, accept / reject

Returns version string from program_name, version and release.

Version

[Validate]

ruby-doc.org is a service of James Britt and Neurogami, a Ruby application development company in Phoenix, AZ.

Documentation content on ruby-doc.org is provided by remarkable members of the Ruby community.

For more information on the Ruby programming language, visit ruby-lang.org.

Want to help improve Ruby's API docs? See Ruby Documentation Guidelines.