The GetOpt class provides an easy way to process POSIX-style command-line options in Qore scripts/programs.
Table 4.219. 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; if there are any errors, they are returned in the |
| N | Parses the argument list passed and retuns a hash of the results; if there are any errors, an exception is thrown. |
Creates the GetOpt object and sets the option hash with the single required argument.
GetOpt::constructor(hash$options)
const program_options = ( "url" : "url,u=s", "xml" : "xml,x", "lxml" : "literal-xml,X", "verb" : "verbose,v", "help" : "help,h" ); my GetOpt $getopt(program_options);
Table 4.220. Arguments for GetOpt::constructor()
Argument | Description |
|---|---|
| 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.221. 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.222. Exceptions Thrown by GetOpt::constructor()
err | desc |
|---|---|
| There was a syntax or format error in the option specification. |
Throws an exception; objects of this class cannot be copied.
Table 4.223. 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).
If any errors are encountered, the return value hash will have a key "_ERRORS_" giving a list of error messages pertaining to the options parsed.
See GetOpt::parse2() for a similar method that throws an exception instead of putting error information in the _ERRORS_ key of the hash value returned.
hashGetOpt::parse(reference$list_ref)
hashGetOpt::parse(list$list)
my hash $o = $getopt.parse(\$ARGV); if (exists $o."_ERRORS_") { stderr.printf("%s\n", $o."_ERRORS_"[0]); exit(1); }
Table 4.225. Return Values for GetOpt::parse()
Return Type | Description |
|---|---|
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 |
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).
If any errors are encountered, an appropriate exception will be thrown.
See GetOpt::parse() for a similar method that puts error information in the "_ERRORS_" key of the hash value returned instead of throwing an exception.
hashGetOpt::parse2(reference$list_ref)
hashGetOpt::parse2(list$list)
try { my hash $o = $getopt.parse2(\$ARGV); } catch ($ex) { stderr.printf("%s\n", $ex.desc); exit(1); }
Table 4.227. Return Values for GetOpt::parse2()
Return Type | Description |
|---|---|
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. If any errors are encountered processing the arguments, an appropriate exception will be thrown. |
Table 4.228. Exceptions Thrown by GetOpt::parse2()
err | desc |
|---|---|
| The description varies according to the error encountered. |
There are no comments yet