Permalink
Browse files

Apply CS patch for bug #14786.

git-svn-id: http://svn.php.net/repository/pear/packages/Console_Getopt/trunk@267328 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information...
1 parent fe71f3d commit 1fb7adbf0126484828f6f31f20cd0d4909fc855e Andrei Zmievski committed Oct 14, 2008
Showing with 123 additions and 56 deletions.
  1. +123 −56 Console/Getopt.php
View
179 Console/Getopt.php
@@ -1,32 +1,42 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
-// +----------------------------------------------------------------------+
-// | PHP Version 5 |
-// +----------------------------------------------------------------------+
-// | 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$
+/**
+ * Color.php
+ *
+ * PHP Version 5
+ *
+ * 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.
+ *
+ * @category Console
+ * @package Console_Getopt
+ * @author Andrei Zmievski <andrei@php.net>
+ * @license http://www.php.net/license/3_0.txt PHP 3.0
+ * @version CVS: $Id$
+ * @link http://pear.php.net/package/Console_Getopt
+ */
require_once 'PEAR.php';
/**
* Command-line options parsing class.
*
- * @author Andrei Zmievski <andrei@php.net>
- *
+ * @category Console
+ * @package Console_Getopt
+ * @author Andrei Zmievski <andrei@php.net>
+ * @license http://www.php.net/license/3_0.txt PHP 3.0
+ * @link http://pear.php.net/package/Console_Getopt
*/
-class Console_Getopt {
+class Console_Getopt
+{
+
/**
* Parses the command-line options.
*
@@ -53,15 +63,13 @@ class Console_Getopt {
*
* 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
+ * @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
- *
* @access public
- *
*/
function getopt2($args, $short_options, $long_options = null)
{
@@ -71,7 +79,14 @@ function getopt2($args, $short_options, $long_options = null)
/**
* This function expects $args to start with the script name (POSIX-style).
* Preserved for backwards compatibility.
+ *
+ * @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
+ *
* @see getopt2()
+ * @return array two-element array containing the list of parsed options and
+ * the non-option arguments
*/
function getopt($args, $short_options, $long_options = null)
{
@@ -80,6 +95,13 @@ function getopt($args, $short_options, $long_options = null)
/**
* The actual implementation of the argument parsing code.
+ *
+ * @param int $version Version to use
+ * @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
*/
function doGetopt($version, $args, $short_options, $long_options = null)
{
@@ -120,41 +142,56 @@ function doGetopt($version, $args, $short_options, $long_options = null)
break;
}
- if ($arg{0} != '-' || (strlen($arg) > 1 && $arg{1} == '-' && !$long_options)) {
+ 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);
- if (PEAR::isError($error))
+ $error = Console_Getopt::_parseLongOption(substr($arg, 2),
+ $long_options, $opts,
+ $args);
+ 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);
- if (PEAR::isError($error))
+ $error = Console_Getopt::_parseShortOption(substr($arg, 1),
+ $short_options, $opts,
+ $args);
+ if (PEAR::isError($error)) {
return $error;
+ }
}
}
return array($opts, $non_opts);
}
/**
- * @access private
+ * Parse short option
+ *
+ * @param string $arg Argument
+ * @param string[] $short_options Available short options
+ * @param string[][] &$opts
+ * @param string[] &$args
*
+ * @access private
+ * @return void
*/
function _parseShortOption($arg, $short_options, &$opts, &$args)
{
for ($i = 0; $i < strlen($arg); $i++) {
- $opt = $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} == ':')
- {
- return PEAR::raiseError("Console_Getopt: unrecognized option -- $opt");
+ if (($spec = strstr($short_options, $opt)) === false
+ || $arg{$i} == ':') {
+ $msg = "Console_Getopt: unrecognized option -- $opt";
+ return PEAR::raiseError($msg);
}
if (strlen($spec) > 1 && $spec{1} == ':') {
@@ -173,11 +210,14 @@ function _parseShortOption($arg, $short_options, &$opts, &$args)
break;
} 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");
+ if (Console_Getopt::_isShortOpt($opt_arg)
+ || Console_Getopt::_isLongOpt($opt_arg)) {
+ $msg = "option requires an argument --$opt";
+ return PEAR::raiseError("Console_Getopt:" . $msg);
}
} else {
- return PEAR::raiseError("Console_Getopt: option requires an argument -- $opt");
+ $msg = "option requires an argument --$opt";
+ return PEAR::raiseError("Console_Getopt:" . $msg);
}
}
}
@@ -187,44 +227,62 @@ function _parseShortOption($arg, $short_options, &$opts, &$args)
}
/**
- * @access private
+ * Checks if an argument is a short option
+ *
+ * @param string $arg Argument to check
*
+ * @access private
+ * @return bool
*/
function _isShortOpt($arg)
{
- return strlen($arg) == 2 && $arg[0] == '-' && preg_match('/[a-zA-Z]/', $arg[1]);
+ return strlen($arg) == 2 && $arg[0] == '-'
+ && preg_match('/[a-zA-Z]/', $arg[1]);
}
/**
- * @access private
+ * Checks if an argument is a long option
*
+ * @param string $arg Argument to check
+ *
+ * @access private
+ * @return bool
*/
function _isLongOpt($arg)
{
return strlen($arg) > 2 && $arg[0] == '-' && $arg[1] == '-' &&
- preg_match('/[a-zA-Z]+$/', substr($arg, 2));
+ preg_match('/[a-zA-Z]+$/', substr($arg, 2));
}
/**
- * @access private
+ * Parse long option
+ *
+ * @param string $arg Argument
+ * @param string[] $long_options Available long options
+ * @param string[][] &$opts
+ * @param string[] &$args
*
+ * @access private
+ * @return void|PEAR_Error
*/
function _parseLongOption($arg, $long_options, &$opts, &$args)
{
@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);
+ $opt_rest = substr($long_opt, $opt_len);
/* Check that the options uniquely matches one of the allowed
options. */
@@ -233,27 +291,35 @@ function _parseLongOption($arg, $long_options, &$opts, &$args)
} 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} != '=') {
- return PEAR::raiseError("Console_Getopt: option --$opt is ambiguous");
+
+ $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))) {
- return PEAR::raiseError("Console_Getopt: option --$opt requires an argument");
+ $msg = "Console_Getopt: option requires an argument --$opt";
+ return PEAR::raiseError($msg);
}
- if (Console_Getopt::_isShortOpt($opt_arg) || Console_Getopt::_isLongOpt($opt_arg)) {
- return PEAR::raiseError("Console_Getopt: option requires an argument --$opt");
+
+ if (Console_Getopt::_isShortOpt($opt_arg)
+ || Console_Getopt::_isLongOpt($opt_arg)) {
+ $msg = "Console_Getopt: option requires an argument --$opt";
+ return PEAR::raiseError($msg);
}
}
} else if ($opt_arg) {
- return PEAR::raiseError("Console_Getopt: option --$opt doesn't allow an argument");
+ $msg = "Console_Getopt: option --$opt doesn't allow an argument";
+ return PEAR::raiseError($msg);
}
$opts[] = array('--' . $opt, $opt_arg);
@@ -264,19 +330,20 @@ function _parseLongOption($arg, $long_options, &$opts, &$args)
}
/**
- * Safely read the $argv PHP array across different PHP configurations.
- * Will take care on register_globals and register_argc_argv ini directives
- *
- * @access public
- * @return mixed the $argv PHP array or PEAR error if not registered
- */
+ * Safely read the $argv PHP array across different PHP configurations.
+ * Will take care on register_globals and register_argc_argv ini directives
+ *
+ * @access public
+ * @return mixed the $argv PHP array or PEAR error if not registered
+ */
function readPHPArgv()
{
global $argv;
if (!is_array($argv)) {
if (!@is_array($_SERVER['argv'])) {
if (!@is_array($GLOBALS['HTTP_SERVER_VARS']['argv'])) {
- return PEAR::raiseError("Console_Getopt: Could not read cmd args (register_argc_argv=Off?)");
+ $msg = "Could not read cmd args (register_argc_argv=Off?)";
+ return PEAR::raiseError("Console_Getopt: " . $msg);
}
return $GLOBALS['HTTP_SERVER_VARS']['argv'];
}

0 comments on commit 1fb7adb

Please sign in to comment.