Menu

Option_String

Ray

Config - The Option String

In order for the parser to know what options to expect and what arguments they should have, one passes so called option string to the constructor of the Config class. Generally speaking, the option string has the following regexp pattern:

([^-: \t]:?:?)+

All white space in the option string is ignored, so if you find it useful, you can separate the options into groups by spaces.
An option is defined by its character, which can be any printable non-WS character except a minus (-) and a colon (:). This character, preceded by a minus, is then sought by the parser when parsing the command line arguments.
The option character in the option string can be followed by one or two colons. When followed by a single colon, the option is required to have an argument value if seen on the command line. If an argument-requiring option is seen on the command line, but followed by no argument, the parseArguments() method shall return false (see Parsing). Option character followed by a double colon denotes an option with optional argument. Such option can be left without any argument value. Note however that this could turn out to be tricky. For example, assume you have a "-m::" optional-argument option, and you want to specify this option On without argument value, but specify a non-optional argument. Then the command:

program -m non-optional

will not do what you expect. the "non-optional" string will become option m's value. You would have to change the order of the arguments to

program non-optional -m

because in contrast to the POSIX getopt, CmdConfig does not do any argument reordering.

Specifying a boolean option
A boolean option is one that does not permit and accept any arguments. It is simply specified by including its discerning character in the option string. For example, an option string of

"bf ad vg"

specifies only boolean options, which, given on the command line, would look like -b, -f, -a, -d, -v or -g, respectively.

Specifying a argument-requiring option
As specified above, an argument requiring option's discerning character must be followed by a single colon. For example,

"ab:cd:"

specifies two boolean options (-a and -c) and two argument-requiring options -b and -d, thus you would need to supply at least one argument to each if you switch them on:

-b arg_to_b -d arg_to_d

Specifying an argument-optional option
Options, whose discerning characters in the option string are followed by a double colon, can be followed by an argument value, but that's not required. You can switch them on without an argument, just pay attention to the tricky part as described above. For example,

"ab:c::"

specifies that -a is not followed by any argument, -b must be followed by an argument when it is seen on the command line, and -c can be followed by an argument.


Related

Wiki: Overview
Wiki: Usage_Phases