Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 25f2be2

Browse files
committed
[Console] Expose the original input arguments and options
PoC
1 parent 68a5704 commit 25f2be2

File tree

6 files changed

+643
-0
lines changed

6 files changed

+643
-0
lines changed

src/Symfony/Component/Console/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
CHANGELOG
22
=========
33

4+
7.2
5+
---
6+
7+
* Add `InputInterface::getRawArguments()`
8+
* Add `InputInterface::getRawOptions()`
9+
* Add `Input::unparse()`
10+
11+
412
7.1
513
---
614

src/Symfony/Component/Console/Input/Input.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Console\Exception\InvalidArgumentException;
1515
use Symfony\Component\Console\Exception\RuntimeException;
16+
use function array_keys;
1617

1718
/**
1819
* Input is the base class for all concrete Input classes.
@@ -85,6 +86,35 @@ public function getArguments(): array
8586
return array_merge($this->definition->getArgumentDefaults(), $this->arguments);
8687
}
8788

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+
88118
public function getArgument(string $name): mixed
89119
{
90120
if (!$this->definition->hasArgument($name)) {
@@ -113,6 +143,16 @@ public function getOptions(): array
113143
return array_merge($this->definition->getOptionDefaults(), $this->options);
114144
}
115145

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+
116156
public function getOption(string $name): mixed
117157
{
118158
if ($this->definition->hasNegation($name)) {
@@ -171,4 +211,55 @@ public function getStream()
171211
{
172212
return $this->stream;
173213
}
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+
}
174265
}

src/Symfony/Component/Console/Input/InputInterface.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
* InputInterface is the interface implemented by all input classes.
1919
*
2020
* @author Fabien Potencier <[email protected]>
21+
*
22+
* @method getRawArguments(bool $strip = false): array<string|bool|int|float|null|array<string|bool|int|float|null>> Returns all the given arguments NOT merged with the default values.
23+
* @method getRawOptions(): array<string|bool|int|float|null|array<string|bool|int|float|null>> Returns all the given options NOT merged with the default values.
2124
*/
2225
interface InputInterface
2326
{

0 commit comments

Comments
 (0)