| 16591 |
anikendra |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* ConsoleArgumentOption file
|
|
|
4 |
*
|
|
|
5 |
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
|
|
6 |
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
7 |
*
|
|
|
8 |
* Licensed under The MIT License
|
|
|
9 |
* For full copyright and license information, please see the LICENSE.txt
|
|
|
10 |
* Redistributions of files must retain the above copyright notice.
|
|
|
11 |
*
|
|
|
12 |
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
13 |
* @link http://cakephp.org CakePHP(tm) Project
|
|
|
14 |
* @since CakePHP(tm) v 2.0
|
|
|
15 |
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
|
|
16 |
*/
|
|
|
17 |
|
|
|
18 |
/**
|
|
|
19 |
* An object to represent a single argument used in the command line.
|
|
|
20 |
* ConsoleOptionParser creates these when you use addArgument()
|
|
|
21 |
*
|
|
|
22 |
* @see ConsoleOptionParser::addArgument()
|
|
|
23 |
* @package Cake.Console
|
|
|
24 |
*/
|
|
|
25 |
class ConsoleInputArgument {
|
|
|
26 |
|
|
|
27 |
/**
|
|
|
28 |
* Name of the argument.
|
|
|
29 |
*
|
|
|
30 |
* @var string
|
|
|
31 |
*/
|
|
|
32 |
protected $_name;
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* Help string
|
|
|
36 |
*
|
|
|
37 |
* @var string
|
|
|
38 |
*/
|
|
|
39 |
protected $_help;
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* Is this option required?
|
|
|
43 |
*
|
|
|
44 |
* @var bool
|
|
|
45 |
*/
|
|
|
46 |
protected $_required;
|
|
|
47 |
|
|
|
48 |
/**
|
|
|
49 |
* An array of valid choices for this argument.
|
|
|
50 |
*
|
|
|
51 |
* @var array
|
|
|
52 |
*/
|
|
|
53 |
protected $_choices;
|
|
|
54 |
|
|
|
55 |
/**
|
|
|
56 |
* Make a new Input Argument
|
|
|
57 |
*
|
|
|
58 |
* @param string|array $name The long name of the option, or an array with all the properties.
|
|
|
59 |
* @param string $help The help text for this option
|
|
|
60 |
* @param bool $required Whether this argument is required. Missing required args will trigger exceptions
|
|
|
61 |
* @param array $choices Valid choices for this option.
|
|
|
62 |
*/
|
|
|
63 |
public function __construct($name, $help = '', $required = false, $choices = array()) {
|
|
|
64 |
if (is_array($name) && isset($name['name'])) {
|
|
|
65 |
foreach ($name as $key => $value) {
|
|
|
66 |
$this->{'_' . $key} = $value;
|
|
|
67 |
}
|
|
|
68 |
} else {
|
|
|
69 |
$this->_name = $name;
|
|
|
70 |
$this->_help = $help;
|
|
|
71 |
$this->_required = $required;
|
|
|
72 |
$this->_choices = $choices;
|
|
|
73 |
}
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
/**
|
|
|
77 |
* Get the value of the name attribute.
|
|
|
78 |
*
|
|
|
79 |
* @return string Value of this->_name.
|
|
|
80 |
*/
|
|
|
81 |
public function name() {
|
|
|
82 |
return $this->_name;
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
/**
|
|
|
86 |
* Generate the help for this argument.
|
|
|
87 |
*
|
|
|
88 |
* @param int $width The width to make the name of the option.
|
|
|
89 |
* @return string
|
|
|
90 |
*/
|
|
|
91 |
public function help($width = 0) {
|
|
|
92 |
$name = $this->_name;
|
|
|
93 |
if (strlen($name) < $width) {
|
|
|
94 |
$name = str_pad($name, $width, ' ');
|
|
|
95 |
}
|
|
|
96 |
$optional = '';
|
|
|
97 |
if (!$this->isRequired()) {
|
|
|
98 |
$optional = __d('cake_console', ' <comment>(optional)</comment>');
|
|
|
99 |
}
|
|
|
100 |
if (!empty($this->_choices)) {
|
|
|
101 |
$optional .= __d('cake_console', ' <comment>(choices: %s)</comment>', implode('|', $this->_choices));
|
|
|
102 |
}
|
|
|
103 |
return sprintf('%s%s%s', $name, $this->_help, $optional);
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
/**
|
|
|
107 |
* Get the usage value for this argument
|
|
|
108 |
*
|
|
|
109 |
* @return string
|
|
|
110 |
*/
|
|
|
111 |
public function usage() {
|
|
|
112 |
$name = $this->_name;
|
|
|
113 |
if (!empty($this->_choices)) {
|
|
|
114 |
$name = implode('|', $this->_choices);
|
|
|
115 |
}
|
|
|
116 |
$name = '<' . $name . '>';
|
|
|
117 |
if (!$this->isRequired()) {
|
|
|
118 |
$name = '[' . $name . ']';
|
|
|
119 |
}
|
|
|
120 |
return $name;
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
/**
|
|
|
124 |
* Check if this argument is a required argument
|
|
|
125 |
*
|
|
|
126 |
* @return bool
|
|
|
127 |
*/
|
|
|
128 |
public function isRequired() {
|
|
|
129 |
return (bool)$this->_required;
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
/**
|
|
|
133 |
* Check that $value is a valid choice for this argument.
|
|
|
134 |
*
|
|
|
135 |
* @param string $value The choice to validate.
|
|
|
136 |
* @return bool
|
|
|
137 |
* @throws ConsoleException
|
|
|
138 |
*/
|
|
|
139 |
public function validChoice($value) {
|
|
|
140 |
if (empty($this->_choices)) {
|
|
|
141 |
return true;
|
|
|
142 |
}
|
|
|
143 |
if (!in_array($value, $this->_choices)) {
|
|
|
144 |
throw new ConsoleException(
|
|
|
145 |
__d('cake_console', '"%s" is not a valid value for %s. Please use one of "%s"',
|
|
|
146 |
$value, $this->_name, implode(', ', $this->_choices)
|
|
|
147 |
));
|
|
|
148 |
}
|
|
|
149 |
return true;
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
/**
|
|
|
153 |
* Append this arguments XML representation to the passed in SimpleXml object.
|
|
|
154 |
*
|
|
|
155 |
* @param SimpleXmlElement $parent The parent element.
|
|
|
156 |
* @return SimpleXmlElement The parent with this argument appended.
|
|
|
157 |
*/
|
|
|
158 |
public function xml(SimpleXmlElement $parent) {
|
|
|
159 |
$option = $parent->addChild('argument');
|
|
|
160 |
$option->addAttribute('name', $this->_name);
|
|
|
161 |
$option->addAttribute('help', $this->_help);
|
|
|
162 |
$option->addAttribute('required', $this->isRequired());
|
|
|
163 |
$choices = $option->addChild('choices');
|
|
|
164 |
foreach ($this->_choices as $valid) {
|
|
|
165 |
$choices->addChild('choice', $valid);
|
|
|
166 |
}
|
|
|
167 |
return $parent;
|
|
|
168 |
}
|
|
|
169 |
|
|
|
170 |
}
|