\Phool\OptionsGetopt

Command-line options parsing class.

Summary

Methods
Properties
Constants
getopt2()
getopt()
readPHPArgv()
No public properties found
No constants found
No protected methods found
No protected properties found
N/A
doGetopt()
_parseShortOption()
_parseLongOption()
No private properties found
N/A

Methods

getopt2()

getopt2(array  $args, string  $short_options, array  $long_options = null) : array

Parses the command-line options.

The first parameter to this function should be the list of command-line arguments without the leading reference to the running program.

The second parameter is a string of allowed short options. Each of the option letters can be followed by a colon ':' to specify that the option requires an argument, or a double colon '::' to specify that the option takes an optional argument.

The third argument is an optional array of allowed long options. The leading '--' should not be included in the option name. Options that require an argument should be followed by '=', and options that take an option argument should be followed by '=='.

The return value is an array of two elements: the list of parsed options and the list of non-option command-line arguments. Each entry in the list of parsed options is a pair of elements - the first one specifies the option, and the second one specifies the option argument, if there was one.

Long and short options can be mixed.

Most of the semantics of this function are based on GNU getopt_long().

Changes from original version:

- switch to PHP 5 object syntax
- make independant from PEAR error handling using exceptions

Parameters

array $args

an array of command-line arguments

string $short_options

specifies the list of allowed short options

array $long_options

specifies the list of allowed long options

Throws

\Phool\Options\Exception

Returns

array —

two-element array containing the list of parsed options and the non-option arguments

getopt()

getopt(  $args,   $short_options,   $long_options = null) 

This function expects $args to start with the script name (POSIX-style).

Preserved for backwards compatibility.

Parameters

$args
$short_options
$long_options

readPHPArgv()

readPHPArgv() : array

Safely read the $argv PHP array across different PHP configurations.

Will take care on register_globals and register_argc_argv ini directives

Throws

\Phool\Options\Exception

if not registered

Returns

array —

the $argv PHP array

doGetopt()

doGetopt(  $version,   $args,   $short_options,   $long_options = null) 

The actual implementation of the argument parsing code.

Parameters

$version
$args
$short_options
$long_options

_parseShortOption()

_parseShortOption(  $arg,   $short_options,   $opts,   $args) 

Parameters

$arg
$short_options
$opts
$args

_parseLongOption()

_parseLongOption(  $arg,   $long_options,   $opts,   $args) 

Parameters

$arg
$long_options
$opts
$args