Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Console] Expose the original input arguments and options and to unparse options #57598

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: 7.4
Choose a base branch
Loading
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
slightly adjust the API
  • Loading branch information
theofidry committed Apr 18, 2025
commit 9338ba833bf79f98f29e1a6a3bd6d3d8be1846e2
11 changes: 6 additions & 5 deletions 11 src/Symfony/Component/Console/Input/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,18 +194,19 @@ public function getStream()

/**
* Returns a stringified representation of the options passed to the command.
* The options must NOT be escaped as otherwise passing them to a `Process` would result in them being escaped twice.
*
* InputArguments must NOT be escaped as otherwise passing them to a `Process` would result in them being escaped twice.
*
* @param string[] $optionNames Names of the options returned. If empty, all options are returned and non-passed or non-existent are ignored.
* @param string[] $optionNames Names of the options returned. If null, all options are returned. Requested options
* that either do not exist or were not passed (even if the option has a default value)
* will not be part of the method output.
*
* @return list<string>
*/
public function unparse(array $optionNames = []): array
public function unparse(?array $optionNames = null): array
Copy link
Member

@GromNaN GromNaN Apr 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to how you would use this method in webmozarts/console-parallelization, you have a list of option names to exclude. This parameter should be $excludedOptions?

Copy link
Contributor Author

@theofidry theofidry Apr 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In WebmozartsConsoleParallelization we know the options we do NOT want, hence to get the ones we need to forward we need to juggle with the command definition. It is a specific use case for that feature, but not the only one, for instance you could want those values for specific options.

That's why I thought it would be better in Symfony to have a more re-usable piece of code (which is also simpler) even thought that means a tiny bit more work on my side.

{
$rawOptions = $this->getRawOptions();

$filteredRawOptions = 0 === \count($optionNames)
$filteredRawOptions = null === $optionNames
? $rawOptions
: array_intersect_key($rawOptions, array_fill_keys($optionNames, ''));

Expand Down
1 change: 1 addition & 0 deletions 1 src/Symfony/Component/Console/Input/InputInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*
* @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.
* @method getRawOptions(): array<string|bool|int|float|array<string|bool|int|float|null>|null> Returns all the given options NOT merged with the default values.
* @method unparse(): list<string> Returns a stringified representation of the options passed to the command.
*/
interface InputInterface
{
Expand Down
34 changes: 27 additions & 7 deletions 34 src/Symfony/Component/Console/Tests/Input/ArgvInputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ public static function unparseProvider(): iterable
yield 'empty input and empty definition' => [
new InputDefinition(),
new ArgvInput([]),
[],
null,
[],
];

Expand All @@ -635,7 +635,7 @@ public static function unparseProvider(): iterable
),
]),
new ArgvInput([]),
[],
null,
[],
];

Expand Down Expand Up @@ -669,14 +669,14 @@ public static function unparseProvider(): iterable
yield 'arguments & options: returns all passed options but ignore default values' => [
$completeInputDefinition,
new ArgvInput(['argValue', '--optWithoutDefaultValue=optValue']),
[],
null,
['--optWithoutDefaultValue=optValue'],
];

yield 'arguments & options; explicitly pass the default values: the default values are returned' => [
$completeInputDefinition,
new ArgvInput(['argValue', 'argDefaultValue', '--optWithoutDefaultValue=optValue', '--optWithDefaultValue=optDefaultValue']),
[],
null,
[
'--optWithoutDefaultValue=optValue',
'--optWithDefaultValue=optDefaultValue',
Expand All @@ -686,7 +686,7 @@ public static function unparseProvider(): iterable
yield 'arguments & options; no input definition: nothing returned' => [
null,
new ArgvInput(['argValue', 'argDefaultValue', '--optWithoutDefaultValue=optValue', '--optWithDefaultValue=optDefaultValue']),
[],
null,
[],
];

Expand All @@ -704,14 +704,34 @@ public static function unparseProvider(): iterable
[],
];

yield 'arguments & options; requesting a specific option' => [
$completeInputDefinition,
new ArgvInput([
'--optWithoutDefaultValue=optValue1',
'--optWithDefaultValue=optValue2',
]),
['optWithDefaultValue'],
['--optWithDefaultValue=optValue2'],
];

yield 'arguments & options; requesting no options' => [
$completeInputDefinition,
new ArgvInput([
'--optWithoutDefaultValue=optValue1',
'--optWithDefaultValue=optValue2',
]),
[],
[],
];

$createSingleOptionScenario = static fn (
InputOption $option,
array $input,
array $expected,
) => [
new InputDefinition([$option]),
new ArgvInput(['appName', ...$input]),
[],
null,
$expected,
];

Expand Down Expand Up @@ -817,7 +837,7 @@ public static function unparseProvider(): iterable
),
]),
new ArgvInput(['appName', '--opt='.$optionValue]),
[],
null,
['--opt='.$expected],
];

Expand Down
36 changes: 28 additions & 8 deletions 36 src/Symfony/Component/Console/Tests/Input/ArrayInputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public function testUnparse(
$input->bind($inputDefinition);
}

$actual = null === $parsedOptions ? $input->unparse() : $input->unparse($parsedOptions);
$actual = $input->unparse($parsedOptions);

self::assertSame($expected, $actual);
}
Expand All @@ -194,7 +194,7 @@ public static function unparseProvider(): iterable
yield 'empty input and empty definition' => [
new InputDefinition(),
new ArrayInput([]),
[],
null,
[],
];

Expand All @@ -215,7 +215,7 @@ public static function unparseProvider(): iterable
),
]),
new ArrayInput([]),
[],
null,
[],
];

Expand Down Expand Up @@ -252,7 +252,7 @@ public static function unparseProvider(): iterable
'requiredArgWithoutDefaultValue' => 'argValue',
'--optWithoutDefaultValue' => 'optValue',
]),
[],
null,
['--optWithoutDefaultValue=optValue'],
];

Expand All @@ -264,7 +264,7 @@ public static function unparseProvider(): iterable
'--optWithoutDefaultValue' => 'optValue',
'--optWithDefaultValue' => 'optDefaultValue',
]),
[],
null,
[
'--optWithoutDefaultValue=optValue',
'--optWithDefaultValue=optDefaultValue',
Expand All @@ -279,7 +279,7 @@ public static function unparseProvider(): iterable
'--optWithoutDefaultValue' => 'optValue',
'--optWithDefaultValue' => 'optDefaultValue',
]),
[],
null,
[],
];

Expand All @@ -297,14 +297,34 @@ public static function unparseProvider(): iterable
[],
];

yield 'arguments & options; requesting a specific option' => [
$completeInputDefinition,
new ArrayInput([
'--optWithoutDefaultValue' => 'optValue1',
'--optWithDefaultValue' => 'optValue2',
]),
['optWithDefaultValue'],
['--optWithDefaultValue=optValue2'],
];

yield 'arguments & options; requesting no options' => [
$completeInputDefinition,
new ArrayInput([
'--optWithoutDefaultValue' => 'optValue1',
'--optWithDefaultValue' => 'optValue2',
]),
[],
[],
];

$createSingleOptionScenario = static fn (
InputOption $option,
array $input,
array $expected,
) => [
new InputDefinition([$option]),
new ArrayInput($input),
[],
null,
$expected,
];

Expand Down Expand Up @@ -412,7 +432,7 @@ public static function unparseProvider(): iterable
new ArrayInput([
'--opt' => $optionValue,
]),
[],
null,
[
'--opt='.$expected,
],
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.