|
13 | 13 |
|
14 | 14 | use Symfony\Component\Console\Exception\InvalidArgumentException;
|
15 | 15 | use Symfony\Component\Console\Exception\RuntimeException;
|
| 16 | +use function array_keys; |
16 | 17 |
|
17 | 18 | /**
|
18 | 19 | * Input is the base class for all concrete Input classes.
|
@@ -85,6 +86,35 @@ public function getArguments(): array
|
85 | 86 | return array_merge($this->definition->getArgumentDefaults(), $this->arguments);
|
86 | 87 | }
|
87 | 88 |
|
| 89 | + /** |
| 90 | + * Returns all the given arguments NOT merged with the default values. |
| 91 | + * |
| 92 | + * @param bool $strip Whether to return the raw parameters (false) or the values after the command name (true) |
| 93 | + * |
| 94 | + * @return array<string|bool|int|float|null|array<string|bool|int|float|null>> |
| 95 | + */ |
| 96 | + public function getRawArguments(bool $strip = false): array |
| 97 | + { |
| 98 | + if (!$strip) { |
| 99 | + return $this->arguments; |
| 100 | + } |
| 101 | + |
| 102 | + $arguments = []; |
| 103 | + $keep = false; |
| 104 | + foreach ($this->arguments as $argument) { |
| 105 | + if (!$keep && $argument === $this->getFirstArgument()) { |
| 106 | + $keep = true; |
| 107 | + |
| 108 | + continue; |
| 109 | + } |
| 110 | + if ($keep) { |
| 111 | + $arguments[] = $argument; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + return $arguments; |
| 116 | + } |
| 117 | + |
88 | 118 | public function getArgument(string $name): mixed
|
89 | 119 | {
|
90 | 120 | if (!$this->definition->hasArgument($name)) {
|
@@ -113,6 +143,16 @@ public function getOptions(): array
|
113 | 143 | return array_merge($this->definition->getOptionDefaults(), $this->options);
|
114 | 144 | }
|
115 | 145 |
|
| 146 | + /** |
| 147 | + * Returns all the given options NOT merged with the default values. |
| 148 | + * |
| 149 | + * @return array<string|bool|int|float|null|array<string|bool|int|float|null>> |
| 150 | + */ |
| 151 | + public function getRawOptions(): array |
| 152 | + { |
| 153 | + return $this->options; |
| 154 | + } |
| 155 | + |
116 | 156 | public function getOption(string $name): mixed
|
117 | 157 | {
|
118 | 158 | if ($this->definition->hasNegation($name)) {
|
@@ -171,4 +211,55 @@ public function getStream()
|
171 | 211 | {
|
172 | 212 | return $this->stream;
|
173 | 213 | }
|
| 214 | + |
| 215 | + /** |
| 216 | + * Returns a stringified representation of the options passed to the command. |
| 217 | + * |
| 218 | + * InputArguments MUST be escaped as well as the InputOption values passed to the command. |
| 219 | + * |
| 220 | + * @param string[] $optionNames Name of the options returned. If empty, all options are returned and non-passed or non-existent are ignored. |
| 221 | + * |
| 222 | + * @return list<string> |
| 223 | + */ |
| 224 | + public function unparse(array $optionNames = []): array |
| 225 | + { |
| 226 | + $rawOptions = $this->getRawOptions(); |
| 227 | + |
| 228 | + $filteredRawOptions = count($optionNames) === 0 |
| 229 | + ? $rawOptions |
| 230 | + : array_intersect_key($rawOptions, array_fill_keys($optionNames, ''), |
| 231 | + ); |
| 232 | + |
| 233 | + return array_map( |
| 234 | + fn (string $optionName) => $this->unparseOption( |
| 235 | + $this->definition->getOption($optionName), |
| 236 | + $optionName, |
| 237 | + $filteredRawOptions[$optionName], |
| 238 | + ), |
| 239 | + array_keys($filteredRawOptions), |
| 240 | + ); |
| 241 | + } |
| 242 | + |
| 243 | + /** |
| 244 | + * @param string|bool|int|float|null|array<string|bool|int|float|null> $value |
| 245 | + */ |
| 246 | + private function unparseOption( |
| 247 | + InputOption $option, |
| 248 | + string $name, |
| 249 | + array|bool|float|int|string|null $value, |
| 250 | + ): string { |
| 251 | + return match(true) { |
| 252 | + $option->isNegatable() => sprintf('--%s%s', $value ? '' : 'no-', $name), |
| 253 | + !$option->acceptValue() => sprintf('--%s', $name), |
| 254 | + $option->isArray() => implode('', array_map(fn($item) => $this->unparseOptionWithValue($name, $item), $value,)), |
| 255 | + default => $this->unparseOptionWithValue($name, $value), |
| 256 | + }; |
| 257 | + } |
| 258 | + |
| 259 | + private function unparseOptionWithValue( |
| 260 | + string $name, |
| 261 | + bool|float|int|string|null $value, |
| 262 | + ): string { |
| 263 | + return sprintf('--%s=%s', $name, $this->escapeToken($value)); |
| 264 | + } |
174 | 265 | }
|
0 commit comments