comparison vendor/pear/console_commandline/Console/CommandLine/Option.php @ 0:1e000243b222

vanilla 1.3.3 distro, I hope
author Charlie Root
date Thu, 04 Jan 2018 15:50:29 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:1e000243b222
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * This file is part of the PEAR Console_CommandLine package.
7 *
8 * PHP version 5
9 *
10 * LICENSE: This source file is subject to the MIT license that is available
11 * through the world-wide-web at the following URI:
12 * http://opensource.org/licenses/mit-license.php
13 *
14 * @category Console
15 * @package Console_CommandLine
16 * @author David JEAN LOUIS <izimobil@gmail.com>
17 * @copyright 2007 David JEAN LOUIS
18 * @license http://opensource.org/licenses/mit-license.php MIT License
19 * @version CVS: $Id$
20 * @link http://pear.php.net/package/Console_CommandLine
21 * @since File available since release 0.1.0
22 * @filesource
23 */
24
25 /**
26 * Required by this class.
27 */
28 require_once 'Console/CommandLine.php';
29 require_once 'Console/CommandLine/Element.php';
30
31 /**
32 * Class that represent a commandline option.
33 *
34 * @category Console
35 * @package Console_CommandLine
36 * @author David JEAN LOUIS <izimobil@gmail.com>
37 * @copyright 2007 David JEAN LOUIS
38 * @license http://opensource.org/licenses/mit-license.php MIT License
39 * @version Release: @package_version@
40 * @link http://pear.php.net/package/Console_CommandLine
41 * @since Class available since release 0.1.0
42 */
43 class Console_CommandLine_Option extends Console_CommandLine_Element
44 {
45 // Public properties {{{
46
47 /**
48 * The option short name (ex: -v).
49 *
50 * @var string $short_name Short name of the option
51 */
52 public $short_name;
53
54 /**
55 * The option long name (ex: --verbose).
56 *
57 * @var string $long_name Long name of the option
58 */
59 public $long_name;
60
61 /**
62 * The option action, defaults to "StoreString".
63 *
64 * @var string $action Option action
65 */
66 public $action = 'StoreString';
67
68 /**
69 * An array of possible values for the option. If this array is not empty
70 * and the value passed is not in the array an exception is raised.
71 * This only make sense for actions that accept values of course.
72 *
73 * @var array $choices Valid choices for the option
74 */
75 public $choices = array();
76
77 /**
78 * The callback function (or method) to call for an action of type
79 * Callback, this can be any callable supported by the php function
80 * call_user_func.
81 *
82 * Example:
83 *
84 * <code>
85 * $parser->addOption('myoption', array(
86 * 'short_name' => '-m',
87 * 'long_name' => '--myoption',
88 * 'action' => 'Callback',
89 * 'callback' => 'myCallbackFunction'
90 * ));
91 * </code>
92 *
93 * @var callable $callback The option callback
94 */
95 public $callback;
96
97 /**
98 * An associative array of additional params to pass to the class
99 * corresponding to the action, this array will also be passed to the
100 * callback defined for an action of type Callback, Example:
101 *
102 * <code>
103 * // for a custom action
104 * $parser->addOption('myoption', array(
105 * 'short_name' => '-m',
106 * 'long_name' => '--myoption',
107 * 'action' => 'MyCustomAction',
108 * 'action_params' => array('foo'=>true, 'bar'=>false)
109 * ));
110 *
111 * // if the user type:
112 * // $ <yourprogram> -m spam
113 * // in your MyCustomAction class the execute() method will be called
114 * // with the value 'spam' as first parameter and
115 * // array('foo'=>true, 'bar'=>false) as second parameter
116 * </code>
117 *
118 * @var array $action_params Additional parameters to pass to the action
119 */
120 public $action_params = array();
121
122 /**
123 * For options that expect an argument, this property tells the parser if
124 * the option argument is optional and can be ommited.
125 *
126 * @var bool $argumentOptional Whether the option arg is optional or not
127 */
128 public $argument_optional = false;
129
130 /**
131 * For options that uses the "choice" property only.
132 * Adds a --list-<choice> option to the parser that displays the list of
133 * choices for the option.
134 *
135 * @var bool $add_list_option Whether to add a list option or not
136 */
137 public $add_list_option = false;
138
139 // }}}
140 // Private properties {{{
141
142 /**
143 * When an action is called remember it to allow for multiple calls.
144 *
145 * @var object $action_instance Placeholder for action
146 */
147 private $_action_instance = null;
148
149 // }}}
150 // __construct() {{{
151
152 /**
153 * Constructor.
154 *
155 * @param string $name The name of the option
156 * @param array $params An optional array of parameters
157 *
158 * @return void
159 */
160 public function __construct($name = null, $params = array())
161 {
162 parent::__construct($name, $params);
163 if ($this->action == 'Password') {
164 // special case for Password action, password can be passed to the
165 // commandline or prompted by the parser
166 $this->argument_optional = true;
167 }
168 }
169
170 // }}}
171 // toString() {{{
172
173 /**
174 * Returns the string representation of the option.
175 *
176 * @param string $delim Delimiter to use between short and long option
177 *
178 * @return string The string representation of the option
179 * @todo use __toString() instead
180 */
181 public function toString($delim = ", ")
182 {
183 $ret = '';
184 $padding = '';
185 if ($this->short_name != null) {
186 $ret .= $this->short_name;
187 if ($this->expectsArgument()) {
188 $ret .= ' ' . $this->help_name;
189 }
190 $padding = $delim;
191 }
192 if ($this->long_name != null) {
193 $ret .= $padding . $this->long_name;
194 if ($this->expectsArgument()) {
195 $ret .= '=' . $this->help_name;
196 }
197 }
198 return $ret;
199 }
200
201 // }}}
202 // expectsArgument() {{{
203
204 /**
205 * Returns true if the option requires one or more argument and false
206 * otherwise.
207 *
208 * @return bool Whether the option expects an argument or not
209 */
210 public function expectsArgument()
211 {
212 if ($this->action == 'StoreTrue' || $this->action == 'StoreFalse' ||
213 $this->action == 'Help' || $this->action == 'Version' ||
214 $this->action == 'Counter' || $this->action == 'List') {
215 return false;
216 }
217 return true;
218 }
219
220 // }}}
221 // dispatchAction() {{{
222
223 /**
224 * Formats the value $value according to the action of the option and
225 * updates the passed Console_CommandLine_Result object.
226 *
227 * @param mixed $value The value to format
228 * @param Console_CommandLine_Result $result The result instance
229 * @param Console_CommandLine $parser The parser instance
230 *
231 * @return void
232 * @throws Console_CommandLine_Exception
233 */
234 public function dispatchAction($value, $result, $parser)
235 {
236 $actionInfo = Console_CommandLine::$actions[$this->action];
237 if (true === $actionInfo[1]) {
238 // we have a "builtin" action
239 $tokens = explode('_', $actionInfo[0]);
240 include_once implode('/', $tokens) . '.php';
241 }
242 $clsname = $actionInfo[0];
243 if ($this->_action_instance === null) {
244 $this->_action_instance = new $clsname($result, $this, $parser);
245 }
246
247 // check value is in option choices
248 if (!empty($this->choices) && !in_array($this->_action_instance->format($value), $this->choices)) {
249 throw Console_CommandLine_Exception::factory(
250 'OPTION_VALUE_NOT_VALID',
251 array(
252 'name' => $this->name,
253 'choices' => implode('", "', $this->choices),
254 'value' => $value,
255 ),
256 $parser,
257 $this->messages
258 );
259 }
260 $this->_action_instance->execute($value, $this->action_params);
261 }
262
263 // }}}
264 // validate() {{{
265
266 /**
267 * Validates the option instance.
268 *
269 * @return void
270 * @throws Console_CommandLine_Exception
271 * @todo use exceptions instead
272 */
273 public function validate()
274 {
275 // check if the option name is valid
276 if (!preg_match('/^[a-zA-Z_\x7f-\xff]+[a-zA-Z0-9_\x7f-\xff]*$/',
277 $this->name)) {
278 Console_CommandLine::triggerError('option_bad_name',
279 E_USER_ERROR, array('{$name}' => $this->name));
280 }
281 // call the parent validate method
282 parent::validate();
283 // a short_name or a long_name must be provided
284 if ($this->short_name == null && $this->long_name == null) {
285 Console_CommandLine::triggerError('option_long_and_short_name_missing',
286 E_USER_ERROR, array('{$name}' => $this->name));
287 }
288 // check if the option short_name is valid
289 if ($this->short_name != null &&
290 !(preg_match('/^\-[a-zA-Z]{1}$/', $this->short_name))) {
291 Console_CommandLine::triggerError('option_bad_short_name',
292 E_USER_ERROR, array(
293 '{$name}' => $this->name,
294 '{$short_name}' => $this->short_name
295 ));
296 }
297 // check if the option long_name is valid
298 if ($this->long_name != null &&
299 !preg_match('/^\-\-[a-zA-Z]+[a-zA-Z0-9_\-]*$/', $this->long_name)) {
300 Console_CommandLine::triggerError('option_bad_long_name',
301 E_USER_ERROR, array(
302 '{$name}' => $this->name,
303 '{$long_name}' => $this->long_name
304 ));
305 }
306 // check if we have a valid action
307 if (!is_string($this->action)) {
308 Console_CommandLine::triggerError('option_bad_action',
309 E_USER_ERROR, array('{$name}' => $this->name));
310 }
311 if (!isset(Console_CommandLine::$actions[$this->action])) {
312 Console_CommandLine::triggerError('option_unregistered_action',
313 E_USER_ERROR, array(
314 '{$action}' => $this->action,
315 '{$name}' => $this->name
316 ));
317 }
318 // if the action is a callback, check that we have a valid callback
319 if ($this->action == 'Callback' && !is_callable($this->callback)) {
320 Console_CommandLine::triggerError('option_invalid_callback',
321 E_USER_ERROR, array('{$name}' => $this->name));
322 }
323 }
324
325 // }}}
326 // setDefaults() {{{
327
328 /**
329 * Set the default value according to the configured action.
330 *
331 * Note that for backward compatibility issues this method is only called
332 * when the 'force_options_defaults' is set to true, it will become the
333 * default behaviour in the next major release of Console_CommandLine.
334 *
335 * @return void
336 */
337 public function setDefaults()
338 {
339 if ($this->default !== null) {
340 // already set
341 return;
342 }
343 switch ($this->action) {
344 case 'Counter':
345 case 'StoreInt':
346 $this->default = 0;
347 break;
348 case 'StoreFloat':
349 $this->default = 0.0;
350 break;
351 case 'StoreArray':
352 $this->default = array();
353 break;
354 case 'StoreTrue':
355 $this->default = false;
356 break;
357 case 'StoreFalse':
358 $this->default = true;
359 break;
360 default:
361 return;
362 }
363 }
364
365 // }}}
366 }