The GetOpt class provides an easy way to process POSIX-style command-line options in Qore scripts/programs.
Table 4.228. GetOpt Method Overview
Method | Except? | Description |
---|---|---|
Y | Creates the GetOpt object with the option hash passed. | |
N | Destroys the object. | |
Y | Throws an exception; objects of this class cannot be copied. | |
N | Parses the argument list passed and retuns a hash of the results. |
Creates the GetOpt object and sets the option hash with the single required argument.
new GetOpt(option_hash
)
const program_options =
( "url" : "url,u=s",
"xml" : "xml,x",
"lxml" : "literal-xml,X",
"verb" : "verbose,v",
"help" : "help,h" );
$getopt = new GetOpt(program_options);
Table 4.229. Arguments for GetOpt::constructor()
Argument | Type | Description |
---|---|---|
| Hash | Each key defines the key value for the return hash if any arguments are given corresponding to the string value of the key. |
The string value of each hash follows the following pattern:
opts
[=|:type
[modifier
]]
Table 4.230. Option Hash Value String
Component | Description |
---|---|
| At least one short option and/or a long option name; if both are present, then they must be separated by a comma. The short option must be a single character. |
[=: | if "=" is used, then the option takes a mandatory argument, if ":" is used, then the argument is optional. Types are specified as follows: s=string, i=integer, f=float, d=date, b=boolean |
| @ specifies a list, + an additive value (sum; must be integer or float type) |
Table 4.231. Return Values for GetOpt::constructor()
Return Type | Description |
---|---|
Object | The GetOpt object created. |
Table 4.232. Exceptions Thrown by GetOpt::constructor()
err | desc |
---|---|
| There was a syntax or format error in the option specification. |
Destroys the GetOpt object.
delete lvalue
delete $getopt;
Throws an exception; objects of this class cannot be copied.
Table 4.233. Arguments for GetOpt::copy()
Argument | Type | Description |
---|---|---|
n/a | n/a | This method takes no arguments. |
Table 4.234. Return Values for GetOpt::copy()
Return Type | Description |
---|---|
n/a | This method returns no value because it throws an exception. |
Table 4.235. Exceptions Thrown by GetOpt::copy()
err | desc |
---|---|
| Objects of this class cannot be copied. |
Parses the list of parameters according to the option hash passed to the constructor. If a reference to a list is passed to this function, then all arguments parsed will be removed from the list, leaving only unparsed arguments (for example, file names).
GetOpt::parse(list_reference
)
$o = $getopt.parse(\$ARGV);
Table 4.236. Arguments for GetOpt::parse()
Argument | Type | Description |
---|---|---|
| List Reference | The entire command line to process (ex: $ARGV). |
Table 4.237. Return Values for GetOpt::parse()
Return Type | Description |
---|---|
Hash | A hash keyed by option names (as given in the hash to the GetOpt constructor), where each key's value is the value of the arguments passed in the list argument. The hash key "_ERRORS_" will contain any errors. |