2010-10-07 03:18:31 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ConsoleOptionParser file
|
|
|
|
*
|
|
|
|
* PHP 5
|
|
|
|
*
|
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
|
|
|
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
|
|
|
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.console
|
|
|
|
* @since CakePHP(tm) v 2.0
|
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles parsing the ARGV in the command line and provides support
|
|
|
|
* for GetOpt compatible option definition. Provides a builder pattern implementation
|
|
|
|
* for creating shell option parsers.
|
|
|
|
*
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.console
|
|
|
|
*/
|
|
|
|
class ConsoleOptionParser {
|
2010-10-08 01:18:22 +00:00
|
|
|
|
2010-10-09 14:41:11 +00:00
|
|
|
/**
|
|
|
|
* Description text - displays before options when help is generated
|
|
|
|
*
|
|
|
|
* @see ConsoleOptionParser::description()
|
|
|
|
* @var string
|
|
|
|
*/
|
2010-10-08 01:18:22 +00:00
|
|
|
protected $_description = null;
|
2010-10-09 14:41:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Epilog text - displays after options when help is generated
|
|
|
|
*
|
|
|
|
* @see ConsoleOptionParser::epilog()
|
|
|
|
* @var string
|
|
|
|
*/
|
2010-10-08 01:18:22 +00:00
|
|
|
protected $_epilog = null;
|
2010-10-09 14:41:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Option definitions.
|
|
|
|
*
|
|
|
|
* @see ConsoleOptionParser::addOption()
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-10-08 01:18:22 +00:00
|
|
|
protected $_options = array();
|
2010-10-09 14:41:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Positional argument definitions.
|
|
|
|
*
|
|
|
|
* @see ConsoleOptionParser::addArgument()
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-10-08 01:18:22 +00:00
|
|
|
protected $_args = array();
|
|
|
|
|
2010-10-07 03:18:31 +00:00
|
|
|
/**
|
2010-10-09 04:21:47 +00:00
|
|
|
* Construct an OptionParser so you can define its behavior
|
2010-10-07 03:18:31 +00:00
|
|
|
*
|
2010-10-09 04:21:47 +00:00
|
|
|
* ### Options
|
2010-10-07 03:18:31 +00:00
|
|
|
*
|
|
|
|
* Named arguments come in two forms, long and short. Long arguments are preceeded
|
|
|
|
* by two - and give a more verbose option name. i.e. `--version`. Short arguments are
|
|
|
|
* preceeded by one - and are only one character long. They usually match with a long option,
|
|
|
|
* and provide a more terse alternative.
|
|
|
|
*
|
2010-10-09 04:21:47 +00:00
|
|
|
* #### Using Options
|
2010-10-09 04:16:50 +00:00
|
|
|
*
|
2010-10-09 04:21:47 +00:00
|
|
|
* Options can be defined with both long and short forms. By using `$parser->addOption()`
|
|
|
|
* you can define new options. The name of the option is used as its long form, and you
|
|
|
|
* can supply an additional short form, with the `short` option.
|
|
|
|
*
|
|
|
|
* Calling options can be done using syntax similar to most *nix command line tools. Long options
|
|
|
|
* cane either include an `=` or leave it out.
|
|
|
|
*
|
|
|
|
* `cake myshell command --connection default --name=something`
|
|
|
|
*
|
|
|
|
* Short options can be defined singally or in groups.
|
|
|
|
*
|
|
|
|
* `cake myshell command -cn`
|
|
|
|
*
|
|
|
|
* ### Positional arguments
|
2010-10-09 04:16:50 +00:00
|
|
|
*
|
2010-10-09 04:57:09 +00:00
|
|
|
* If no positional arguments are defined, all of them will be parsed. If you define positional
|
|
|
|
* arguments any arguments greater than those defined will cause exceptions. Additionally you can
|
|
|
|
* declare arguments as optional, by setting the required param to false.
|
|
|
|
*
|
|
|
|
* `$parser->addArgument('model', array('required' => false));`
|
|
|
|
*
|
2010-10-07 03:18:31 +00:00
|
|
|
* ### Providing Help text
|
|
|
|
*
|
|
|
|
* By providing help text for your positional arguments and named arguments, the ConsoleOptionParser
|
|
|
|
* can generate a help display for you. You can view the help for shells by using the `--help` or `-h` switch.
|
|
|
|
*
|
|
|
|
*/
|
2010-10-08 01:18:22 +00:00
|
|
|
public function __construct() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get or set the description text for shell/task
|
|
|
|
*
|
|
|
|
* @param string $text The text to set, or null if you want to read
|
|
|
|
* @return mixed If reading, the value of the description. If setting $this will be returned
|
|
|
|
*/
|
|
|
|
public function description($text = null) {
|
|
|
|
if ($text !== null) {
|
|
|
|
$this->_description = $text;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
return $this->_description;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get or set an epilog to the parser. The epilog is added to the end of
|
|
|
|
* the options and arguments listing when help is generated.
|
|
|
|
*
|
|
|
|
* @param string $text Text when setting or null when reading.
|
|
|
|
* @return mixed If reading, the value of the epilog. If setting $this will be returned.
|
|
|
|
*/
|
|
|
|
public function epilog($text = null) {
|
|
|
|
if ($text !== null) {
|
|
|
|
$this->_epilog = $text;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
return $this->_epilog;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an option to the option parser. Options allow you to define optional or required
|
|
|
|
* parameters for your console application. Options are defined by the parameters they use.
|
|
|
|
*
|
|
|
|
* ### Params
|
|
|
|
*
|
2010-10-09 04:16:50 +00:00
|
|
|
* - `short` - The single letter variant for this option, leave undefined for none.
|
2010-10-09 04:57:09 +00:00
|
|
|
* - `help` - Help text for this option. Used when generating help for the option.
|
2010-10-08 02:44:02 +00:00
|
|
|
* - `default` - The default value for this option. If not defined the default will be true.
|
2010-10-09 14:33:54 +00:00
|
|
|
* - `boolean` - The option uses no value, its just a boolean switch. Defaults to false.
|
2010-10-08 01:18:22 +00:00
|
|
|
*
|
2010-10-08 01:46:19 +00:00
|
|
|
* @param string $name The long name you want to the value to be parsed out as when options are parsed.
|
2010-10-08 01:18:22 +00:00
|
|
|
* @param array $params An array of parameters that define the behavior of the option
|
2010-10-08 01:46:19 +00:00
|
|
|
* @return returns $this.
|
2010-10-08 01:18:22 +00:00
|
|
|
*/
|
|
|
|
public function addOption($name, $params = array()) {
|
2010-10-08 01:46:19 +00:00
|
|
|
$defaults = array(
|
2010-10-08 02:44:02 +00:00
|
|
|
'name' => $name,
|
2010-10-09 04:16:50 +00:00
|
|
|
'short' => null,
|
2010-10-09 04:57:09 +00:00
|
|
|
'help' => '',
|
2010-10-09 14:33:54 +00:00
|
|
|
'default' => true,
|
|
|
|
'boolean' => false
|
2010-10-08 01:46:19 +00:00
|
|
|
);
|
2010-10-08 02:44:02 +00:00
|
|
|
$options = array_merge($defaults, $params);
|
|
|
|
$this->_options[$name] = $options;
|
2010-10-09 04:16:50 +00:00
|
|
|
if (!empty($options['short'])) {
|
|
|
|
$this->_options[$options['short']] = $options;
|
2010-10-08 02:44:02 +00:00
|
|
|
}
|
2010-10-08 01:46:19 +00:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2010-10-09 04:24:07 +00:00
|
|
|
/**
|
|
|
|
* Add a positional argument to the option parser.
|
|
|
|
*
|
2010-10-09 04:57:09 +00:00
|
|
|
* ### Params
|
|
|
|
*
|
|
|
|
* - `help` The help text to display for this argument.
|
|
|
|
* - `required` Whether this parameter is required.
|
|
|
|
* - `index` The index for the arg, if left undefined the argument will be put
|
|
|
|
* onto the end of the arguments. If you define the same index twice the first
|
|
|
|
* option will be overwritten.
|
|
|
|
*
|
|
|
|
* @param string $name The name of the argument.
|
|
|
|
* @param array $params Parameters for the argument, see above.
|
|
|
|
* @return $this.
|
2010-10-09 04:24:07 +00:00
|
|
|
*/
|
|
|
|
public function addArgument($name, $params = array()) {
|
2010-10-09 04:57:09 +00:00
|
|
|
$index = count($this->_args);
|
|
|
|
$defaults = array(
|
|
|
|
'name' => $name,
|
|
|
|
'help' => '',
|
|
|
|
'index' => $index,
|
|
|
|
'required' => false
|
|
|
|
);
|
|
|
|
$options = array_merge($defaults, $params);
|
|
|
|
$this->_args[$options['index']] = $options;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the arguments defined in the parser.
|
|
|
|
*
|
|
|
|
* @return array Array of argument descriptions
|
|
|
|
*/
|
|
|
|
public function arguments() {
|
|
|
|
return $this->_args;
|
2010-10-09 04:24:07 +00:00
|
|
|
}
|
|
|
|
|
2010-10-08 01:46:19 +00:00
|
|
|
/**
|
|
|
|
* Parse the argv array into a set of params and args.
|
|
|
|
*
|
|
|
|
* @param array $argv Array of args (argv) to parse
|
|
|
|
* @return Array array($params, $args)
|
2010-10-09 04:57:09 +00:00
|
|
|
* @throws InvalidArgumentException When an invalid parameter is encountered.
|
2010-10-08 01:46:19 +00:00
|
|
|
*/
|
|
|
|
public function parse($argv) {
|
|
|
|
$params = $args = array();
|
2010-10-08 02:44:02 +00:00
|
|
|
$this->_tokens = $argv;
|
|
|
|
while ($token = array_shift($this->_tokens)) {
|
|
|
|
if (substr($token, 0, 2) == '--') {
|
|
|
|
$params = $this->_parseLongOption($token, $params);
|
|
|
|
} elseif (substr($token, 0, 1) == '-') {
|
|
|
|
$params = $this->_parseShortOption($token, $params);
|
2010-10-09 04:57:09 +00:00
|
|
|
} else {
|
|
|
|
$args = $this->_parseArg($token, $args);
|
2010-10-08 02:44:02 +00:00
|
|
|
}
|
|
|
|
}
|
2010-10-09 14:41:11 +00:00
|
|
|
foreach ($this->_args as $i => $arg) {
|
|
|
|
if ($arg['required'] && !isset($args[$i])) {
|
|
|
|
throw new RuntimeException(
|
|
|
|
sprintf(__('Missing required arguments. %s is required.'), $arg['name'])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2010-10-08 01:46:19 +00:00
|
|
|
return array($params, $args);
|
2010-10-07 03:18:31 +00:00
|
|
|
}
|
2010-10-08 02:44:02 +00:00
|
|
|
|
|
|
|
/**
|
2010-10-09 04:24:07 +00:00
|
|
|
* Parse the value for a long option out of $this->_tokens. Will handle
|
|
|
|
* options with an `=` in them.
|
2010-10-08 02:44:02 +00:00
|
|
|
*
|
|
|
|
* @param string $option The option to parse.
|
|
|
|
* @param array $params The params to append the parsed value into
|
|
|
|
* @return array Params with $option added in.
|
|
|
|
*/
|
|
|
|
protected function _parseLongOption($option, $params) {
|
|
|
|
$name = substr($option, 2);
|
2010-10-08 03:08:32 +00:00
|
|
|
if (strpos($name, '=') !== false) {
|
|
|
|
list($name, $value) = explode('=', $name, 2);
|
|
|
|
array_unshift($this->_tokens, $value);
|
|
|
|
}
|
2010-10-08 02:44:02 +00:00
|
|
|
return $this->_parseOptionName($name, $params);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse the value for a short option out of $this->_tokens
|
2010-10-09 04:16:50 +00:00
|
|
|
* If the $option is a combination of multiple shortcuts like -otf
|
|
|
|
* they will be shifted onto the token stack and parsed individually.
|
2010-10-08 02:44:02 +00:00
|
|
|
*
|
|
|
|
* @param string $option The option to parse.
|
|
|
|
* @param array $params The params to append the parsed value into
|
|
|
|
* @return array Params with $option added in.
|
|
|
|
*/
|
|
|
|
protected function _parseShortOption($option, $params) {
|
|
|
|
$key = substr($option, 1);
|
2010-10-09 04:16:50 +00:00
|
|
|
if (strlen($key) > 1) {
|
|
|
|
$flags = str_split($key);
|
|
|
|
$key = $flags[0];
|
|
|
|
for ($i = 1, $len = count($flags); $i < $len; $i++) {
|
|
|
|
array_unshift($this->_tokens, '-' . $flags[$i]);
|
|
|
|
}
|
|
|
|
}
|
2010-10-08 02:44:02 +00:00
|
|
|
$name = $this->_options[$key]['name'];
|
|
|
|
return $this->_parseOptionName($name, $params);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse an option by its name index.
|
|
|
|
*
|
|
|
|
* @param string $option The option to parse.
|
|
|
|
* @param array $params The params to append the parsed value into
|
|
|
|
* @return array Params with $option added in.
|
|
|
|
*/
|
|
|
|
protected function _parseOptionName($name, $params) {
|
|
|
|
$definition = $this->_options[$name];
|
|
|
|
$nextValue = $this->_nextToken();
|
2010-10-09 14:33:54 +00:00
|
|
|
if (!$definition['boolean'] && !empty($nextValue) && $nextValue{0} != '-') {
|
|
|
|
array_shift($this->_tokens);
|
2010-10-08 02:44:02 +00:00
|
|
|
$value = $nextValue;
|
2010-10-08 03:13:56 +00:00
|
|
|
} else {
|
|
|
|
$value = $definition['default'];
|
2010-10-08 02:44:02 +00:00
|
|
|
}
|
|
|
|
$params[$name] = $value;
|
|
|
|
return $params;
|
|
|
|
}
|
|
|
|
|
2010-10-09 04:57:09 +00:00
|
|
|
/**
|
|
|
|
* Checks that the argument doesn't exceed the declared arguments.
|
|
|
|
*
|
|
|
|
* @param string $argument The argument to append
|
|
|
|
* @param array $args The array of parsed args to append to.
|
|
|
|
* @return array Args
|
|
|
|
*/
|
|
|
|
protected function _parseArg($argument, $args) {
|
|
|
|
if (empty($this->_args)) {
|
|
|
|
array_push($args, $argument);
|
|
|
|
return $args;
|
|
|
|
}
|
|
|
|
$position = 0;
|
|
|
|
$next = count($args);
|
|
|
|
if (!isset($this->_args[$next])) {
|
|
|
|
throw new InvalidArgumentException(__('Too many arguments.'));
|
|
|
|
}
|
|
|
|
array_push($args, $argument);
|
|
|
|
return $args;
|
|
|
|
}
|
|
|
|
|
2010-10-08 02:44:02 +00:00
|
|
|
/**
|
|
|
|
* Find the next token in the argv set.
|
|
|
|
*
|
2010-10-08 03:08:32 +00:00
|
|
|
* @param string
|
2010-10-08 02:44:02 +00:00
|
|
|
* @return next token or ''
|
|
|
|
*/
|
|
|
|
protected function _nextToken() {
|
|
|
|
return isset($this->_tokens[0]) ? $this->_tokens[0] : '';
|
|
|
|
}
|
2010-10-07 03:18:31 +00:00
|
|
|
}
|