0
|
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 * Class that represent a command line element (an option, or an argument).
|
|
27 *
|
|
28 * @category Console
|
|
29 * @package Console_CommandLine
|
|
30 * @author David JEAN LOUIS <izimobil@gmail.com>
|
|
31 * @copyright 2007 David JEAN LOUIS
|
|
32 * @license http://opensource.org/licenses/mit-license.php MIT License
|
|
33 * @version Release: @package_version@
|
|
34 * @link http://pear.php.net/package/Console_CommandLine
|
|
35 * @since Class available since release 0.1.0
|
|
36 */
|
|
37 abstract class Console_CommandLine_Element
|
|
38 {
|
|
39 // Public properties {{{
|
|
40
|
|
41 /**
|
|
42 * The element name.
|
|
43 *
|
|
44 * @var string $name Element name
|
|
45 */
|
|
46 public $name;
|
|
47
|
|
48 /**
|
|
49 * The name of variable displayed in the usage message, if no set it
|
|
50 * defaults to the "name" property.
|
|
51 *
|
|
52 * @var string $help_name Element "help" variable name
|
|
53 */
|
|
54 public $help_name;
|
|
55
|
|
56 /**
|
|
57 * The element description.
|
|
58 *
|
|
59 * @var string $description Element description
|
|
60 */
|
|
61 public $description;
|
|
62
|
|
63 /**
|
|
64 * The default value of the element if not provided on the command line.
|
|
65 *
|
|
66 * @var mixed $default Default value of the option.
|
|
67 */
|
|
68 public $default;
|
|
69
|
|
70 /**
|
|
71 * Custom errors messages for this element
|
|
72 *
|
|
73 * This array is of the form:
|
|
74 * <code>
|
|
75 * <?php
|
|
76 * array(
|
|
77 * $messageName => $messageText,
|
|
78 * $messageName => $messageText,
|
|
79 * ...
|
|
80 * );
|
|
81 * ?>
|
|
82 * </code>
|
|
83 *
|
|
84 * If specified, these messages override the messages provided by the
|
|
85 * default message provider. For example:
|
|
86 * <code>
|
|
87 * <?php
|
|
88 * $messages = array(
|
|
89 * 'ARGUMENT_REQUIRED' => 'The argument foo is required.',
|
|
90 * );
|
|
91 * ?>
|
|
92 * </code>
|
|
93 *
|
|
94 * @var array
|
|
95 * @see Console_CommandLine_MessageProvider_Default
|
|
96 */
|
|
97 public $messages = array();
|
|
98
|
|
99 // }}}
|
|
100 // __construct() {{{
|
|
101
|
|
102 /**
|
|
103 * Constructor.
|
|
104 *
|
|
105 * @param string $name The name of the element
|
|
106 * @param array $params An optional array of parameters
|
|
107 *
|
|
108 * @return void
|
|
109 */
|
|
110 public function __construct($name = null, $params = array())
|
|
111 {
|
|
112 $this->name = $name;
|
|
113 foreach ($params as $attr => $value) {
|
|
114 if (property_exists($this, $attr)) {
|
|
115 $this->$attr = $value;
|
|
116 }
|
|
117 }
|
|
118 }
|
|
119
|
|
120 // }}}
|
|
121 // toString() {{{
|
|
122
|
|
123 /**
|
|
124 * Returns the string representation of the element.
|
|
125 *
|
|
126 * @return string The string representation of the element
|
|
127 * @todo use __toString() instead
|
|
128 */
|
|
129 public function toString()
|
|
130 {
|
|
131 return $this->help_name;
|
|
132 }
|
|
133 // }}}
|
|
134 // validate() {{{
|
|
135
|
|
136 /**
|
|
137 * Validates the element instance and set it's default values.
|
|
138 *
|
|
139 * @return void
|
|
140 * @throws Console_CommandLine_Exception
|
|
141 */
|
|
142 public function validate()
|
|
143 {
|
|
144 // if no help_name passed, default to name
|
|
145 if ($this->help_name == null) {
|
|
146 $this->help_name = $this->name;
|
|
147 }
|
|
148 }
|
|
149
|
|
150 // }}}
|
|
151 }
|