Class Trollop::Parser
In: lib/trollop.rb
Parent: Object

The commandline parser. In typical usage, the methods in this class will be handled internally by Trollop#options, in which case only the methods opt, banner and version, depends, and conflicts will typically be called.

Methods

banner   conflicts   depends   educate   new   opt   stop_on   stop_on_unknown   text   version  

Constants

FLAG_TYPES = [:flag, :bool, :boolean]   The set of values that indicate a flag type of option when one of the values is given to the :type parameter to opt.
SINGLE_ARG_TYPES = [:int, :integer, :string, :double, :float]   The set of values that indicate an option that takes a single parameter when one of the values is given to the :type parameter to opt.
MULTI_ARG_TYPES = [:ints, :integers, :strings, :doubles, :floats]   The set of values that indicate an option that takes multiple parameters when one of the values is given to the :type parameter to opt.
TYPES = FLAG_TYPES + SINGLE_ARG_TYPES + MULTI_ARG_TYPES   The set of values specifiable as the :type parameter to opt.

Attributes

leftovers  [R]  The values from the commandline that were not interpreted by parse.
specs  [R]  The complete configuration hashes for each option. (Mainly useful for testing.)

Public Class methods

Initializes the parser, and instance-evaluates any block given.

Public Instance methods

Adds text to the help display.

Marks two (or more!) options as conflicting.

Marks two (or more!) options as requiring each other. Only handles undirected (i.e., mutual) dependencies. Directed dependencies are better modeled with Trollop::die.

Print the help message to ‘stream’.

Add an option. ‘name’ is the argument name, a unique identifier for the option that you will use internally. ‘desc’ a string description which will be displayed in help messages. Takes the following optional arguments:

  • :long: Specify the long form of the argument, i.e. the form with two dashes. If unspecified, will be automatically derived based on the argument name.
  • :short: Specify the short form of the argument, i.e. the form with one dash. If unspecified, will be automatically derived based on the argument name.
  • :type: Require that the argument take a parameter or parameters of type ‘type’. For a single parameter, the value can be a member of the SINGLE_ARG_TYPES constant or a corresponding class (e.g. Integer for :int). For multiple parameters, the value can be a member of the MULTI_ARG_TYPES constant. If unset, the default argument type is :flag, meaning that the argument does not take a parameter. The specification of :type is not necessary if :default is given.
  • :default: Set the default value for an argument. Without a default value, the hash returned by parse (and thus Trollop#options) will not contain the argument unless it is given on the commandline. The argument type is derived automatically from the class of the default value given, if any. Specifying a :flag argument on the commandline whose default value is true will change its value to false.
  • :required: If set to true, the argument must be provided on the commandline.
  • :multi: If set to true, allows multiple instances of the option. Otherwise, only a single instance of the option is allowed.

Defines a set of words which cause parsing to terminate when encountered, such that any options to the left of the word are parsed as usual, and options to the right of the word are left intact.

A typical use case would be for subcommand support, where these would be set to the list of subcommands. A subsequent Trollop invocation would then be used to parse subcommand options.

Similar to stop_on, but stops on any unknown word when encountered (unless it is a parameter for an argument).

text(s;)

Alias for banner

Sets the version string. If set, the user can request the version on the commandline. Should be of the form "<program name> <version number>".

[Validate]