Skip to content

Commit

Permalink
Pulling out classes into separate files.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Oct 14, 2010
1 parent 2774493 commit 5e8222e
Show file tree
Hide file tree
Showing 3 changed files with 209 additions and 173 deletions.
99 changes: 99 additions & 0 deletions cake/console/console_input_argument.php
@@ -0,0 +1,99 @@
<?php
/**
* ConsoleArgumentOption 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)
*/
/**
* 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 string
*/
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;
}
}
108 changes: 108 additions & 0 deletions cake/console/console_input_option.php
@@ -0,0 +1,108 @@
<?php
/**
* ConsoleInputOption 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)
*/

/**
* An object to represent a single option used in the command line.
* ConsoleOptionParser creates these when you use addOption()
*
* @see ConsoleOptionParser::addOption()
* @package cake.console
*/
class ConsoleInputOption {

/**
* Make a new Input Option
*
* @param mixed $name The long name of the option, or an array with all the properites.
* @param string $short The short alias for this option
* @param string $help The help text for this option
* @param boolean $boolean Whether this option is a boolean option. Boolean options don't consume extra tokens
* @param string $default The default value for this option.
* @param arraty $choices Valid choices for this option.
* @return void
*/
public function __construct($name, $short = null, $help = '', $boolean = false, $default = '', $choices = array()) {
if (is_array($name) && isset($name['name'])) {
foreach ($name as $key => $value) {
$this->{$key} = $value;
}
} else {
$this->name = $name;
$this->short = $short;
$this->help = $help;
$this->boolean = $boolean;
$this->default = $default;
$this->choices = $choices;
}
}

/**
* Generate the help for this this option.
*
* @param int $width The width to make the name of the option.
* @return string
*/
public function help($width = 0) {
$default = $short = '';
if (!empty($this->default) && $this->default !== true) {
$default = sprintf(__(' <comment>(default: %s)</comment>'), $this->default);
}
if (!empty($this->short)) {
$short = ', -' . $this->short;
}
$name = sprintf('--%s%s', $this->name, $short);
if (strlen($name) < $width) {
$name = str_pad($name, $width, ' ');
}
return sprintf('%s%s%s', $name, $this->help, $default);
}

/**
* Get the usage value for this option
*
* @return string
*/
public function usage() {
$name = empty($this->short) ? '--' . $this->name : '-' . $this->short;
$default = '';
if (!empty($this->default) && $this->default !== true) {
$default = ' ' . $this->default;
}
return sprintf('[%s%s]', $name, $default);
}

/**
* Get the default value for this option
*
* @return void
*/
public function defaultValue() {
return $this->default;
}

/**
* Check if this option is a boolean option
*
* @return boolean
*/
public function isBoolean() {
return (bool) $this->boolean;
}
}
175 changes: 2 additions & 173 deletions cake/console/console_option_parser.php
Expand Up @@ -17,6 +17,8 @@
* @since CakePHP(tm) v 2.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
require_once 'console_input_option.php';
require_once 'console_input_argument.php';

/**
* Handles parsing the ARGV in the command line and provides support
Expand Down Expand Up @@ -546,176 +548,3 @@ protected function _nextToken() {
return isset($this->_tokens[0]) ? $this->_tokens[0] : '';
}
}


/**
* An object to represent a single option used in the command line.
* ConsoleOptionParser creates these when you use addOption()
*
* @see ConsoleOptionParser::addOption()
* @package cake.console
*/
class ConsoleInputOption {

/**
* Make a new Input Option
*
* @param mixed $name The long name of the option, or an array with all the properites.
* @param string $short The short alias for this option
* @param string $help The help text for this option
* @param boolean $boolean Whether this option is a boolean option. Boolean options don't consume extra tokens
* @param string $default The default value for this option.
* @param arraty $choices Valid choices for this option.
* @return void
*/
public function __construct($name, $short = null, $help = '', $boolean = false, $default = '', $choices = array()) {
if (is_array($name) && isset($name['name'])) {
foreach ($name as $key => $value) {
$this->{$key} = $value;
}
} else {
$this->name = $name;
$this->short = $short;
$this->help = $help;
$this->boolean = $boolean;
$this->default = $default;
$this->choices = $choices;
}
}

/**
* Generate the help for this this option.
*
* @param int $width The width to make the name of the option.
* @return string
*/
public function help($width = 0) {
$default = $short = '';
if (!empty($this->default) && $this->default !== true) {
$default = sprintf(__(' <comment>(default: %s)</comment>'), $this->default);
}
if (!empty($this->short)) {
$short = ', -' . $this->short;
}
$name = sprintf('--%s%s', $this->name, $short);
if (strlen($name) < $width) {
$name = str_pad($name, $width, ' ');
}
return sprintf('%s%s%s', $name, $this->help, $default);
}

/**
* Get the usage value for this option
*
* @return string
*/
public function usage() {
$name = empty($this->short) ? '--' . $this->name : '-' . $this->short;
$default = '';
if (!empty($this->default) && $this->default !== true) {
$default = ' ' . $this->default;
}
return sprintf('[%s%s]', $name, $default);
}

/**
* Get the default value for this option
*
* @return void
*/
public function defaultValue() {
return $this->default;
}

/**
* Check if this option is a boolean option
*
* @return boolean
*/
public function isBoolean() {
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;
}
}

0 comments on commit 5e8222e

Please sign in to comment.