mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 11:28:25 +00:00
Refactoring arguments into a separate object.
This commit is contained in:
parent
12fba88528
commit
2774493577
1 changed files with 96 additions and 32 deletions
|
@ -228,15 +228,17 @@ class ConsoleOptionParser {
|
||||||
* @return $this.
|
* @return $this.
|
||||||
*/
|
*/
|
||||||
public function addArgument($name, $params = array()) {
|
public function addArgument($name, $params = array()) {
|
||||||
$index = count($this->_args);
|
|
||||||
$defaults = array(
|
$defaults = array(
|
||||||
'name' => $name,
|
'name' => $name,
|
||||||
'help' => '',
|
'help' => '',
|
||||||
'index' => $index,
|
'index' => count($this->_args),
|
||||||
'required' => false
|
'required' => false
|
||||||
);
|
);
|
||||||
$options = array_merge($defaults, $params);
|
$options = array_merge($defaults, $params);
|
||||||
$this->_args[$options['index']] = $options;
|
$index = $options['index'];
|
||||||
|
unset($options['index']);
|
||||||
|
|
||||||
|
$this->_args[$index] = new ConsoleInputArgument($options);
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -336,9 +338,9 @@ class ConsoleOptionParser {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
foreach ($this->_args as $i => $arg) {
|
foreach ($this->_args as $i => $arg) {
|
||||||
if ($arg['required'] && !isset($args[$i])) {
|
if ($arg->isRequired() && !isset($args[$i])) {
|
||||||
throw new RuntimeException(
|
throw new RuntimeException(
|
||||||
sprintf(__('Missing required arguments. %s is required.'), $arg['name'])
|
sprintf(__('Missing required arguments. %s is required.'), $arg->name())
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -400,14 +402,14 @@ class ConsoleOptionParser {
|
||||||
}
|
}
|
||||||
if (!empty($this->_args)) {
|
if (!empty($this->_args)) {
|
||||||
$max = 0;
|
$max = 0;
|
||||||
foreach ($this->_args as $description) {
|
foreach ($this->_args as $argument) {
|
||||||
$max = (strlen($description['name']) > $max) ? strlen($description['name']) : $max;
|
$max = (strlen($argument->name) > $max) ? strlen($argument->name) : $max;
|
||||||
}
|
}
|
||||||
$max += 2;
|
$max += 2;
|
||||||
$out[] = '<info>Arguments:</info>';
|
$out[] = '<info>Arguments:</info>';
|
||||||
$out[] = '';
|
$out[] = '';
|
||||||
foreach ($this->_args as $description) {
|
foreach ($this->_args as $argument) {
|
||||||
$out[] = $this->_argumentHelp($description, $max);
|
$out[] = $argument->help($max);
|
||||||
}
|
}
|
||||||
$out[] = '';
|
$out[] = '';
|
||||||
}
|
}
|
||||||
|
@ -432,33 +434,12 @@ class ConsoleOptionParser {
|
||||||
foreach ($this->_options as $option) {
|
foreach ($this->_options as $option) {
|
||||||
$usage[] = $option->usage();
|
$usage[] = $option->usage();
|
||||||
}
|
}
|
||||||
foreach ($this->_args as $definition) {
|
foreach ($this->_args as $argument) {
|
||||||
$name = $definition['name'];
|
$usage[] = $argument->usage();
|
||||||
if (!$definition['required']) {
|
|
||||||
$name = '[' . $name . ']';
|
|
||||||
}
|
|
||||||
$usage[] = $name;
|
|
||||||
}
|
}
|
||||||
return implode(' ', $usage);
|
return implode(' ', $usage);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Generate the usage for a single argument.
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
protected function _argumentHelp($definition, $width) {
|
|
||||||
$name = $definition['name'];
|
|
||||||
if (strlen($name) < $width) {
|
|
||||||
$name = str_pad($name, $width, ' ');
|
|
||||||
}
|
|
||||||
$optional = '';
|
|
||||||
if (!$definition['required']) {
|
|
||||||
$optional = ' <comment>(optional)</comment>';
|
|
||||||
}
|
|
||||||
return sprintf('%s%s%s', $name, $definition['help'], $optional);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate help for a single subcommand.
|
* Generate help for a single subcommand.
|
||||||
*
|
*
|
||||||
|
@ -655,3 +636,86 @@ class ConsoleInputOption {
|
||||||
return (bool) $this->boolean;
|
return (bool) $this->boolean;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An object to represent a single argument used in the command line.
|
||||||
|
* ConsoleOptionParser creates these when you use addArgument()
|
||||||
|
*
|
||||||
|
* @see ConsoleOptionParser::addArgument()
|
||||||
|
* @package cake.console
|
||||||
|
*/
|
||||||
|
class ConsoleInputArgument {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Make a new Input Argument
|
||||||
|
*
|
||||||
|
* @param mixed $name The long name of the option, or an array with all the properites.
|
||||||
|
* @param string $help The help text for this option
|
||||||
|
* @param boolean $required Whether this argument is required. Missing required args will trigger exceptions
|
||||||
|
* @param arraty $choices Valid choices for this option.
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __construct($name, $help = '', $required = false, $choices = array()) {
|
||||||
|
if (is_array($name) && isset($name['name'])) {
|
||||||
|
foreach ($name as $key => $value) {
|
||||||
|
$this->{$key} = $value;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$this->name = $name;
|
||||||
|
$this->help = $help;
|
||||||
|
$this->required = $required;
|
||||||
|
$this->choices = $choices;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the name of the argument
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function name() {
|
||||||
|
return $this->name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate the help for this this argument.
|
||||||
|
*
|
||||||
|
* @param int $width The width to make the name of the option.
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function help($width = 0) {
|
||||||
|
$name = $this->name;
|
||||||
|
if (strlen($name) < $width) {
|
||||||
|
$name = str_pad($name, $width, ' ');
|
||||||
|
}
|
||||||
|
$optional = '';
|
||||||
|
if (!$this->isRequired()) {
|
||||||
|
$optional = ' <comment>(optional)</comment>';
|
||||||
|
}
|
||||||
|
return sprintf('%s%s%s', $name, $this->help, $optional);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the usage value for this argument
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function usage() {
|
||||||
|
$name = $this->name;
|
||||||
|
if (!$this->isRequired()) {
|
||||||
|
$name = '[' . $name . ']';
|
||||||
|
}
|
||||||
|
return $name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if this argument is a required argument
|
||||||
|
*
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
public function isRequired() {
|
||||||
|
return (bool) $this->required;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue