bool OEParseCommandLine(OEInterface& itf, int argc, char** argv, const unsigned char* requirements = NULL, unsigned int error_level = OEErrorLevel::Error);
The overall purpose of this function is to set parameters in the itf
object by parsing the command line, argc and argv. This
function returns true if it was successful and false otherwise. Errors are
reported at error_level to OEThrow
.
Parameters are entered in key-value pairs after the executable name on the command line. The key is the parameters name appendend with its instance (typically instance is just an empty string, so key is just the parameter name).
E.g.
program <parameter1 key> <parameter1 value> <parameter2 key> <parameter2 value>
The order in which the parameter key-value pairs appear on the command line is unimportant, except when the same key is specified twice in which case the second value specified is used.
E.g.
program <parameter2 key> <parameter2 value> <parameter1 key> <parameter1 value>
is the same as the preceding example.
The following special case rules also apply when parsing the command line.
program -b true -x 5
and
program -b -x 5
are equivilant, provided that -b is a boolean parameter.
keyless
values (see GetKeyless() method of
OEParameter) can be entered at the end of the command line as a value without
a key. The format is as follows
program [<key> <value>]... <keyless1 value> [<keyless2 value>]...
Any number of key-value pairs can be entered on the command line before the keyless values. Parsing from left to right the first argument on the command line not recognized as a key-value pair is assigned to the parameter with the keyless value 1. Additional arguments beyond the first are assigned to the parameter with keyless value 2, 3, 4 and so on. If the itf object does not have a parameter with the appropriate keyless value or has more than one parameter with the appropriate keyless value an error will be reported and the function will return false.
Note: There is a potential ambiguity in the command line when the last key-value pair specified before the first keyless parameter is a boolean, as in the following example.
program -b true keylessvalue
If -b is a boolean parameter true could either be the setting of -b or the
first keyless parameter. OEParseCommandLine resolves this by always assuming
that true, or any valid boolean setting (i.e., true/yes/on/t/y or
false/no/off/f/n), is a setting for the boolean parameter -b and NOT
the first keyless value.
After OEParseCommandLine parses the command line it then check that all required parameter have been set. If all required parameters have not been set and error is issued and the function returns false.
There are two types of requirements
single parameter requirements
SetRequired
method of the OEParameter
class (or !REQUIRED
field on OEConfigure
).
multi-parameter requirements
!REQUIREMENT !OPTION [[!]key [value] [[!]key [value]... !OPTION [[!]key [value] [[!]key [value]... . . . !ERROR_MSG <error message line 1> <error message line 2> . . . !END
One or more !OPTION
fields may appear in a requirement record. Each
!OPTION
field is a list of keys, optionally with values, to require.
The rules for the format of the !OPTION
line are as follows.
!OPTION
line, but is not followed
by a parameter value, then that parameter must have a valid setting
(e.g., the parameter method IsSet
must return true).
!OPTION
line followed by a value,
then that parameter must be set to the specified value.
!OPTION
line preceeded by a !
,
but is not followed by a parameter value, then that parameter must not
have a valid setting (e.g., the parameter method IsSet
must
return false).
If any !OPTION
field of a requirement record is satisfied, the entire
requirement is satisfied. No !OPTION
in the requirement record is
satisfied, the error message is sent to OEThrow
and the function
returns false.
The following is an example parameter record
!REQUIREMENT !OPTION -a 5 !OPTION !-b !OPTION -a -b !-c 1 !ERROR_MSG The command line must satisfy one of the following criteion 1) -a is set to 5 2) -b is not set 3) -a and -b are set and -c is not set to 1 !END
This requirement record requires that the settings in itf follow
the rules listed between !ERROR_MSG
and !END
. If it does
not those lines are sent to OEThrow
and the function returns false.