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