import v1.1.0_beta1 | 2009-08-21
This commit is contained in:
@ -20,11 +20,6 @@
|
||||
* @version $Id: $
|
||||
*/
|
||||
|
||||
/**
|
||||
* @see Zend_Console_Getopt_Exception
|
||||
*/
|
||||
require_once 'Zend/Console/Getopt/Exception.php';
|
||||
|
||||
/**
|
||||
* Zend_Console_Getopt is a class to parse options for command-line
|
||||
* applications.
|
||||
@ -131,6 +126,7 @@ require_once 'Zend/Console/Getopt/Exception.php';
|
||||
*/
|
||||
class Zend_Console_Getopt
|
||||
{
|
||||
|
||||
/**
|
||||
* The options for a given application can be in multiple formats.
|
||||
* modeGnu is for traditional 'ab:c:' style getopt format.
|
||||
@ -154,23 +150,26 @@ class Zend_Console_Getopt
|
||||
* ruleMode is either 'zend' or 'gnu' or a user-defined mode.
|
||||
* dashDash is true if '--' signifies the end of command-line options.
|
||||
* ignoreCase is true if '--opt' and '--OPT' are implicitly synonyms.
|
||||
* parseAll is true if all options on the command line should be parsed, regardless of
|
||||
* whether an argument appears before them.
|
||||
*/
|
||||
const CONFIG_RULEMODE = 'ruleMode';
|
||||
const CONFIG_DASHDASH = 'dashDash';
|
||||
const CONFIG_IGNORECASE = 'ignoreCase';
|
||||
const CONFIG_PARSEALL = 'parseAll';
|
||||
|
||||
/**
|
||||
* Defaults for getopt configuration are:
|
||||
* ruleMode is 'zend' format,
|
||||
* dashDash (--) token is enabled,
|
||||
* ignoreCase is not enabled.
|
||||
*
|
||||
* @var array Config
|
||||
* ignoreCase is not enabled,
|
||||
* parseAll is enabled.
|
||||
*/
|
||||
protected $_getoptConfig = array(
|
||||
self::CONFIG_RULEMODE => self::MODE_ZEND,
|
||||
self::CONFIG_DASHDASH => true,
|
||||
self::CONFIG_IGNORECASE => false
|
||||
self::CONFIG_IGNORECASE => false,
|
||||
self::CONFIG_PARSEALL => true
|
||||
);
|
||||
|
||||
/**
|
||||
@ -242,6 +241,19 @@ class Zend_Console_Getopt
|
||||
*/
|
||||
public function __construct($rules, $argv = null, $getoptConfig = array())
|
||||
{
|
||||
if (!isset($_SERVER['argv'])) {
|
||||
require_once 'Zend/Console/Getopt/Exception.php';
|
||||
if(ini_get('register_argc_argv') == false) {
|
||||
throw new Zend_Console_Getopt_Exception(
|
||||
"argv is not available, because ini option 'register_argc_argv' is set Off"
|
||||
);
|
||||
} else {
|
||||
throw new Zend_Console_Getopt_Exception(
|
||||
'$_SERVER["argv"] is not set, but Zend_Console_Getopt cannot work without this information.'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
$this->_progname = $_SERVER['argv'][0];
|
||||
$this->setOptions($getoptConfig);
|
||||
$this->addRules($rules);
|
||||
@ -630,9 +642,7 @@ class Zend_Console_Getopt
|
||||
$flag = $this->_ruleMap[$flag];
|
||||
if (isset($this->_rules[$alias]) || isset($this->_ruleMap[$alias])) {
|
||||
$o = (strlen($alias) == 1 ? '-' : '--') . $alias;
|
||||
/**
|
||||
* @see Zend_Console_Getopt_Exception
|
||||
*/
|
||||
require_once 'Zend/Console/Getopt/Exception.php';
|
||||
throw new Zend_Console_Getopt_Exception(
|
||||
"Option \"$o\" is being defined more than once.");
|
||||
}
|
||||
@ -691,10 +701,17 @@ class Zend_Console_Getopt
|
||||
}
|
||||
if (substr($argv[0], 0, 2) == '--') {
|
||||
$this->_parseLongOption($argv);
|
||||
} else if (substr($argv[0], 0, 1) == '-') {
|
||||
} else if (substr($argv[0], 0, 1) == '-' && ('-' != $argv[0] || count($argv) >1)) {
|
||||
$this->_parseShortOptionCluster($argv);
|
||||
} else {
|
||||
} else if($this->_getoptConfig[self::CONFIG_PARSEALL]) {
|
||||
$this->_remainingArgs[] = array_shift($argv);
|
||||
} else {
|
||||
/*
|
||||
* We should put all other arguments in _remainingArgs and stop parsing
|
||||
* since CONFIG_PARSEALL is false.
|
||||
*/
|
||||
$this->_remainingArgs = array_merge($this->_remainingArgs, $argv);
|
||||
break;
|
||||
}
|
||||
}
|
||||
$this->_parsed = true;
|
||||
@ -712,7 +729,7 @@ class Zend_Console_Getopt
|
||||
protected function _parseLongOption(&$argv)
|
||||
{
|
||||
$optionWithParam = ltrim(array_shift($argv), '-');
|
||||
$l = explode('=', $optionWithParam);
|
||||
$l = explode('=', $optionWithParam, 2);
|
||||
$flag = array_shift($l);
|
||||
$param = array_shift($l);
|
||||
if (isset($param)) {
|
||||
@ -751,9 +768,7 @@ class Zend_Console_Getopt
|
||||
$flag = strtolower($flag);
|
||||
}
|
||||
if (!isset($this->_ruleMap[$flag])) {
|
||||
/**
|
||||
* @see Zend_Console_Getopt_Exception
|
||||
*/
|
||||
require_once 'Zend/Console/Getopt/Exception.php';
|
||||
throw new Zend_Console_Getopt_Exception(
|
||||
"Option \"$flag\" is not recognized.",
|
||||
$this->getUsageMessage());
|
||||
@ -765,9 +780,7 @@ class Zend_Console_Getopt
|
||||
$param = array_shift($argv);
|
||||
$this->_checkParameterType($realFlag, $param);
|
||||
} else {
|
||||
/**
|
||||
* @see Zend_Console_Getopt_Exception
|
||||
*/
|
||||
require_once 'Zend/Console/Getopt/Exception.php';
|
||||
throw new Zend_Console_Getopt_Exception(
|
||||
"Option \"$flag\" requires a parameter.",
|
||||
$this->getUsageMessage());
|
||||
@ -806,9 +819,7 @@ class Zend_Console_Getopt
|
||||
switch ($type) {
|
||||
case 'word':
|
||||
if (preg_match('/\W/', $param)) {
|
||||
/**
|
||||
* @see Zend_Console_Getopt_Exception
|
||||
*/
|
||||
require_once 'Zend/Console/Getopt/Exception.php';
|
||||
throw new Zend_Console_Getopt_Exception(
|
||||
"Option \"$flag\" requires a single-word parameter, but was given \"$param\".",
|
||||
$this->getUsageMessage());
|
||||
@ -816,9 +827,7 @@ class Zend_Console_Getopt
|
||||
break;
|
||||
case 'integer':
|
||||
if (preg_match('/\D/', $param)) {
|
||||
/**
|
||||
* @see Zend_Console_Getopt_Exception
|
||||
*/
|
||||
require_once 'Zend/Console/Getopt/Exception.php';
|
||||
throw new Zend_Console_Getopt_Exception(
|
||||
"Option \"$flag\" requires an integer parameter, but was given \"$param\".",
|
||||
$this->getUsageMessage());
|
||||
@ -840,7 +849,7 @@ class Zend_Console_Getopt
|
||||
protected function _addRulesModeGnu($rules)
|
||||
{
|
||||
$ruleArray = array();
|
||||
|
||||
|
||||
/**
|
||||
* Options may be single alphanumeric characters.
|
||||
* Options may have a ':' which indicates a required string parameter.
|
||||
@ -878,7 +887,7 @@ class Zend_Console_Getopt
|
||||
{
|
||||
// this may have to translate the long parm type if there
|
||||
// are any complaints that =string will not work (even though that use
|
||||
// case is not documented)
|
||||
// case is not documented)
|
||||
if (in_array(substr($ruleCode, -2, 1), array('-', '='))) {
|
||||
$flagList = substr($ruleCode, 0, -2);
|
||||
$delimiter = substr($ruleCode, -2, 1);
|
||||
@ -895,17 +904,13 @@ class Zend_Console_Getopt
|
||||
$mainFlag = $flags[0];
|
||||
foreach ($flags as $flag) {
|
||||
if (empty($flag)) {
|
||||
/**
|
||||
* @see Zend_Console_Getopt_Exception
|
||||
*/
|
||||
require_once 'Zend/Console/Getopt/Exception.php';
|
||||
throw new Zend_Console_Getopt_Exception(
|
||||
"Blank flag not allowed in rule \"$ruleCode\".");
|
||||
}
|
||||
if (strlen($flag) == 1) {
|
||||
if (isset($this->_ruleMap[$flag])) {
|
||||
/**
|
||||
* @see Zend_Console_Getopt_Exception
|
||||
*/
|
||||
require_once 'Zend/Console/Getopt/Exception.php';
|
||||
throw new Zend_Console_Getopt_Exception(
|
||||
"Option \"-$flag\" is being defined more than once.");
|
||||
}
|
||||
@ -913,9 +918,7 @@ class Zend_Console_Getopt
|
||||
$rule['alias'][] = $flag;
|
||||
} else {
|
||||
if (isset($this->_rules[$flag]) || isset($this->_ruleMap[$flag])) {
|
||||
/**
|
||||
* @see Zend_Console_Getopt_Exception
|
||||
*/
|
||||
require_once 'Zend/Console/Getopt/Exception.php';
|
||||
throw new Zend_Console_Getopt_Exception(
|
||||
"Option \"--$flag\" is being defined more than once.");
|
||||
}
|
||||
|
Reference in New Issue
Block a user