Qore Programming Language Reference Manual 1.18.1
Loading...
Searching...
No Matches
Qore::GetOpt Class Reference

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

#include <QC_GetOpt.dox.h>

Public Member Methods

 constructor (hash< auto > 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< auto > parse (reference< list< string > > pgm_args)
 Parses the parameter list according to the option hash passed to the constructor. More...
 
hash< auto > parse (softlist< auto > pgm_args)
 Parses the parameter list according to the option hash passed to the constructor. More...
 
hash< auto > parse2 (reference< list< string > > pgm_args)
 Parses the parameter list according to the option hash passed to the constructor. More...
 
hash< auto > parse2 (softlist< auto > pgm_args)
 Parses the parameter list according to the option hash passed to the constructor. More...
 
hash< auto > 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< auto > parse3 (softlist< auto > 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...
 

Static Public Member Methods

static hash< auto > parse (hash< auto > opts, reference< list< string > > pgm_args)
 Parses the given options and returns a hash of the parsed options. More...
 
static hash< auto > parseEx (hash< auto > opts, reference< list< string > > pgm_args)
 Parses the given options and returns a hash of the parsed options and throws and exception if there are any errors. More...
 
static hash< auto > parseExit (hash< auto > opts, reference< list< string > > pgm_args)
 Parses the given options and returns a hash of the parsed options; prints out an error message and exits the program if there are any errors. 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< auto >  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/3]

static hash< auto > Qore::GetOpt::parse ( hash< auto >  opts,
reference< list< string > >  pgm_args 
)
static

Parses the given options and returns a hash of the parsed options.

Example:
const Opts = {
"url" : "u,url=s",
"xml" : "x,xml",
"lxml" : "X,literal-xml",
"verb" : "v,verbose",
"help" : "h,help",
};
hash<auto> o = GetOpt::parse(Opts, \ARGV);
if (exists o."_ERRORS_") {
foreach string err in (o."_ERRORS_")
stderr.printf("%s\n", err);
exit(1);
}
Parameters
optsa hash where 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 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)
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.

All arguments parsed will be removed from pgm_args, 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 also

◆ parse() [2/3]

hash< auto > 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 sole 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<auto> 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() [3/3]

hash< auto > Qore::GetOpt::parse ( softlist< auto >  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<auto> o = getopt.parse(ARGV);
if (o._ERRORS_) {
map stderr.printf("%s\n", $1), o._ERRORS_;
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< auto > 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<auto> o = getopt.parse2(\ARGV);
} catch (hash<ExceptionInfo> 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< auto > Qore::GetOpt::parse2 ( softlist< auto >  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<auto> o = getopt.parse2(ARGV);
} catch (hash<ExceptionInfo> 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< auto > 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<auto> 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< auto > Qore::GetOpt::parse3 ( softlist< auto >  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<auto> 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

◆ parseEx()

static hash< auto > Qore::GetOpt::parseEx ( hash< auto >  opts,
reference< list< string > >  pgm_args 
)
static

Parses the given options and returns a hash of the parsed options and throws and exception if there are any errors.

Example:
const Opts = {
"url" : "u,url=s",
"xml" : "x,xml",
"lxml" : "X,literal-xml",
"verb" : "v,verbose",
"help" : "h,help",
};
hash<auto> o = GetOpt::parseEx(Opts, \ARGV);
Parameters
optsa hash where 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 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)
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. An exception is thrown if there are any errors.

All arguments parsed will be removed from pgm_args, leaving only unparsed arguments (for example, file names).

If any errors are encountered, a GETOPT-ERROR exception is thrown.

Exceptions
GETOPT-ERRORerror parsing arguments
See also

◆ parseExit()

static hash< auto > Qore::GetOpt::parseExit ( hash< auto >  opts,
reference< list< string > >  pgm_args 
)
static

Parses the given options and returns a hash of the parsed options; prints out an error message and exits the program if there are any errors.

Restrictions:
Qore::PO_NO_PROCESS_CONTROL
Example:
const Opts = {
"url" : "u,url=s",
"xml" : "x,xml",
"lxml" : "X,literal-xml",
"verb" : "v,verbose",
"help" : "h,help",
};
hash<auto> o = GetOpt::parseExit(Opts, \ARGV);
Parameters
optsa hash where 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 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)
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. Prints out an error message and exits the program if there are any errors.

All arguments parsed will be removed from pgm_args, leaving only unparsed arguments (for example, file names).

See also