Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\Console\Input;
|
Chris@0
|
13
|
Chris@0
|
14 use Symfony\Component\Console\Exception\InvalidArgumentException;
|
Chris@0
|
15 use Symfony\Component\Console\Exception\LogicException;
|
Chris@0
|
16
|
Chris@0
|
17 /**
|
Chris@0
|
18 * Represents a command line option.
|
Chris@0
|
19 *
|
Chris@0
|
20 * @author Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
21 */
|
Chris@0
|
22 class InputOption
|
Chris@0
|
23 {
|
Chris@0
|
24 const VALUE_NONE = 1;
|
Chris@0
|
25 const VALUE_REQUIRED = 2;
|
Chris@0
|
26 const VALUE_OPTIONAL = 4;
|
Chris@0
|
27 const VALUE_IS_ARRAY = 8;
|
Chris@0
|
28
|
Chris@0
|
29 private $name;
|
Chris@0
|
30 private $shortcut;
|
Chris@0
|
31 private $mode;
|
Chris@0
|
32 private $default;
|
Chris@0
|
33 private $description;
|
Chris@0
|
34
|
Chris@0
|
35 /**
|
Chris@0
|
36 * Constructor.
|
Chris@0
|
37 *
|
Chris@0
|
38 * @param string $name The option name
|
Chris@0
|
39 * @param string|array $shortcut The shortcuts, can be null, a string of shortcuts delimited by | or an array of shortcuts
|
Chris@0
|
40 * @param int $mode The option mode: One of the VALUE_* constants
|
Chris@0
|
41 * @param string $description A description text
|
Chris@0
|
42 * @param mixed $default The default value (must be null for self::VALUE_NONE)
|
Chris@0
|
43 *
|
Chris@0
|
44 * @throws InvalidArgumentException If option mode is invalid or incompatible
|
Chris@0
|
45 */
|
Chris@0
|
46 public function __construct($name, $shortcut = null, $mode = null, $description = '', $default = null)
|
Chris@0
|
47 {
|
Chris@0
|
48 if (0 === strpos($name, '--')) {
|
Chris@0
|
49 $name = substr($name, 2);
|
Chris@0
|
50 }
|
Chris@0
|
51
|
Chris@0
|
52 if (empty($name)) {
|
Chris@0
|
53 throw new InvalidArgumentException('An option name cannot be empty.');
|
Chris@0
|
54 }
|
Chris@0
|
55
|
Chris@0
|
56 if (empty($shortcut)) {
|
Chris@0
|
57 $shortcut = null;
|
Chris@0
|
58 }
|
Chris@0
|
59
|
Chris@0
|
60 if (null !== $shortcut) {
|
Chris@0
|
61 if (is_array($shortcut)) {
|
Chris@0
|
62 $shortcut = implode('|', $shortcut);
|
Chris@0
|
63 }
|
Chris@0
|
64 $shortcuts = preg_split('{(\|)-?}', ltrim($shortcut, '-'));
|
Chris@0
|
65 $shortcuts = array_filter($shortcuts);
|
Chris@0
|
66 $shortcut = implode('|', $shortcuts);
|
Chris@0
|
67
|
Chris@0
|
68 if (empty($shortcut)) {
|
Chris@0
|
69 throw new InvalidArgumentException('An option shortcut cannot be empty.');
|
Chris@0
|
70 }
|
Chris@0
|
71 }
|
Chris@0
|
72
|
Chris@0
|
73 if (null === $mode) {
|
Chris@0
|
74 $mode = self::VALUE_NONE;
|
Chris@0
|
75 } elseif (!is_int($mode) || $mode > 15 || $mode < 1) {
|
Chris@0
|
76 throw new InvalidArgumentException(sprintf('Option mode "%s" is not valid.', $mode));
|
Chris@0
|
77 }
|
Chris@0
|
78
|
Chris@0
|
79 $this->name = $name;
|
Chris@0
|
80 $this->shortcut = $shortcut;
|
Chris@0
|
81 $this->mode = $mode;
|
Chris@0
|
82 $this->description = $description;
|
Chris@0
|
83
|
Chris@0
|
84 if ($this->isArray() && !$this->acceptValue()) {
|
Chris@0
|
85 throw new InvalidArgumentException('Impossible to have an option mode VALUE_IS_ARRAY if the option does not accept a value.');
|
Chris@0
|
86 }
|
Chris@0
|
87
|
Chris@0
|
88 $this->setDefault($default);
|
Chris@0
|
89 }
|
Chris@0
|
90
|
Chris@0
|
91 /**
|
Chris@0
|
92 * Returns the option shortcut.
|
Chris@0
|
93 *
|
Chris@0
|
94 * @return string The shortcut
|
Chris@0
|
95 */
|
Chris@0
|
96 public function getShortcut()
|
Chris@0
|
97 {
|
Chris@0
|
98 return $this->shortcut;
|
Chris@0
|
99 }
|
Chris@0
|
100
|
Chris@0
|
101 /**
|
Chris@0
|
102 * Returns the option name.
|
Chris@0
|
103 *
|
Chris@0
|
104 * @return string The name
|
Chris@0
|
105 */
|
Chris@0
|
106 public function getName()
|
Chris@0
|
107 {
|
Chris@0
|
108 return $this->name;
|
Chris@0
|
109 }
|
Chris@0
|
110
|
Chris@0
|
111 /**
|
Chris@0
|
112 * Returns true if the option accepts a value.
|
Chris@0
|
113 *
|
Chris@0
|
114 * @return bool true if value mode is not self::VALUE_NONE, false otherwise
|
Chris@0
|
115 */
|
Chris@0
|
116 public function acceptValue()
|
Chris@0
|
117 {
|
Chris@0
|
118 return $this->isValueRequired() || $this->isValueOptional();
|
Chris@0
|
119 }
|
Chris@0
|
120
|
Chris@0
|
121 /**
|
Chris@0
|
122 * Returns true if the option requires a value.
|
Chris@0
|
123 *
|
Chris@0
|
124 * @return bool true if value mode is self::VALUE_REQUIRED, false otherwise
|
Chris@0
|
125 */
|
Chris@0
|
126 public function isValueRequired()
|
Chris@0
|
127 {
|
Chris@0
|
128 return self::VALUE_REQUIRED === (self::VALUE_REQUIRED & $this->mode);
|
Chris@0
|
129 }
|
Chris@0
|
130
|
Chris@0
|
131 /**
|
Chris@0
|
132 * Returns true if the option takes an optional value.
|
Chris@0
|
133 *
|
Chris@0
|
134 * @return bool true if value mode is self::VALUE_OPTIONAL, false otherwise
|
Chris@0
|
135 */
|
Chris@0
|
136 public function isValueOptional()
|
Chris@0
|
137 {
|
Chris@0
|
138 return self::VALUE_OPTIONAL === (self::VALUE_OPTIONAL & $this->mode);
|
Chris@0
|
139 }
|
Chris@0
|
140
|
Chris@0
|
141 /**
|
Chris@0
|
142 * Returns true if the option can take multiple values.
|
Chris@0
|
143 *
|
Chris@0
|
144 * @return bool true if mode is self::VALUE_IS_ARRAY, false otherwise
|
Chris@0
|
145 */
|
Chris@0
|
146 public function isArray()
|
Chris@0
|
147 {
|
Chris@0
|
148 return self::VALUE_IS_ARRAY === (self::VALUE_IS_ARRAY & $this->mode);
|
Chris@0
|
149 }
|
Chris@0
|
150
|
Chris@0
|
151 /**
|
Chris@0
|
152 * Sets the default value.
|
Chris@0
|
153 *
|
Chris@0
|
154 * @param mixed $default The default value
|
Chris@0
|
155 *
|
Chris@0
|
156 * @throws LogicException When incorrect default value is given
|
Chris@0
|
157 */
|
Chris@0
|
158 public function setDefault($default = null)
|
Chris@0
|
159 {
|
Chris@0
|
160 if (self::VALUE_NONE === (self::VALUE_NONE & $this->mode) && null !== $default) {
|
Chris@0
|
161 throw new LogicException('Cannot set a default value when using InputOption::VALUE_NONE mode.');
|
Chris@0
|
162 }
|
Chris@0
|
163
|
Chris@0
|
164 if ($this->isArray()) {
|
Chris@0
|
165 if (null === $default) {
|
Chris@0
|
166 $default = array();
|
Chris@0
|
167 } elseif (!is_array($default)) {
|
Chris@0
|
168 throw new LogicException('A default value for an array option must be an array.');
|
Chris@0
|
169 }
|
Chris@0
|
170 }
|
Chris@0
|
171
|
Chris@0
|
172 $this->default = $this->acceptValue() ? $default : false;
|
Chris@0
|
173 }
|
Chris@0
|
174
|
Chris@0
|
175 /**
|
Chris@0
|
176 * Returns the default value.
|
Chris@0
|
177 *
|
Chris@0
|
178 * @return mixed The default value
|
Chris@0
|
179 */
|
Chris@0
|
180 public function getDefault()
|
Chris@0
|
181 {
|
Chris@0
|
182 return $this->default;
|
Chris@0
|
183 }
|
Chris@0
|
184
|
Chris@0
|
185 /**
|
Chris@0
|
186 * Returns the description text.
|
Chris@0
|
187 *
|
Chris@0
|
188 * @return string The description text
|
Chris@0
|
189 */
|
Chris@0
|
190 public function getDescription()
|
Chris@0
|
191 {
|
Chris@0
|
192 return $this->description;
|
Chris@0
|
193 }
|
Chris@0
|
194
|
Chris@0
|
195 /**
|
Chris@0
|
196 * Checks whether the given option equals this one.
|
Chris@0
|
197 *
|
Chris@0
|
198 * @param InputOption $option option to compare
|
Chris@0
|
199 *
|
Chris@0
|
200 * @return bool
|
Chris@0
|
201 */
|
Chris@0
|
202 public function equals(InputOption $option)
|
Chris@0
|
203 {
|
Chris@0
|
204 return $option->getName() === $this->getName()
|
Chris@0
|
205 && $option->getShortcut() === $this->getShortcut()
|
Chris@0
|
206 && $option->getDefault() === $this->getDefault()
|
Chris@0
|
207 && $option->isArray() === $this->isArray()
|
Chris@0
|
208 && $option->isValueRequired() === $this->isValueRequired()
|
Chris@0
|
209 && $option->isValueOptional() === $this->isValueOptional()
|
Chris@0
|
210 ;
|
Chris@0
|
211 }
|
Chris@0
|
212 }
|