ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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. More...
 
 getopt ($args, $short_options, $long_options=null, $skip_unknown=false)
 This function expects $args to start with the script name (POSIX-style). More...
 
 doGetopt ($version, $args, $short_options, $long_options=null, $skip_unknown=false)
 The actual implementation of the argument parsing code. More...
 
 _parseShortOption ($arg, $short_options, &$opts, &$args, $skip_unknown)
 Parse short option. More...
 
 _isShortOpt ($arg)
 Checks if an argument is a short option. More...
 
 _isLongOpt ($arg)
 Checks if an argument is a long option. More...
 
 _parseLongOption ($arg, $long_options, &$opts, &$args, $skip_unknown)
 Parse long option. More...
 
 readPHPArgv ()
 Safely read the $argv PHP array across different PHP configurations. More...
 

Detailed Description

Definition at line 35 of file Getopt.php.

Member Function Documentation

◆ _isLongOpt()

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

259  {
260  return strlen($arg) > 2 && $arg[0] == '-' && $arg[1] == '-' &&
261  preg_match('/[a-zA-Z]+$/', substr($arg, 2));
262  }
+ Here is the caller graph for this function:

◆ _isShortOpt()

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

245  {
246  return strlen($arg) == 2 && $arg[0] == '-'
247  && preg_match('/[a-zA-Z]/', $arg[1]);
248  }
+ Here is the caller graph for this function:

◆ _parseLongOption()

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

276  {
277  @list($opt, $opt_arg) = explode('=', $arg, 2);
278 
279  $opt_len = strlen($opt);
280 
281  for ($i = 0; $i < count($long_options); $i++) {
282  $long_opt = $long_options[$i];
283  $opt_start = substr($long_opt, 0, $opt_len);
284 
285  $long_opt_name = str_replace('=', '', $long_opt);
286 
287  /* Option doesn't match. Go on to the next one. */
288  if ($long_opt_name != $opt) {
289  continue;
290  }
291 
292  $opt_rest = substr($long_opt, $opt_len);
293 
294  /* Check that the options uniquely matches one of the allowed
295  options. */
296  if ($i + 1 < count($long_options)) {
297  $next_option_rest = substr($long_options[$i + 1], $opt_len);
298  } else {
299  $next_option_rest = '';
300  }
301 
302  if ($opt_rest != '' && $opt{0} != '=' &&
303  $i + 1 < count($long_options) &&
304  $opt == substr($long_options[$i+1], 0, $opt_len) &&
305  $next_option_rest != '' &&
306  $next_option_rest{0} != '=') {
307 
308  $msg = "Console_Getopt: option --$opt is ambiguous";
309  return PEAR::raiseError($msg);
310  }
311 
312  if (substr($long_opt, -1) == '=') {
313  if (substr($long_opt, -2) != '==') {
314  /* Long option requires an argument.
315  Take the next argument if one wasn't specified. */;
316  if (!strlen($opt_arg) && !(list(, $opt_arg) = each($args))) {
317  $msg = "Console_Getopt: option requires an argument --$opt";
318  return PEAR::raiseError($msg);
319  }
320 
321  if (Console_Getopt::_isShortOpt($opt_arg)
322  || Console_Getopt::_isLongOpt($opt_arg)) {
323  $msg = "Console_Getopt: option requires an argument --$opt";
324  return PEAR::raiseError($msg);
325  }
326  }
327  } else if ($opt_arg) {
328  $msg = "Console_Getopt: option --$opt doesn't allow an argument";
329  return PEAR::raiseError($msg);
330  }
331 
332  $opts[] = array('--' . $opt, $opt_arg);
333  return;
334  }
335 
336  if ($skip_unknown === true) {
337  return;
338  }
339 
340  return PEAR::raiseError("Console_Getopt: unrecognized option --$opt");
341  }
_isShortOpt($arg)
Checks if an argument is a short option.
Definition: Getopt.php:244
_isLongOpt($arg)
Checks if an argument is a long option.
Definition: Getopt.php:258
& raiseError($message=null, $code=null, $mode=null, $options=null, $userinfo=null, $error_class=null, $skipmsg=false)
This method is a wrapper that returns an instance of the configured error class with this object&#39;s de...
Definition: PEAR.php:524
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _parseShortOption()

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

