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@17
|
36 * @param string $name The option name
|
Chris@17
|
37 * @param string|array|null $shortcut The shortcuts, can be null, a string of shortcuts delimited by | or an array of shortcuts
|
Chris@17
|
38 * @param int|null $mode The option mode: One of the VALUE_* constants
|
Chris@17
|
39 * @param string $description A description text
|
Chris@17
|
40 * @param string|string[]|int|bool|null $default The default value (must be null for self::VALUE_NONE)
|
Chris@0
|
41 *
|
Chris@0
|
42 * @throws InvalidArgumentException If option mode is invalid or incompatible
|
Chris@0
|
43 */
|
Chris@0
|
44 public function __construct($name, $shortcut = null, $mode = null, $description = '', $default = null)
|
Chris@0
|
45 {
|
Chris@0
|
46 if (0 === strpos($name, '--')) {
|
Chris@0
|
47 $name = substr($name, 2);
|
Chris@0
|
48 }
|
Chris@0
|
49
|
Chris@0
|
50 if (empty($name)) {
|
Chris@0
|
51 throw new InvalidArgumentException('An option name cannot be empty.');
|
Chris@0
|
52 }
|
Chris@0
|
53
|
Chris@0
|
54 if (empty($shortcut)) {
|
Chris@0
|
55 $shortcut = null;
|
Chris@0
|
56 }
|
Chris@0
|
57
|
Chris@0
|
58 if (null !== $shortcut) {
|
Chris@17
|
59 if (\is_array($shortcut)) {
|
Chris@0
|
60 $shortcut = implode('|', $shortcut);
|
Chris@0
|
61 }
|
Chris@0
|
62 $shortcuts = preg_split('{(\|)-?}', ltrim($shortcut, '-'));
|
Chris@0
|
63 $shortcuts = array_filter($shortcuts);
|
Chris@0
|
64 $shortcut = implode('|', $shortcuts);
|
Chris@0
|
65
|
Chris@0
|
66 if (empty($shortcut)) {
|
Chris@0
|
67 throw new InvalidArgumentException('An option shortcut cannot be empty.');
|
Chris@0
|
68 }
|
Chris@0
|
69 }
|
Chris@0
|
70
|
Chris@0
|
71 if (null === $mode) {
|
Chris@0
|
72 $mode = self::VALUE_NONE;
|
Chris@17
|
73 } elseif (!\is_int($mode) || $mode > 15 || $mode < 1) {
|
Chris@0
|
74 throw new InvalidArgumentException(sprintf('Option mode "%s" is not valid.', $mode));
|
Chris@0
|
75 }
|
Chris@0
|
76
|
Chris@0
|
77 $this->name = $name;
|
Chris@0
|
78 $this->shortcut = $shortcut;
|
Chris@0
|
79 $this->mode = $mode;
|
Chris@0
|
80 $this->description = $description;
|
Chris@0
|
81
|
Chris@0
|
82 if ($this->isArray() && !$this->acceptValue()) {
|
Chris@0
|
83 throw new InvalidArgumentException('Impossible to have an option mode VALUE_IS_ARRAY if the option does not accept a value.');
|
Chris@0
|
84 }
|
Chris@0
|
85
|
Chris@0
|
86 $this->setDefault($default);
|
Chris@0
|
87 }
|
Chris@0
|
88
|
Chris@0
|
89 /**
|
Chris@0
|
90 * Returns the option shortcut.
|
Chris@0
|
91 *
|
Chris@17
|
92 * @return string|null The shortcut
|
Chris@0
|
93 */
|
Chris@0
|
94 public function getShortcut()
|
Chris@0
|
95 {
|
Chris@0
|
96 return $this->shortcut;
|
Chris@0
|
97 }
|
Chris@0
|
98
|
Chris@0
|
99 /**
|
Chris@0
|
100 * Returns the option name.
|
Chris@0
|
101 *
|
Chris@0
|
102 * @return string The name
|
Chris@0
|
103 */
|
Chris@0
|
104 public function getName()
|
Chris@0
|
105 {
|
Chris@0
|
106 return $this->name;
|
Chris@0
|
107 }
|
Chris@0
|
108
|
Chris@0
|
109 /**
|
Chris@0
|
110 * Returns true if the option accepts a value.
|
Chris@0
|
111 *
|
Chris@0
|
112 * @return bool true if value mode is not self::VALUE_NONE, false otherwise
|
Chris@0
|
113 */
|
Chris@0
|
114 public function acceptValue()
|
Chris@0
|
115 {
|
Chris@0
|
116 return $this->isValueRequired() || $this->isValueOptional();
|
Chris@0
|
117 }
|
Chris@0
|
118
|
Chris@0
|
119 /**
|
Chris@0
|
120 * Returns true if the option requires a value.
|
Chris@0
|
121 *
|
Chris@0
|
122 * @return bool true if value mode is self::VALUE_REQUIRED, false otherwise
|
Chris@0
|
123 */
|
Chris@0
|
124 public function isValueRequired()
|
Chris@0
|
125 {
|
Chris@0
|
126 return self::VALUE_REQUIRED === (self::VALUE_REQUIRED & $this->mode);
|
Chris@0
|
127 }
|
Chris@0
|
128
|
Chris@0
|
129 /**
|
Chris@0
|
130 * Returns true if the option takes an optional value.
|
Chris@0
|
131 *
|
Chris@0
|
132 * @return bool true if value mode is self::VALUE_OPTIONAL, false otherwise
|
Chris@0
|
133 */
|
Chris@0
|
134 public function isValueOptional()
|
Chris@0
|
135 {
|
Chris@0
|
136 return self::VALUE_OPTIONAL === (self::VALUE_OPTIONAL & $this->mode);
|
Chris@0
|
137 }
|
Chris@0
|
138
|
Chris@0
|
139 /**
|
Chris@0
|
140 * Returns true if the option can take multiple values.
|
Chris@0
|
141 *
|
Chris@0
|
142 * @return bool true if mode is self::VALUE_IS_ARRAY, false otherwise
|
Chris@0
|
143 */
|
Chris@0
|
144 public function isArray()
|
Chris@0
|
145 {
|
Chris@0
|
146 return self::VALUE_IS_ARRAY === (self::VALUE_IS_ARRAY & $this->mode);
|
Chris@0
|
147 }
|
Chris@0
|
148
|
Chris@0
|
149 /**
|
Chris@0
|
150 * Sets the default value.
|
Chris@0
|
151 *
|
Chris@17
|
152 * @param string|string[]|int|bool|null $default The default value
|
Chris@0
|
153 *
|
Chris@0
|
154 * @throws LogicException When incorrect default value is given
|
Chris@0
|
155 */
|
Chris@0
|
156 public function setDefault($default = null)
|
Chris@0
|
157 {
|
Chris@0
|
158 if (self::VALUE_NONE === (self::VALUE_NONE & $this->mode) && null !== $default) {
|
Chris@0
|
159 throw new LogicException('Cannot set a default value when using InputOption::VALUE_NONE mode.');
|
Chris@0
|
160 }
|
Chris@0
|
161
|
Chris@0
|
162 if ($this->isArray()) {
|
Chris@0
|
163 if (null === $default) {
|
Chris@17
|
164 $default = [];
|
Chris@17
|
165 } elseif (!\is_array($default)) {
|
Chris@0
|
166 throw new LogicException('A default value for an array option must be an array.');
|
Chris@0
|
167 }
|
Chris@0
|
168 }
|
Chris@0
|
169
|
Chris@0
|
170 $this->default = $this->acceptValue() ? $default : false;
|
Chris@0
|
171 }
|
Chris@0
|
172
|
Chris@0
|
173 /**
|
Chris@0
|
174 * Returns the default value.
|
Chris@0
|
175 *
|
Chris@17
|
176 * @return string|string[]|int|bool|null The default value
|
Chris@0
|
177 */
|
Chris@0
|
178 public function getDefault()
|
Chris@0
|
179 {
|
Chris@0
|
180 return $this->default;
|
Chris@0
|
181 }
|
Chris@0
|
182
|
Chris@0
|
183 /**
|
Chris@0
|
184 * Returns the description text.
|
Chris@0
|
185 *
|
Chris@0
|
186 * @return string The description text
|
Chris@0
|
187 */
|
Chris@0
|
188 public function getDescription()
|
Chris@0
|
189 {
|
Chris@0
|
190 return $this->description;
|
Chris@0
|
191 }
|
Chris@0
|
192
|
Chris@0
|
193 /**
|
Chris@0
|
194 * Checks whether the given option equals this one.
|
Chris@0
|
195 *
|
Chris@0
|
196 * @return bool
|
Chris@0
|
197 */
|
Chris@16
|
198 public function equals(self $option)
|
Chris@0
|
199 {
|
Chris@0
|
200 return $option->getName() === $this->getName()
|
Chris@0
|
201 && $option->getShortcut() === $this->getShortcut()
|
Chris@0
|
202 && $option->getDefault() === $this->getDefault()
|
Chris@0
|
203 && $option->isArray() === $this->isArray()
|
Chris@0
|
204 && $option->isValueRequired() === $this->isValueRequired()
|
Chris@0
|
205 && $option->isValueOptional() === $this->isValueOptional()
|
Chris@0
|
206 ;
|
Chris@0
|
207 }
|
Chris@0
|
208 }
|