Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'zf2/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Shahar Evron committed Apr 19, 2011
14 parents 0b45aba + 1778b0c + a574713 + ba3f132 + 56365a6 + 019f89a + b09b3ba + 2af32d0 + cb39fc7 + 3f8a6fd + 8999753 + 6ff135c + a7cd86b + da1c469 commit 1321747
Show file tree
Hide file tree
Showing 4 changed files with 246 additions and 56 deletions.
4 changes: 2 additions & 2 deletions src/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*
* @category Zend
* @package Zend_Console
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

Expand All @@ -27,7 +27,7 @@
* @uses Zend\Exception
* @category Zend
* @package Zend_Console
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
interface Exception
Expand Down
4 changes: 2 additions & 2 deletions src/Exception/RuntimeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*
* @category Zend
* @package Zend_Console_Getopt
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

Expand All @@ -27,7 +27,7 @@
* @uses \Zend\Exception
* @category Zend
* @package Zend_Console_Getopt
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class RuntimeException
Expand Down
175 changes: 125 additions & 50 deletions src/Getopt.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*
* @category Zend
* @package Zend_Console
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

Expand Down Expand Up @@ -84,41 +84,13 @@
*
* @category Zend
* @package Zend_Console_Getopt
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version Release: @package_version@
* @since Class available since Release 0.6.0
*
* @todo Handle params with multiple values, e.g. --colors=red,green,blue
* Set value of parameter to the array of values. Allow user to specify
* the separator with Zend_Console_Getopt::CONFIG_PARAMETER_SEPARATOR.
* If this config value is null or empty string, do not split values
* into arrays. Default separator is comma (',').
*
* @todo Handle params with multiple values specified with separate options
* e.g. --colors red --colors green --colors blue should give one
* option with an array(red, green, blue).
* Enable with Zend_Console_Getopt::CONFIG_CUMULATIVE_PARAMETERS.
* Default is that subsequent options overwrite the parameter value.
*
* @todo Handle flags occurring multiple times, e.g. -v -v -v
* Set value of the option's parameter to the integer count of instances
* instead of a boolean.
* Enable with Zend_Console_Getopt::CONFIG_CUMULATIVE_FLAGS.
* Default is that the value is simply boolean true regardless of
* how many instances of the flag appear.
*
* @todo Handle flags that implicitly print usage message, e.g. --help
*
* @todo Handle freeform options, e.g. --set-variable
* Enable with Zend_Console_Getopt::CONFIG_FREEFORM_FLAGS
* All flag-like syntax is recognized, no flag generates an exception.
*
* @todo Handle numeric options, e.g. -1, -2, -3, -1000
* Enable with Zend_Console_Getopt::CONFIG_NUMERIC_FLAGS
* The rule must specify a named flag and the '#' symbol as the
* parameter type. e.g., 'lines=#'
*
* @todo Enable user to specify header and footer content in the help message.
*
* @todo Feature request to handle option interdependencies.
Expand Down Expand Up @@ -150,6 +122,7 @@ class Getopt
const TYPE_STRING = 's';
const TYPE_WORD = 'w';
const TYPE_INTEGER = 'i';
const TYPE_NUMERIC_FLAG = '#';

/**
* These are constants for optional behavior of this class.
Expand All @@ -163,19 +136,33 @@ class Getopt
const CONFIG_DASHDASH = 'dashDash';
const CONFIG_IGNORECASE = 'ignoreCase';
const CONFIG_PARSEALL = 'parseAll';
const CONFIG_CUMULATIVE_PARAMETERS = 'cumulativeParameters';
const CONFIG_CUMULATIVE_FLAGS = 'cumulativeFlags';
const CONFIG_PARAMETER_SEPARATOR = 'parameterSeparator';
const CONFIG_FREEFORM_FLAGS = 'freeformFlags';
const CONFIG_NUMERIC_FLAGS = 'numericFlags';

/**
* Defaults for getopt configuration are:
* ruleMode is 'zend' format,
* dashDash (--) token is enabled,
* ignoreCase is not enabled,
* parseAll is enabled.
* parseAll is enabled,
* cumulative parameters are disabled,
* this means that subsequent options overwrite the parameter value,
* cumulative flags are disable,
* freeform flags are disable.
*/
protected $_getoptConfig = array(
self::CONFIG_RULEMODE => self::MODE_ZEND,
self::CONFIG_DASHDASH => true,
self::CONFIG_IGNORECASE => false,
self::CONFIG_PARSEALL => true,
self::CONFIG_RULEMODE => self::MODE_ZEND,
self::CONFIG_DASHDASH => true,
self::CONFIG_IGNORECASE => false,
self::CONFIG_PARSEALL => true,
self::CONFIG_CUMULATIVE_PARAMETERS => false,
self::CONFIG_CUMULATIVE_FLAGS => false,
self::CONFIG_PARAMETER_SEPARATOR => null,
self::CONFIG_FREEFORM_FLAGS => false,
self::CONFIG_NUMERIC_FLAGS => false
);

