4.6. GetOpt Class

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

GetOpt::constructor()

Y

Creates the GetOpt object with the option hash passed.

GetOpt::destructor()

N

Destroys the object.

GetOpt::copy()

Y

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

GetOpt::parse()

N

Parses the argument list passed and retuns a hash of the results.


4.6.1. GetOpt::constructor()

Synopsis

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

Usage
new GetOpt(option_hash)
Example
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

option_hash

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

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)


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

GETOPT-OPTION-ERROR

There was a syntax or format error in the option specification.


4.6.2. GetOpt::destructor()

Synopsis

Destroys the GetOpt object.

Usage
delete lvalue
Example
delete $getopt;

4.6.3. GetOpt::copy()

Synopsis

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

GETOPT-COPY-ERROR

Objects of this class cannot be copied.


4.6.4. GetOpt::parse()

Synopsis

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).

Usage
GetOpt::parse(list_reference)
Example
$o = $getopt.parse(\$ARGV);

Table 4.236. Arguments for GetOpt::parse()

Argument

Type

Description

list_reference

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.