2010-10-10 18:50:20 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ConsoleInputSubcommand file
|
|
|
|
*
|
2017-06-10 21:33:55 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
|
2017-06-10 22:10:52 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2010-10-10 18:50:20 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2010-10-10 18:50:20 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2017-06-10 22:10:52 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2017-06-10 21:33:55 +00:00
|
|
|
* @link https://cakephp.org CakePHP(tm) Project
|
2010-10-10 18:50:20 +00:00
|
|
|
* @since CakePHP(tm) v 2.0
|
2017-06-10 22:23:14 +00:00
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License
|
2010-10-10 18:50:20 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An object to represent a single subcommand used in the command line.
|
2011-07-26 01:46:52 +00:00
|
|
|
* Created when you call ConsoleOptionParser::addSubcommand()
|
2010-10-10 18:50:20 +00:00
|
|
|
*
|
|
|
|
* @see ConsoleOptionParser::addSubcommand()
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Console
|
2010-10-10 18:50:20 +00:00
|
|
|
*/
|
|
|
|
class ConsoleInputSubcommand {
|
|
|
|
|
2010-12-22 04:11:23 +00:00
|
|
|
/**
|
|
|
|
* Name of the subcommand
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $_name;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Help string for the subcommand
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2011-08-16 03:55:08 +00:00
|
|
|
protected $_help;
|
2010-12-22 04:11:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The ConsoleOptionParser for this subcommand.
|
|
|
|
*
|
|
|
|
* @var ConsoleOptionParser
|
|
|
|
*/
|
|
|
|
protected $_parser;
|
2010-10-10 18:55:15 +00:00
|
|
|
|
2010-10-10 18:50:20 +00:00
|
|
|
/**
|
|
|
|
* Make a new Subcommand
|
|
|
|
*
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param string|array $name The long name of the subcommand, or an array with all the properties.
|
2010-10-10 18:50:20 +00:00
|
|
|
* @param string $help The help text for this option
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param ConsoleOptionParser|array $parser A parser for this subcommand. Either a ConsoleOptionParser, or an array that can be
|
2010-12-22 04:11:23 +00:00
|
|
|
* used with ConsoleOptionParser::buildFromArray()
|
2010-10-10 18:50:20 +00:00
|
|
|
*/
|
|
|
|
public function __construct($name, $help = '', $parser = null) {
|
|
|
|
if (is_array($name) && isset($name['name'])) {
|
|
|
|
foreach ($name as $key => $value) {
|
2010-10-10 18:55:15 +00:00
|
|
|
$this->{'_' . $key} = $value;
|
2010-10-10 18:50:20 +00:00
|
|
|
}
|
|
|
|
} else {
|
2010-10-10 18:55:15 +00:00
|
|
|
$this->_name = $name;
|
|
|
|
$this->_help = $help;
|
|
|
|
$this->_parser = $parser;
|
2010-10-10 18:50:20 +00:00
|
|
|
}
|
2010-10-10 20:44:22 +00:00
|
|
|
if (is_array($this->_parser)) {
|
|
|
|
$this->_parser['command'] = $this->_name;
|
|
|
|
$this->_parser = ConsoleOptionParser::buildFromArray($this->_parser);
|
|
|
|
}
|
2010-10-10 18:50:20 +00:00
|
|
|
}
|
|
|
|
|
2010-11-13 04:34:52 +00:00
|
|
|
/**
|
2010-11-13 04:57:55 +00:00
|
|
|
* Get the value of the name attribute.
|
2010-11-13 04:34:52 +00:00
|
|
|
*
|
2010-11-13 04:57:55 +00:00
|
|
|
* @return string Value of this->_name.
|
2010-11-13 04:34:52 +00:00
|
|
|
*/
|
2010-11-13 04:57:55 +00:00
|
|
|
public function name() {
|
|
|
|
return $this->_name;
|
2010-11-13 04:34:52 +00:00
|
|
|
}
|
|
|
|
|
2010-10-10 18:50:20 +00:00
|
|
|
/**
|
|
|
|
* Generate the help for this this subcommand.
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param int $width The width to make the name of the subcommand.
|
2011-08-16 03:55:08 +00:00
|
|
|
* @return string
|
2010-10-10 18:50:20 +00:00
|
|
|
*/
|
|
|
|
public function help($width = 0) {
|
2010-10-10 18:55:15 +00:00
|
|
|
$name = $this->_name;
|
2010-10-10 18:50:20 +00:00
|
|
|
if (strlen($name) < $width) {
|
|
|
|
$name = str_pad($name, $width, ' ');
|
|
|
|
}
|
2010-10-10 18:55:15 +00:00
|
|
|
return $name . $this->_help;
|
2010-10-10 18:50:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the usage value for this option
|
|
|
|
*
|
2010-10-10 18:55:15 +00:00
|
|
|
* @return mixed Either false or a ConsoleOptionParser
|
2010-10-10 18:50:20 +00:00
|
|
|
*/
|
|
|
|
public function parser() {
|
2010-10-10 18:55:15 +00:00
|
|
|
if ($this->_parser instanceof ConsoleOptionParser) {
|
|
|
|
return $this->_parser;
|
2010-10-10 18:50:20 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2010-10-20 02:44:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Append this subcommand to the Parent element
|
|
|
|
*
|
2011-07-29 02:03:44 +00:00
|
|
|
* @param SimpleXmlElement $parent The parent element.
|
2010-10-20 02:44:46 +00:00
|
|
|
* @return SimpleXmlElement The parent with this subcommand appended.
|
|
|
|
*/
|
|
|
|
public function xml(SimpleXmlElement $parent) {
|
|
|
|
$command = $parent->addChild('command');
|
|
|
|
$command->addAttribute('name', $this->_name);
|
|
|
|
$command->addAttribute('help', $this->_help);
|
|
|
|
return $parent;
|
|
|
|
}
|
2012-03-03 23:55:07 +00:00
|
|
|
|
2010-10-10 18:50:20 +00:00
|
|
|
}
|