ILIAS  Release_4_2_x_branch Revision 61807
 All Data Structures Namespaces Files Functions Variables Groups Pages
Console_Getopt Class Reference
+ Collaboration diagram for Console_Getopt:

Public Member Functions

 getopt2 ($args, $short_options, $long_options=null, $skip_unknown=false)
 Parses the command-line options.
 getopt ($args, $short_options, $long_options=null, $skip_unknown=false)
 This function expects $args to start with the script name (POSIX-style).
 doGetopt ($version, $args, $short_options, $long_options=null, $skip_unknown=false)
 The actual implementation of the argument parsing code.
 _parseShortOption ($arg, $short_options, &$opts, &$args, $skip_unknown)
 Parse short option.
 _isShortOpt ($arg)
 Checks if an argument is a short option.
 _isLongOpt ($arg)
 Checks if an argument is a long option.
 _parseLongOption ($arg, $long_options, &$opts, &$args, $skip_unknown)
 Parse long option.
 readPHPArgv ()
 Safely read the $argv PHP array across different PHP configurations.

Detailed Description

Definition at line 35 of file Getopt.php.

Member Function Documentation

Console_Getopt::_isLongOpt (   $arg)

Checks if an argument is a long option.

Parameters
string$argArgument to check

private

Returns
bool

Definition at line 258 of file Getopt.php.

Referenced by _parseLongOption(), and _parseShortOption().

{
return strlen($arg) > 2 && $arg[0] == '-' && $arg[1] == '-' &&
preg_match('/[a-zA-Z]+$/', substr($arg, 2));
}

+ Here is the caller graph for this function:

Console_Getopt::_isShortOpt (   $arg)

Checks if an argument is a short option.

Parameters
string$argArgument to check

private

Returns
bool

Definition at line 244 of file Getopt.php.

Referenced by _parseLongOption(), and _parseShortOption().

{
return strlen($arg) == 2 && $arg[0] == '-'
&& preg_match('/[a-zA-Z]/', $arg[1]);
}

+ Here is the caller graph for this function:

Console_Getopt::_parseLongOption (   $arg,
  $long_options,
$opts,
$args,
  $skip_unknown 
)

Parse long option.

Parameters
string$argArgument
string[]$long_options Available long options
string[][]&$opts
string[]&$args

private

Returns
void|PEAR_Error

Definition at line 275 of file Getopt.php.

References _isLongOpt(), _isShortOpt(), and PEAR\raiseError().

Referenced by doGetopt().

{
@list($opt, $opt_arg) = explode('=', $arg, 2);
$opt_len = strlen($opt);
for ($i = 0; $i < count($long_options); $i++) {
$long_opt = $long_options[$i];
$opt_start = substr($long_opt, 0, $opt_len);
$long_opt_name = str_replace('=', '', $long_opt);
/* Option doesn't match. Go on to the next one. */
if ($long_opt_name != $opt) {
continue;
}
$opt_rest = substr($long_opt, $opt_len);
/* Check that the options uniquely matches one of the allowed
options. */
if ($i + 1 < count($long_options)) {
$next_option_rest = substr($long_options[$i + 1], $opt_len);
} else {
$next_option_rest = '';
}
if ($opt_rest != '' && $opt{0} != '=' &&
$i + 1 < count($long_options) &&
$opt == substr($long_options[$i+1], 0, $opt_len) &&
$next_option_rest != '' &&
$next_option_rest{0} != '=') {
$msg = "Console_Getopt: option --$opt is ambiguous";
return PEAR::raiseError($msg);
}
if (substr($long_opt, -1) == '=') {
if (substr($long_opt, -2) != '==') {
/* Long option requires an argument.
Take the next argument if one wasn't specified. */;
if (!strlen($opt_arg) && !(list(, $opt_arg) = each($args))) {
$msg = "Console_Getopt: option requires an argument --$opt";
return PEAR::raiseError($msg);
}
$msg = "Console_Getopt: option requires an argument --$opt";
return PEAR::raiseError($msg);
}
}
} else if ($opt_arg) {
$msg = "Console_Getopt: option --$opt doesn't allow an argument";
return PEAR::raiseError($msg);
}
$opts[] = array('--' . $opt, $opt_arg);
return;
}
if ($skip_unknown === true) {
return;
}
return PEAR::raiseError("Console_Getopt: unrecognized option --$opt");
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Console_Getopt::_parseShortOption (   $arg,
  $short_options,
$opts,
$args,
  $skip_unknown 
)

Parse short option.

Parameters
string$argArgument
string[]$short_options Available short options
string[][]&$opts
string[]&$args
boolean$skip_unknownsuppresses Console_Getopt: unrecognized option

private

Returns
void

Definition at line 188 of file Getopt.php.

References _isLongOpt(), _isShortOpt(), and PEAR\raiseError().

Referenced by doGetopt().