/**
Expand Down Expand Up @@ -248,15 +235,10 @@ class Getopt
public function __construct($rules, $argv = null, $getoptConfig = array())
{
if (!isset($_SERVER['argv'])) {
if (ini_get('register_argc_argv') == false) {
throw new Exception\InvalidArgumentException(
"argv is not available, because ini option 'register_argc_argv' is set Off"
);
} else {
throw new Exception\InvalidArgumentException(
'$_SERVER["argv"] is not set, but Zend_Console_Getopt cannot work without this information.'
);
}
$errorDescription = (ini_get('register_argc_argv') == false)
? "argv is not available, because ini option 'register_argc_argv' is set Off"
: '$_SERVER["argv"] is not set, but Zend_Console_Getopt cannot work without this information.';
throw new Exception\InvalidArgumentException($errorDescription);
}

$this->_progname = $_SERVER['argv'][0];
Expand Down Expand Up @@ -774,13 +756,29 @@ protected function _parseSingleOption($flag, &$argv)
if ($this->_getoptConfig[self::CONFIG_IGNORECASE]) {
$flag = strtolower($flag);
}

// Check if this option is numeric one
if (preg_match('/^\d+$/', $flag)) {
return $this->_setNumericOptionValue($flag);
}

if (!isset($this->_ruleMap[$flag])) {
throw new Exception\RuntimeException(
"Option \"$flag\" is not recognized.",
$this->getUsageMessage()
);
// Don't throw Exception for flag-like param in case when freeform flags are allowed
if (!$this->_getoptConfig[self::CONFIG_FREEFORM_FLAGS]) {
throw new Exception\RuntimeException(
"Option \"$flag\" is not recognized.",
$this->getUsageMessage()
);
}

// Magic methods in future will use this mark as real flag value
$this->_ruleMap[$flag] = $flag;
$realFlag = $flag;
$this->_rules[$realFlag] = array('param' => 'optional');
} else {
$realFlag = $this->_ruleMap[$flag];
}
$realFlag = $this->_ruleMap[$flag];

switch ($this->_rules[$realFlag]['param']) {
case 'required':
if (count($argv) > 0) {
Expand All @@ -804,7 +802,80 @@ protected function _parseSingleOption($flag, &$argv)
default:
$param = true;
}
$this->_options[$realFlag] = $param;

$this->_setSingleOptionValue($realFlag, $param);
}


/**
* Set given value as value of numeric option
*
* Throw runtime exception if this action is deny by configuration
* or no one numeric option handlers is defined
*
* @param int $value
* @return void
*/
protected function _setNumericOptionValue($value)
{
if (!$this->_getoptConfig[self::CONFIG_NUMERIC_FLAGS]) {
throw new Exception\RuntimeException("Using of numeric flags are deny by configuration");
}

if (empty($this->_getoptConfig['numericFlagsOption'])) {
throw new Exception\RuntimeException("Any option for handling numeric flags are specified");
}

return $this->_setSingleOptionValue($this->_getoptConfig['numericFlagsOption'], $value);
}

/**
* Add relative to options' flag value
*
* If options list already has current flag as key
* and parser should follow cumulative params by configuration,
* we should to add new param to array, not to overwrite
*
* @param string $flag
* @param string $value
* @return null
*/
protected function _setSingleOptionValue($flag, $value)
{
if (true === $value && $this->_getoptConfig[self::CONFIG_CUMULATIVE_FLAGS]) {
// For boolean values we have to create new flag, or increase number of flags' usage count
return $this->_setBooleanFlagValue($flag);
}

// Split multiple values, if necessary
// Filter empty values from splited array
$separator = $this->_getoptConfig[self::CONFIG_PARAMETER_SEPARATOR];
if (is_string($value) && !empty($separator) && is_string($separator) && substr_count($value, $separator)) {
$value = array_filter(explode($separator, $value));
}

if (!array_key_exists($flag, $this->_options)) {
$this->_options[$flag] = $value;
} else if($this->_getoptConfig[self::CONFIG_CUMULATIVE_PARAMETERS]) {
$this->_options[$flag] = (array) $this->_options[$flag];
array_push($this->_options[$flag], $value);
} else {
$this->_options[$flag] = $value;
}
}

/**
* Set TRUE value to given flag, if this option does not exist yet
* In other case increase value to show count of flags' usage
*
* @param string $flag
* @return null
*/
protected function _setBooleanFlagValue($flag)
{
$this->_options[$flag] = array_key_exists($flag, $this->_options)
? (int) $this->_options[$flag] + 1
: true;
}

/**
Expand Down Expand Up @@ -943,6 +1014,10 @@ protected function _addRulesModeZend($rules)
case self::TYPE_INTEGER:
$rule['paramType'] = 'integer';
break;
case self::TYPE_NUMERIC_FLAG:
$rule['paramType'] = 'numericFlag';
$this->_getoptConfig['numericFlagsOption'] = $mainFlag;
break;
case self::TYPE_STRING:
default:
$rule['paramType'] = 'string';
Expand Down
Loading

0 comments on commit 1321747

Please sign in to comment.