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 this case, only the opt, banner and version, depends, and conflicts methods will typically be called.
If it‘s necessary to instantiate this class (for more complicated argument-parsing situations), be sure to call parse to actually produce the output hash.
FLAG_TYPES | = | [:flag, :bool, :boolean] | The set of values that indicate a flag option when passed as the +:type+ parameter of opt. | |
SINGLE_ARG_TYPES | = | [:int, :integer, :string, :double, :float, :io, :date] |
The set of values that indicate a single-parameter (normal) option when
passed as the +:type+ parameter of opt.
A value of io corresponds to a readable IO resource, including a filename, URI, or the strings ‘stdin’ or ’-’. |
|
MULTI_ARG_TYPES | = | [:ints, :integers, :strings, :doubles, :floats, :ios, :dates] | The set of values that indicate a multiple-parameter option (i.e., that takes multiple space-separated values on the commandline) when passed as the +:type+ parameter of opt. | |
TYPES | = | FLAG_TYPES + SINGLE_ARG_TYPES + MULTI_ARG_TYPES | The complete set of legal values for the +:type+ parameter of opt. |
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.) |
Marks two (or more!) options as requiring each other. Only handles undirected (i.e., mutual) dependencies. Directed dependencies are better modeled with Trollop::die.
Define an option. name is the option name, a unique identifier for the option that you will use internally, which should be a symbol or a string. desc is a string description which will be displayed in help messages.
Takes the following optional arguments:
Note that there are two types of argument multiplicity: an argument can take multiple values, e.g. "—arg 1 2 3". An argument can also be allowed to occur multiple times, e.g. "—arg 1 —arg 2".
Arguments that take multiple values should have a +:type+ parameter drawn from MULTI_ARG_TYPES (e.g. +:strings+), or a +:default:+ value of an array of the correct type (e.g. [String]). The value of this argument will be an array of the parameters on the commandline.
Arguments that can occur multiple times should be marked with +:multi+ => true. The value of this argument will also be an array. In contrast with regular non-multi options, if not specified on the commandline, the default value will be [], not nil.
These two attributes can be combined (e.g. +:type+ => +:strings+, +:multi+ => true), in which case the value of the argument will be an array of arrays.
There‘s one ambiguous case to be aware of: when +:multi+: is true and a +:default+ is set to an array (of something), it‘s ambiguous whether this is a multi-value argument as well as a multi-occurrence argument. In thise case, Trollop assumes that it‘s not a multi-value argument. If you want a multi-value, multi-occurrence argument with a default value, you must specify +:type+ as well.
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, after shifting the subcommand off of ARGV.
Similar to stop_on, but stops on any unknown word when encountered (unless it is a parameter for an argument). This is useful for cases where you don‘t know the set of subcommands ahead of time, i.e., without first parsing the global options.