Qore Programming Language Reference Manual  0.9.1
Qore::GetOpt Class Reference

The GetOpt class provides an easy way to process POSIX-style command-line options in Qore scripts/programs. More...

Public Member Methods

 constructor (hash options)
 Creates the GetOpt object and sets the option hash with the single required argument. More...
 
 copy ()
 Throws an exception; objects of this class cannot be copied. More...
 
hash parse (reference< list< string >> pgm_args)
 Parses the parameter list according to the option hash passed to the constructor. More...
 
hash parse (softlist pgm_args)
 Parses the parameter list according to the option hash passed to the constructor. More...
 
hash parse2 (reference< list< string >> pgm_args)
 Parses the parameter list according to the option hash passed to the constructor. More...
 
hash parse2 (softlist pgm_args)
 Parses the parameter list according to the option hash passed to the constructor. More...
 
hash parse3 (reference< list< string >> pgm_args)
 Parses the parameter list according to the option hash passed to the constructor and displays an explanatory error message on stderr and exits the program if an error occurs. More...
 
hash parse3 (softlist pgm_args)
 Parses the parameter list according to the option hash passed to the constructor and displays an explanatory error message on stderr and exits the program if an error occurs. More...
 

Detailed Description

The GetOpt class provides an easy way to process POSIX-style command-line options in Qore scripts/programs.

Member Function Documentation

◆ constructor()

Qore::GetOpt::constructor ( hash  options)

Creates the GetOpt object and sets the option hash with the single required argument.

Parameters
optionsEach 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 key follows the following pattern:
opts[(=|:)type[modifier]]
Where the meaning of the above placeholders in italics is:
  • opts: 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.
  • (=|:)type: 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
  • modifier: "@" specifies a list, "+" an additive value (sum; must be integer or float type)
Example:
const ProgramPptions = (
"url" : "u,url=s",
"xml" : "x,xml",
"lxml" : "X,literal-xml",
"verb" : "v,verbose",
"help" : "h,help",
);
GetOpt getopt(ProgramOptions);
Exceptions
GETOPT-PARAMETER-ERRORoption key value is not a string; "_ERRORS_" used as option key
GETOPT-OPTION-ERRORlist option specified as optional; empty option key value string; multiple long options given for key; duplicate options given; invalid attributes for option; unknown modifier given for option

◆ copy()

Qore::GetOpt::copy ( )

Throws an exception; objects of this class cannot be copied.

Exceptions
GETOPT-COPY-ERRORcopying GetOpt objects is not supported

◆ parse() [1/2]

hash Qore::GetOpt::parse ( reference< list< string >>  pgm_args)

Parses the parameter list according to the option hash passed to the constructor.

All arguments parsed will be removed from the list reference passed as the sol argument, 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.

Parameters
pgm_argsThe reference should point to a list of arguments to process (normally ARGV); any argument accepted by the object will be removed from the list
Returns
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 argument passed in the list argument. The hash key "_ERRORS_" will contain any errors.
Example:
hash o = getopt.parse(\ARGV);
if (exists o."_ERRORS_") {
foreach string err in (o."_ERRORS_")
stderr.printf("%s\n", err);
exit(1);
}
See also
GetOpt::parse2() for a similar method that throws an exception instead of putting error information in the "_ERRORS_" key of the hash value returned

◆ parse() [2/2]

hash Qore::GetOpt::parse ( softlist  pgm_args)

Parses the parameter list according to the option hash passed to the constructor.

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.

Parameters
pgm_argsA list of arguments to process
Returns
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 argument passed in the list argument. The hash key "_ERRORS_" will contain any errors.
Example:
hash o = getopt.parse(ARGV);
if (exists o."_ERRORS_") {
foreach string err in (o."_ERRORS_")
stderr.printf("%s\n", err);
exit(1);
}
See also
GetOpt::parse2() for a similar method that throws an exception instead of putting error information in the "_ERRORS_" key of the hash value returned

◆ parse2() [1/2]

hash Qore::GetOpt::parse2 ( reference< list< string >>  pgm_args)

Parses the parameter list according to the option hash passed to the constructor.

If any errors are encountered, an appropriate exception will be thrown.

Parameters
pgm_argsThe reference should point to a list of arguments to process (normally ARGV); any argument accepted by the object will be removed from the list
Returns
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 argument passed in the list argument
Example:
try {
hash o = getopt.parse2(\ARGV);
}
catch (ex) {
stderr.printf("%s\n", ex.desc);
exit(1);
}
Exceptions
GETOPT-ERRORerror parsing arguments
See also
GetOpt::parse() for a similar method that puts error information in the "_ERRORS_" key of the hash value returned instead of throwing an exception

◆ parse2() [2/2]

hash Qore::GetOpt::parse2 ( softlist  pgm_args)

Parses the parameter list according to the option hash passed to the constructor.

If any errors are encountered, an appropriate exception will be thrown.

Parameters
pgm_argsA list of arguments to process
Returns
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 argument passed in the list argument
Example:
try {
hash o = getopt.parse2(ARGV);
}
catch (ex) {
stderr.printf("%s\n", ex.desc);
exit(1);
}
Exceptions
GETOPT-ERRORerror parsing arguments
See also
GetOpt::parse() for a similar method that puts error information in the "_ERRORS_" key of the hash value returned instead of throwing an exception

◆ parse3() [1/2]

hash Qore::GetOpt::parse3 ( reference< list< string >>  pgm_args)

Parses the parameter list according to the option hash passed to the constructor and displays an explanatory error message on stderr and exits the program if an error occurs.

Restrictions:
Qore::PO_NO_PROCESS_CONTROL
Parameters
pgm_argsThe reference should point to a list of arguments to process (normally ARGV); any argument accepted by the object will be removed from the list
Returns
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 argument passed in the list argument
Example:
hash o = getopt.parse3(\ARGV);
See also
  • GetOpt::parse() for a similar method that puts error information in the "_ERRORS_" key of the hash value returned
  • GetOpt::parse2() for a similar method that throws exceptions instead of exiting the program

◆ parse3() [2/2]

hash Qore::GetOpt::parse3 ( softlist  pgm_args)

Parses the parameter list according to the option hash passed to the constructor and displays an explanatory error message on stderr and exits the program if an error occurs.

Restrictions:
Qore::PO_NO_PROCESS_CONTROL
Parameters
pgm_argsA list of arguments to process
Returns
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 argument passed in the list argument
Example:
hash o = getopt.parse3(ARGV);
Exceptions
GETOPT-ERRORerror parsing arguments
See also
  • GetOpt::parse() for a similar method that puts error information in the "_ERRORS_" key of the hash value returned
  • GetOpt::parse2() for a similar method that throws exceptions instead of exiting the program