{
for ($i = 0; $i < strlen($arg); $i++) {
$opt = $arg{$i};
$opt_arg = null;
/* Try to find the short option in the specifier string. */
if (($spec = strstr($short_options, $opt)) === false || $arg{$i} == ':') {
if ($skip_unknown === true) {
break;
}
$msg = "Console_Getopt: unrecognized option -- $opt";
return PEAR::raiseError($msg);
}
if (strlen($spec) > 1 && $spec{1} == ':') {
if (strlen($spec) > 2 && $spec{2} == ':') {
if ($i + 1 < strlen($arg)) {
/* Option takes an optional argument. Use the remainder of
the arg string if there is anything left. */
$opts[] = array($opt, substr($arg, $i + 1));
break;
}
} else {
/* Option requires an argument. Use the remainder of the arg
string if there is anything left. */
if ($i + 1 < strlen($arg)) {
$opts[] = array($opt, substr($arg, $i + 1));
break;
} else if (list(, $opt_arg) = each($args)) {
/* Else use the next argument. */;
$msg = "option requires an argument --$opt";
return PEAR::raiseError("Console_Getopt:" . $msg);
}
} else {
$msg = "option requires an argument --$opt";
return PEAR::raiseError("Console_Getopt:" . $msg);
}
}
}
$opts[] = array($opt, $opt_arg);
}
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Console_Getopt::doGetopt (   $version,
  $args,
  $short_options,
  $long_options = null,
  $skip_unknown = false 
)

The actual implementation of the argument parsing code.

Parameters
int$versionVersion to use
array$argsan array of command-line arguments
string$short_optionsspecifies the list of allowed short options
array$long_optionsspecifies the list of allowed long options
boolean$skip_unknownsuppresses Console_Getopt: unrecognized option
Returns
array

Definition at line 106 of file Getopt.php.

References _parseLongOption(), _parseShortOption(), and PEAR\isError().

Referenced by getopt(), and getopt2().

{
// in case you pass directly readPHPArgv() as the first arg
if (PEAR::isError($args)) {
return $args;
}
if (empty($args)) {
return array(array(), array());
}
$non_opts = $opts = array();
settype($args, 'array');
if ($long_options) {
sort($long_options);
}
/*
* Preserve backwards compatibility with callers that relied on
* erroneous POSIX fix.
*/
if ($version < 2) {
if (isset($args[0]{0}) && $args[0]{0} != '-') {
array_shift($args);
}
}
reset($args);
while (list($i, $arg) = each($args)) {
/* The special element '--' means explicit end of
options. Treat the rest of the arguments as non-options
and end the loop. */
if ($arg == '--') {
$non_opts = array_merge($non_opts, array_slice($args, $i + 1));
break;
}
if ($arg{0} != '-' || (strlen($arg) > 1 && $arg{1} == '-' && !$long_options)) {
$non_opts = array_merge($non_opts, array_slice($args, $i));
break;
} elseif (strlen($arg) > 1 && $arg{1} == '-') {
$error = Console_Getopt::_parseLongOption(substr($arg, 2),
$long_options,
$opts,
$args,
$skip_unknown);
if (PEAR::isError($error)) {
return $error;
}
} elseif ($arg == '-') {
// - is stdin
$non_opts = array_merge($non_opts, array_slice($args, $i));
break;
} else {
$error = Console_Getopt::_parseShortOption(substr($arg, 1),
$short_options,
$opts,
$args,
$skip_unknown);
if (PEAR::isError($error)) {
return $error;
}
}
}
return array($opts, $non_opts);
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Console_Getopt::getopt (   $args,
  $short_options,
  $long_options = null,
  $skip_unknown = false 
)

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

Preserved for backwards compatibility.

Parameters
array$argsan array of command-line arguments
string$short_optionsspecifies the list of allowed short options
array$long_optionsspecifies the list of allowed long options
See Also
getopt2()
Returns
array two-element array containing the list of parsed options and the non-option arguments

Definition at line 90 of file Getopt.php.

References doGetopt().

{
return Console_Getopt::doGetopt(1, $args, $short_options, $long_options, $skip_unknown);
}

+ Here is the call graph for this function:

Console_Getopt::getopt2 (   $args,
  $short_options,
  $long_options = null,
  $skip_unknown = false 
)

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

Parameters
array$argsan array of command-line arguments
string$short_optionsspecifies the list of allowed short options
array$long_optionsspecifies the list of allowed long options
boolean$skip_unknownsuppresses Console_Getopt: unrecognized option
Returns
array two-element array containing the list of parsed options and the non-option arguments public

Definition at line 73 of file Getopt.php.

References doGetopt().

Referenced by System\_parseArgs().

{
return Console_Getopt::doGetopt(2, $args, $short_options, $long_options, $skip_unknown);
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Console_Getopt::readPHPArgv ( )

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

Will take care on register_globals and register_argc_argv ini directives

public

Returns
mixed the $argv PHP array or PEAR error if not registered

Definition at line 350 of file Getopt.php.

References $GLOBALS, and PEAR\raiseError().

{
global $argv;
if (!is_array($argv)) {
if (!@is_array($_SERVER['argv'])) {
if (!@is_array($GLOBALS['HTTP_SERVER_VARS']['argv'])) {
$msg = "Could not read cmd args (register_argc_argv=Off?)";
return PEAR::raiseError("Console_Getopt: " . $msg);
}
return $GLOBALS['HTTP_SERVER_VARS']['argv'];
}
return $_SERVER['argv'];
}
return $argv;
}

+ Here is the call graph for this function:


The documentation for this class was generated from the following file: