From 2774493577b9ef6dc369c53dc93a58d857bd39a3 Mon Sep 17 00:00:00 2001 From: mark_story Date: Sun, 10 Oct 2010 14:31:47 -0400 Subject: [PATCH] Refactoring arguments into a separate object. --- cake/console/console_option_parser.php | 128 ++++++++++++++++++------- 1 file changed, 96 insertions(+), 32 deletions(-) diff --git a/cake/console/console_option_parser.php b/cake/console/console_option_parser.php index 496b9d49f..53c8c21b8 100644 --- a/cake/console/console_option_parser.php +++ b/cake/console/console_option_parser.php @@ -228,15 +228,17 @@ class ConsoleOptionParser { * @return $this. */ public function addArgument($name, $params = array()) { - $index = count($this->_args); $defaults = array( 'name' => $name, 'help' => '', - 'index' => $index, + 'index' => count($this->_args), 'required' => false ); $options = array_merge($defaults, $params); - $this->_args[$options['index']] = $options; + $index = $options['index']; + unset($options['index']); + + $this->_args[$index] = new ConsoleInputArgument($options); return $this; } @@ -336,9 +338,9 @@ class ConsoleOptionParser { } } foreach ($this->_args as $i => $arg) { - if ($arg['required'] && !isset($args[$i])) { + if ($arg->isRequired() && !isset($args[$i])) { 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)) { $max = 0; - foreach ($this->_args as $description) { - $max = (strlen($description['name']) > $max) ? strlen($description['name']) : $max; + foreach ($this->_args as $argument) { + $max = (strlen($argument->name) > $max) ? strlen($argument->name) : $max; } $max += 2; $out[] = 'Arguments:'; $out[] = ''; - foreach ($this->_args as $description) { - $out[] = $this->_argumentHelp($description, $max); + foreach ($this->_args as $argument) { + $out[] = $argument->help($max); } $out[] = ''; } @@ -432,33 +434,12 @@ class ConsoleOptionParser { foreach ($this->_options as $option) { $usage[] = $option->usage(); } - foreach ($this->_args as $definition) { - $name = $definition['name']; - if (!$definition['required']) { - $name = '[' . $name . ']'; - } - $usage[] = $name; + foreach ($this->_args as $argument) { + $usage[] = $argument->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 = ' (optional)'; - } - return sprintf('%s%s%s', $name, $definition['help'], $optional); - } - /** * Generate help for a single subcommand. * @@ -655,3 +636,86 @@ class ConsoleInputOption { 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 = ' (optional)'; + } + 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; + } +}