189  {
190  for ($i = 0; $i < strlen($arg); $i++) {
191  $opt = $arg{$i};
192  $opt_arg = null;
193 
194  /* Try to find the short option in the specifier string. */
195  if (($spec = strstr($short_options, $opt)) === false || $arg{$i} == ':') {
196  if ($skip_unknown === true) {
197  break;
198  }
199 
200  $msg = "Console_Getopt: unrecognized option -- $opt";
201  return PEAR::raiseError($msg);
202  }
203 
204  if (strlen($spec) > 1 && $spec{1} == ':') {
205  if (strlen($spec) > 2 && $spec{2} == ':') {
206  if ($i + 1 < strlen($arg)) {
207  /* Option takes an optional argument. Use the remainder of
208  the arg string if there is anything left. */
209  $opts[] = array($opt, substr($arg, $i + 1));
210  break;
211  }
212  } else {
213  /* Option requires an argument. Use the remainder of the arg
214  string if there is anything left. */
215  if ($i + 1 < strlen($arg)) {
216  $opts[] = array($opt, substr($arg, $i + 1));
217  break;
218  } else if (list(, $opt_arg) = each($args)) {
219  /* Else use the next argument. */;
220  if (Console_Getopt::_isShortOpt($opt_arg)
221  || Console_Getopt::_isLongOpt($opt_arg)) {
222  $msg = "option requires an argument --$opt";
223  return PEAR::raiseError("Console_Getopt:" . $msg);
224  }
225  } else {
226  $msg = "option requires an argument --$opt";
227  return PEAR::raiseError("Console_Getopt:" . $msg);
228  }
229  }
230  }
231 
232  $opts[] = array($opt, $opt_arg);
233  }
234  }
_isShortOpt($arg)
Checks if an argument is a short option.
Definition: Getopt.php:244
_isLongOpt($arg)
Checks if an argument is a long option.
Definition: Getopt.php:258
& raiseError($message=null, $code=null, $mode=null, $options=null, $userinfo=null, $error_class=null, $skipmsg=false)
This method is a wrapper that returns an instance of the configured error class with this object&#39;s de...
Definition: PEAR.php:524
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ doGetopt()

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

107  {
108  // in case you pass directly readPHPArgv() as the first arg
109  if (PEAR::isError($args)) {
110  return $args;
111  }
112 
113  if (empty($args)) {
114  return array(array(), array());
115  }
116 
117  $non_opts = $opts = array();
118 
119  settype($args, 'array');
120 
121  if ($long_options) {
122  sort($long_options);
123  }
124 
125  /*
126  * Preserve backwards compatibility with callers that relied on
127  * erroneous POSIX fix.
128  */
129  if ($version < 2) {
130  if (isset($args[0]{0}) && $args[0]{0} != '-') {
131  array_shift($args);
132  }
133  }
134 
135  reset($args);
136  while (list($i, $arg) = each($args)) {
137  /* The special element '--' means explicit end of
138  options. Treat the rest of the arguments as non-options
139  and end the loop. */
140  if ($arg == '--') {
141  $non_opts = array_merge($non_opts, array_slice($args, $i + 1));
142  break;
143  }
144 
145  if ($arg{0} != '-' || (strlen($arg) > 1 && $arg{1} == '-' && !$long_options)) {
146  $non_opts = array_merge($non_opts, array_slice($args, $i));
147  break;
148  } elseif (strlen($arg) > 1 && $arg{1} == '-') {
149  $error = Console_Getopt::_parseLongOption(substr($arg, 2),
150  $long_options,
151  $opts,
152  $args,
153  $skip_unknown);
154  if (PEAR::isError($error)) {
155  return $error;
156  }
157  } elseif ($arg == '-') {
158  // - is stdin
159  $non_opts = array_merge($non_opts, array_slice($args, $i));
160  break;
161  } else {
162  $error = Console_Getopt::_parseShortOption(substr($arg, 1),
163  $short_options,
164  $opts,
165  $args,
166  $skip_unknown);
167  if (PEAR::isError($error)) {
168  return $error;
169  }
170  }
171  }
172 
173  return array($opts, $non_opts);
174  }
_parseShortOption($arg, $short_options, &$opts, &$args, $skip_unknown)
Parse short option.
Definition: Getopt.php:188
isError($data, $code=null)
Tell whether a value is a PEAR error.
Definition: PEAR.php:279
_parseLongOption($arg, $long_options, &$opts, &$args, $skip_unknown)
Parse long option.
Definition: Getopt.php:275
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getopt()

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

91  {
92  return Console_Getopt::doGetopt(1, $args, $short_options, $long_options, $skip_unknown);
93  }
doGetopt($version, $args, $short_options, $long_options=null, $skip_unknown=false)
The actual implementation of the argument parsing code.
Definition: Getopt.php:106
+ Here is the call graph for this function:

◆ getopt2()

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

74  {
75  return Console_Getopt::doGetopt(2, $args, $short_options, $long_options, $skip_unknown);
76  }
doGetopt($version, $args, $short_options, $long_options=null, $skip_unknown=false)
The actual implementation of the argument parsing code.
Definition: Getopt.php:106
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ readPHPArgv()

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 $_SERVER, $GLOBALS, and PEAR\raiseError().

351  {
352  global $argv;
353  if (!is_array($argv)) {
354  if (!@is_array($_SERVER['argv'])) {
355  if (!@is_array($GLOBALS['HTTP_SERVER_VARS']['argv'])) {
356  $msg = "Could not read cmd args (register_argc_argv=Off?)";
357  return PEAR::raiseError("Console_Getopt: " . $msg);
358  }
359  return $GLOBALS['HTTP_SERVER_VARS']['argv'];
360  }
361  return $_SERVER['argv'];
362  }
363  return $argv;
364  }
if((!isset($_SERVER['DOCUMENT_ROOT'])) OR(empty($_SERVER['DOCUMENT_ROOT']))) $_SERVER['DOCUMENT_ROOT']
& raiseError($message=null, $code=null, $mode=null, $options=null, $userinfo=null, $error_class=null, $skipmsg=false)
This method is a wrapper that returns an instance of the configured error class with this object&#39;s de...
Definition: PEAR.php:524
$GLOBALS['PHPCAS_CLIENT']
This global variable is used by the interface class phpCAS.
Definition: CAS.php:276
+ Here is the call graph for this function:

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