8000 slightly adjust the API · symfony/symfony@9338ba8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9338ba8

Browse files
committed
slightly adjust the API
1 parent d36058e commit 9338ba8

File tree

4 files changed

+62
-20
lines changed

4 files changed

+62
-20
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,18 +194,19 @@ public function getStream()
194194

195195
/**
196196
* Returns a stringified representation of the options passed to the command.
197+
* The options must NOT be escaped as otherwise passing them to a `Process` would result in them being escaped twice.
197198
*
198-
* InputArguments must NOT be escaped as otherwise passing them to a `Process` would result in them being escaped twice.
199-
*
200-
* @param string[] $optionNames Names of the options returned. If empty, all options are returned and non-passed or non-existent are ignored.
199+
* @param string[] $optionNames Names of the options returned. If null, all options are returned. Requested options
200+
* that either do not exist or were not passed (even if the option has a default value)
201+
* will not be part of the method output.
201202
*
202203
* @return list<string>
203204
*/
204-
public function unparse(array $optionNames = []): array
205+
public function unparse(?array $optionNames = null): array
205206
{
206207
$rawOptions = $this->getRawOptions();
207208

208-
$filteredRawOptions = 0 === \count($optionNames)
209+
$filteredRawOptions = null === $optionNames
209210
? $rawOptions
210211
: array_intersect_key($rawOptions, array_fill_keys($optionNames, ''));
211212

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
*
2222
* @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.
2323
* @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.
24+
* @method unparse(): list<string> Returns a stringified representation of the options passed to the command.
2425
*/
2526
interface InputInterface
2627
{

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

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ public static function unparseProvider(): iterable
614614
yield 'empty input and empty definition' => [
615615
new InputDefinition(),
616616
new ArgvInput([]),
617-
[],
617+
null,
618618
[],
619619
];
620620

@@ -635,7 +635,7 @@ public static function unparseProvider(): iterable
635635
),
636636
]),
637637
new ArgvInput([]),
638-
[],
638+
null,
639639
[],
640640
];
641641

@@ -669,14 +669,14 @@ public static function unparseProvider(): iterable
669669
yield 'arguments & options: returns all passed options but ignore default values' => [
670670
$completeInputDefinition,
671671
new ArgvInput(['argValue', '--optWithoutDefaultValue=optValue']),
672-
[],
672+
null,
673673
['--optWithoutDefaultValue=optValue'],
674674
];
675675

676676
yield 'arguments & options; explicitly pass the default values: the default values are returned' => [
677677
$completeInputDefinition,
678678
new ArgvInput(['argValue', 'argDefaultValue', '--optWithoutDefaultValue=optValue', '--optWithDefaultValue=optDefaultValue']),
679-
[],
679+
null,
680680
[
681681
'--optWithoutDefaultValue=optValue',
682682
'--optWithDefaultValue=optDefaultValue',
@@ -686,7 +686,7 @@ public static function unparseProvider(): iterable
686686
yield 'arguments & options; no input definition: nothing returned' => [
687687
null,
688688
new ArgvInput(['argValue', 'argDefaultValue', '--optWithoutDefaultValue=optValue', '--optWithDefaultValue=optDefaultValue']),
689-
[],
689+
null,
690690
[],
691691
];
692692

@@ -704,14 +704,34 @@ public static function unparseProvider(): iterable
704704
[],
705705
];
706706

707+
yield 'arguments & options; requesting a specific option' => [
708+
$completeInputDefinition,
709+
new ArgvInput([
710+
'--optWithoutDefaultValue=optValue1',
711+
'--optWithDefaultValue=optValue2',
712+
]),
713+
['optWithDefaultValue'],
714+
['--optWithDefaultValue=optValue2'],
715+
];
716+
717+
yield 'arguments & options; requesting no options' => [
718+
$completeInputDefinition,
719+
new ArgvInput([
720+
'--optWithoutDefaultValue=optValue1',
721+
'--optWithDefaultValue=optValue2',
722+
]),
723+
[],
724+
[],
725+
];
726+
707727
$createSingleOptionScenario = static fn (
708728
InputOption $option,
709729
array $input,
710730
array $expected,
711731
) => [
712732
new InputDefinition([$option]),
713733
new ArgvInput(['appName', ...$input]),
714-
[],
734+
null,
715735
$expected,
716736
];
717737

@@ -817,7 +837,7 @@ public static function unparseProvider(): iterable
817837
),
818838
]),
819839
new ArgvInput(['appName', '--opt='.$optionValue]),
820-
[],
840+
null,
821841
['--opt='.$expected],
822842
];
823843

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

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public function testUnparse(
184184
$input->bind($inputDefinition);
185185
}
186186

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

189189
self::assertSame($expected, $actual);
190190
}
@@ -194,7 +194,7 @@ public static function unparseProvider(): iterable
194194
yield 'empty input and empty definition' => [
195195
new InputDefinition(),
196196
new ArrayInput([]),
197-
[],
197+
null,
198198
[],
199199
];
200200

@@ -215,7 +215,7 @@ public static function unparseProvider(): iterable
215215
),
216216
]),
217217
new ArrayInput([]),
218-
[],
218+
null,
219219
[],
220220
];
221221

@@ -252,7 +252,7 @@ public static function unparseProvider(): iterable
252252
'requiredArgWithoutDefaultValue' => 'argValue',
253253
'--optWithoutDefaultValue' => 'optValue',
254254
]),
255-
[],
255+
null,
256256
['--optWithoutDefaultValue=optValue'],
257257
];
258258

@@ -264,7 +264,7 @@ public static function unparseProvider(): iterable
264264
'--optWithoutDefaultValue' => 'optValue',
265265
'--optWithDefaultValue' => 'optDefaultValue',
266266
]),
267-
[],
267+
null,
268268
[
269269
'--optWithoutDefaultValue=optValue',
270270
'--optWithDefaultValue=optDefaultValue',
@@ -279,7 +279,7 @@ public static function unparseProvider(): iterable
279279
'--optWithoutDefaultValue' => 'optValue',
280280
'--optWithDefaultValue' => 'optDefaultValue',
281281
]),
282-
[],
282+
null,
283283
[],
284284
];
285285

@@ -297,14 +297,34 @@ public static function unparseProvider(): iterable
297297
[],
298298
];
299299

300+
yield 'arguments & options; requesting a specific option' => [
301+
$completeInputDefinition,
302+
new ArrayInput([
303+
'--optWithoutDefaultValue' => 'optValue1',
304+
'--optWithDefaultValue' => 'optValue2',
305+
]),
306+
['optWithDefaultValue'],
307+
['--optWithDefaultValue=optValue2'],
308+
];
309+
310+
yield 'arguments & options; requesting no options' => [
311+
$completeInputDefinition,
312+
new ArrayInput([
313+
'--optWithoutDefaultValue' => 'optValue1',
314+
'--optWithDefaultValue' => 'optValue2',
315+
]),
316+
[],
317+
[],
318+
];
319+
300320
$createSingleOptionScenario = static fn (
301321
InputOption $option,
302322
array $input,
303323
array $expected,
304324
) => [
305325
new InputDefinition([$option]),
306326
new ArrayInput($input),
307-
[],
327+
null,
308328
$expected,
309329
];
310330

@@ -412,7 +432,7 @@ public static function unparseProvider(): iterable
412432
new ArrayInput([
413433
'--opt' => $optionValue,
414434
]),
415-
[],
435+
null,
416436
[
417437
'--opt='.$expected,
418438
],

0 commit comments

Comments
 (0)
0