Source for file Getopt.php
Documentation is available at Getopt.php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2004 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 3.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available through the world-wide-web at the following url: |
// | http://www.php.net/license/3_0.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Author: Andrei Zmievski <andrei@php.net> |
// +----------------------------------------------------------------------+
// $Id: Getopt.php,v 1.32 2007/02/18 04:13:07 cellog Exp $
* Command-line options parsing class.
* @author Andrei Zmievski <andrei@php.net>
* 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,
* Long and short options can be mixed.
* Most of the semantics of this function are based on GNU getopt_long().
* @param array $args an array of command-line arguments
* @param string $short_options specifies the list of allowed short options
* @param array $long_options specifies the list of allowed long options
* @return array two-element array containing the list of parsed options and
* the non-option arguments
function getopt2 ($args, $short_options, $long_options = null )
return Console_Getopt ::doGetopt (2 , $args, $short_options, $long_options);
* This function expects $args to start with the script name (POSIX-style).
* Preserved for backwards compatibility.
function getopt ($args, $short_options, $long_options = null )
return Console_Getopt ::doGetopt (1 , $args, $short_options, $long_options);
* The actual implementation of the argument parsing code.
function doGetopt ($version, $args, $short_options, $long_options = null )
// in case you pass directly readPHPArgv() as the first arg
if (PEAR ::isError ($args)) {
return array (array (), array ());
* Preserve backwards compatibility with callers that relied on
if (isset ($args[0 ]{0 }) && $args[0 ]{0 } != '-') {
while (list ($i, $arg) = each($args)) {
/* The special element '--' means explicit end of
options. Treat the rest of the arguments as non-options
if ($arg{0 } != '-' || (strlen($arg) > 1 && $arg{1 } == '-' && !$long_options)) {
} elseif (strlen($arg) > 1 && $arg{1 } == '-') {
$error = Console_Getopt ::_parseLongOption (substr($arg, 2 ), $long_options, $opts, $args);
if (PEAR ::isError ($error))
$error = Console_Getopt ::_parseShortOption (substr($arg, 1 ), $short_options, $opts, $args);
if (PEAR ::isError ($error))
return array ($opts, $non_opts);
function _parseShortOption ($arg, $short_options, &$opts, &$args)
for ($i = 0; $i < strlen($arg); $i++ ) {
/* Try to find the short option in the specifier string. */
if (($spec = strstr($short_options, $opt)) === false || $arg{$i} == ':')
return PEAR ::raiseError (" Console_Getopt: unrecognized option -- $opt" );
if (strlen($spec) > 1 && $spec{1 } == ':') {
if (strlen($spec) > 2 && $spec{2 } == ':') {
/* Option takes an optional argument. Use the remainder of
the arg string if there is anything left. */
$opts[] = array ($opt, substr($arg, $i + 1 ));
/* Option requires an argument. Use the remainder of the arg
string if there is anything left. */
$opts[] = array ($opt, substr($arg, $i + 1 ));
} else if (list (, $opt_arg) = each($args)) {
/* Else use the next argument. */;
if (Console_Getopt ::_isShortOpt ($opt_arg) || Console_Getopt ::_isLongOpt ($opt_arg)) {
return PEAR ::raiseError (" Console_Getopt: option requires an argument -- $opt" );
return PEAR ::raiseError (" Console_Getopt: option requires an argument -- $opt" );
$opts[] = array ($opt, $opt_arg);
function _isShortOpt ($arg)
return strlen($arg) == 2 && $arg[0 ] == '-' && preg_match('/[a-zA-Z]/', $arg[1 ]);
function _isLongOpt ($arg)
return strlen($arg) > 2 && $arg[0 ] == '-' && $arg[1 ] == '-' &&
function _parseLongOption ($arg, $long_options, &$opts, &$args)
@list ($opt, $opt_arg) = explode('=', $arg, 2 );
for ($i = 0; $i < count($long_options); $i++ ) {
$long_opt = $long_options[$i];
$opt_start = substr($long_opt, 0 , $opt_len);
/* Option doesn't match. Go on to the next one. */
if ($long_opt_name != $opt) {
$opt_rest = substr($long_opt, $opt_len);
/* Check that the options uniquely matches one of the allowed
$next_option_rest = substr($long_options[$i + 1 ], $opt_len);
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 } != '=') {
return PEAR ::raiseError (" Console_Getopt: option --$opt is ambiguous" );
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))) {
return PEAR ::raiseError (" Console_Getopt: option --$opt requires an argument" );
return PEAR ::raiseError (" Console_Getopt: option --$opt doesn't allow an argument" );
$opts[] = array ('--' . $opt, $opt_arg);
return PEAR ::raiseError (" Console_Getopt: unrecognized option --$opt" );
* Safely read the $argv PHP array across different PHP configurations.
* Will take care on register_globals and register_argc_argv ini directives
* @return mixed the $argv PHP array or PEAR error if not registered
if (!@is_array($GLOBALS['HTTP_SERVER_VARS']['argv'])) {
return PEAR ::raiseError ("Console_Getopt: Could not read cmd args (register_argc_argv=Off?)");
return $GLOBALS['HTTP_SERVER_VARS']['argv'];
Documentation generated on Mon, 11 Mar 2019 14:56:42 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|