Menu

Parsing

Ray

Config Argument Parsing

Once a Config is instantiated (see Instantiation), it is ready to parse a command-line arguments array, most typically the one passed to the method main() as its argument. To achieve that, one calls the parseArguments() method:

config.parseArguments("-a", "-bonce", "-cd", "d_arg", "non-opt");

The method returns true if all argument-requiring options have been specified with at least one argument value, false otherwise. The side effect of this is that if this method returned true and for an argument-requiring option opt the config.isOn(opt) returns true, you can be sure that config.getOptionArgs(opt) is not empty.

Parsing In Detail
Options on the command line can be grouped, but the parsing process differs slightly but significantly for the three types of options. As a consequence, one must be aware of the following:
- When a group of options is scanned, the first argument-requiring option ends the scan; the rest of the group is considered its argument value.
- Any string not beginning with a minus (-) following a boolean option is collected as non-option argument
- In a group of options, if a argument-optional is encountered, it is only switched on, but considered argumentless. By consequence, the scanning of the group continues until the end or until an argument-mandatory option is found.
- As a consequence, argument-optional options to remain argumentless must be specified either in a group followed by at least one more option, or at the end (after any non-option arguments). Whenever an argument-optional option is encountered before a string not beginning with a minus sign, that string is collected as the option's value.

Note that a sole minus sign (-) is considered an argument value or non-option argument. This allows to keep the habit of specifying standard input or output by a minus, e.g. -f -.
Moreover, a double minus (--) ends the option recognition. After this argument is encountered the parser considers all the remaining strings to be non-option arguments regardless of whether the start by a minus sign.


Related

Wiki: Overview
Wiki: Usage_Phases

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.