Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commita098ff8

Browse files
committed
[Console] Add completion values to input definition
1 parent44db49f commita098ff8

File tree

7 files changed

+103
-49
lines changed

7 files changed

+103
-49
lines changed

‎src/Symfony/Component/Console/CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* Add method`__toString()` to`InputInterface`
88
* Deprecate`Command::$defaultName` and`Command::$defaultDescription`, use the`AsCommand` attribute instead
9+
* Add completion values for arguments and options in input definition
910

1011
6.0
1112
---

‎src/Symfony/Component/Console/Command/Command.php‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,27 @@ public function run(InputInterface $input, OutputInterface $output): int
319319
*/
320320
publicfunctioncomplete(CompletionInput$input,CompletionSuggestions$suggestions):void
321321
{
322+
$definition =$this->getDefinition();
323+
$values =null;
324+
if (CompletionInput::TYPE_OPTION_VALUE ===$input->getCompletionType() &&$definition->hasOption($input->getCompletionName())) {
325+
$values =$definition->getOption($input->getCompletionName())->getValues();
326+
}elseif (CompletionInput::TYPE_ARGUMENT_VALUE ===$input->getCompletionType() &&$definition->hasArgument($input->getCompletionName())) {
327+
$values =$definition->getArgument($input->getCompletionName())->getValues();
328+
}
329+
if (null ===$values) {
330+
return;
331+
}
332+
if ($valuesinstanceof \Closure) {
333+
$values =$values($input);
334+
if (null ===$values) {
335+
return;
336+
}
337+
if (!is_iterable($values)) {
338+
thrownewLogicException(sprintf('Callable for "%s" "%s" must return an iterable or null. Got "%s".',$input->getCompletionType(),$input->getCompletionName(),get_debug_type($values)));
339+
}
340+
}
341+
342+
$suggestions->suggestValues(\is_array($values) ?$values :iterator_to_array($values));
322343
}
323344

324345
/**
@@ -442,6 +463,21 @@ public function addArgument(string $name, int $mode = null, string $description
442463
return$this;
443464
}
444465

466+
/**
467+
* Set values for input completion.
468+
*
469+
* @throws InvalidArgumentException If the argument is not defined
470+
*
471+
* @return $this
472+
*/
473+
publicfunctionsetArgumentValues(string$name,\Closure|iterable|null$values):static
474+
{
475+
$this->definition->getArgument($name)->setValues($values);
476+
$this->fullDefinition?->getArgument($name)->setValues($values);
477+
478+
return$this;
479+
}
480+
445481
/**
446482
* Adds an option.
447483
*
@@ -461,6 +497,21 @@ public function addOption(string $name, string|array $shortcut = null, int $mode
461497
return$this;
462498
}
463499

500+
/**
501+
* Set values for input completion.
502+
*
503+
* @throws InvalidArgumentException If the option is not defined
504+
*
505+
* @return $this
506+
*/
507+
publicfunctionsetOptionValues(string$name,\Closure|iterable|null$values):static
508+
{
509+
$this->definition->getOption($name)->setValues($values);
510+
$this->fullDefinition?->getOption($name)->setValues($values);
511+
512+
return$this;
513+
}
514+
464515
/**
465516
* Sets the name of the command.
466517
*

‎src/Symfony/Component/Console/Command/DumpCompletionCommand.php‎

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
namespaceSymfony\Component\Console\Command;
1313

1414
useSymfony\Component\Console\Attribute\AsCommand;
15-
useSymfony\Component\Console\Completion\CompletionInput;
16-
useSymfony\Component\Console\Completion\CompletionSuggestions;
1715
useSymfony\Component\Console\Input\InputArgument;
1816
useSymfony\Component\Console\Input\InputInterface;
1917
useSymfony\Component\Console\Input\InputOption;
@@ -39,12 +37,7 @@ final class DumpCompletionCommand extends Command
3937
*/
4038
protectedstatic$defaultDescription ='Dump the shell completion script';
4139

42-
publicfunctioncomplete(CompletionInput$input,CompletionSuggestions$suggestions):void
43-
{
44-
if ($input->mustSuggestArgumentValuesFor('shell')) {
45-
$suggestions->suggestValues($this->getSupportedShells());
46-
}
47-
}
40+
privatearray$supportedShells;
4841

4942
protectedfunctionconfigure()
5043
{
@@ -83,6 +76,7 @@ protected function configure()
8376
EOH
8477
)
8578
->addArgument('shell', InputArgument::OPTIONAL,'The shell type (e.g. "bash"), the value of the "$SHELL" env var will be used if this is not given')
79+
->setArgumentValues('shell',$this->getSupportedShells())
8680
->addOption('debug',null, InputOption::VALUE_NONE,'Tail the completion debug log')
8781
;
8882
}
@@ -135,7 +129,7 @@ private function tailDebugLog(string $commandName, OutputInterface $output): voi
135129
*/
136130
privatefunctiongetSupportedShells():array
137131
{
138-
returnarray_map(function ($f) {
132+
return$this->supportedShells ??=array_map(function ($f) {
139133
returnpathinfo($f, \PATHINFO_EXTENSION);
140134
},glob(__DIR__.'/../Resources/completion.*'));
141135
}

‎src/Symfony/Component/Console/Command/HelpCommand.php‎

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111

1212
namespaceSymfony\Component\Console\Command;
1313

14-
useSymfony\Component\Console\Completion\CompletionInput;
15-
useSymfony\Component\Console\Completion\CompletionSuggestions;
1614
useSymfony\Component\Console\Descriptor\ApplicationDescription;
1715
useSymfony\Component\Console\Helper\DescriptorHelper;
1816
useSymfony\Component\Console\Input\InputArgument;
@@ -39,8 +37,12 @@ protected function configure()
3937
$this
4038
->setName('help')
4139
->setDefinition([
42-
newInputArgument('command_name', InputArgument::OPTIONAL,'The command name','help'),
43-
newInputOption('format',null, InputOption::VALUE_REQUIRED,'The output format (txt, xml, json, or md)','txt'),
40+
newInputArgument('command_name', InputArgument::OPTIONAL,'The command name','help',function () {
41+
returnarray_keys((newApplicationDescription($this->getApplication()))->getCommands());
42+
}),
43+
newInputOption('format',null, InputOption::VALUE_REQUIRED,'The output format (txt, xml, json, or md)','txt',function () {
44+
return (newDescriptorHelper())->getFormats();
45+
}),
4446
newInputOption('raw',null, InputOption::VALUE_NONE,'To output raw command help'),
4547
])
4648
->setDescription('Display help for a command')
@@ -81,19 +83,4 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8183

8284
return0;
8385
}
84-
85-
publicfunctioncomplete(CompletionInput$input,CompletionSuggestions$suggestions):void
86-
{
87-
if ($input->mustSuggestArgumentValuesFor('command_name')) {
88-
$descriptor =newApplicationDescription($this->getApplication());
89-
$suggestions->suggestValues(array_keys($descriptor->getCommands()));
90-
91-
return;
92-
}
93-
94-
if ($input->mustSuggestOptionValuesFor('format')) {
95-
$helper =newDescriptorHelper();
96-
$suggestions->suggestValues($helper->getFormats());
97-
}
98-
}
9986
}

‎src/Symfony/Component/Console/Command/ListCommand.php‎

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111

1212
namespaceSymfony\Component\Console\Command;
1313

14-
useSymfony\Component\Console\Completion\CompletionInput;
15-
useSymfony\Component\Console\Completion\CompletionSuggestions;
1614
useSymfony\Component\Console\Descriptor\ApplicationDescription;
1715
useSymfony\Component\Console\Helper\DescriptorHelper;
1816
useSymfony\Component\Console\Input\InputArgument;
@@ -35,9 +33,13 @@ protected function configure()
3533
$this
3634
->setName('list')
3735
->setDefinition([
38-
newInputArgument('namespace', InputArgument::OPTIONAL,'The namespace name'),
36+
newInputArgument('namespace', InputArgument::OPTIONAL,'The namespace name',null,function () {
37+
returnarray_keys((newApplicationDescription($this->getApplication()))->getNamespaces());
38+
}),
3939
newInputOption('raw',null, InputOption::VALUE_NONE,'To output raw command list'),
40-
newInputOption('format',null, InputOption::VALUE_REQUIRED,'The output format (txt, xml, json, or md)','txt'),
40+
newInputOption('format',null, InputOption::VALUE_REQUIRED,'The output format (txt, xml, json, or md)','txt',function () {
41+
return (newDescriptorHelper())->getFormats();
42+
}),
4143
newInputOption('short',null, InputOption::VALUE_NONE,'To skip describing commands\' arguments'),
4244
])
4345
->setDescription('List commands')
@@ -77,19 +79,4 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7779

7880
return0;
7981
}
80-
81-
publicfunctioncomplete(CompletionInput$input,CompletionSuggestions$suggestions):void
82-
{
83-
if ($input->mustSuggestArgumentValuesFor('namespace')) {
84-
$descriptor =newApplicationDescription($this->getApplication());
85-
$suggestions->suggestValues(array_keys($descriptor->getNamespaces()));
86-
87-
return;
88-
}
89-
90-
if ($input->mustSuggestOptionValuesFor('format')) {
91-
$helper =newDescriptorHelper();
92-
$suggestions->suggestValues($helper->getFormats());
93-
}
94-
}
9582
}

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class InputArgument
2828
privatestring$name;
2929
privateint$mode;
3030
privatestring|int|bool|array|null|float$default;
31+
private\Closure|iterable|null$values;
3132
privatestring$description;
3233

3334
/**
@@ -38,7 +39,7 @@ class InputArgument
3839
*
3940
* @throws InvalidArgumentException When argument mode is not valid
4041
*/
41-
publicfunction__construct(string$name,int$mode =null,string$description ='',string|bool|int|float|array$default =null)
42+
publicfunction__construct(string$name,int$mode =null,string$description ='',string|bool|int|float|array$default =null,\Closure|iterable|null$values =null)
4243
{
4344
if (null ===$mode) {
4445
$mode =self::OPTIONAL;
@@ -51,6 +52,7 @@ public function __construct(string $name, int $mode = null, string $description
5152
$this->description =$description;
5253

5354
$this->setDefault($default);
55+
$this->setValues($values);
5456
}
5557

5658
/**
@@ -111,6 +113,19 @@ public function getDefault(): string|bool|int|float|array|null
111113
return$this->default;
112114
}
113115

116+
publicfunctionsetValues(\Closure|iterable|null$values =null)
117+
{
118+
$this->values =$values;
119+
}
120+
121+
/**
122+
* Returns suggestion values for input completion.
123+
*/
124+
publicfunctiongetValues():\Closure|iterable|null
125+
{
126+
return$this->values;
127+
}
128+
114129
/**
115130
* Returns the description text.
116131
*/

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class InputOption
5050
privatestring|array|null$shortcut;
5151
privateint$mode;
5252
privatestring|int|bool|array|null|float$default;
53+
private\Closure|iterable|null$values;
5354
privatestring$description;
5455

5556
/**
@@ -59,7 +60,7 @@ class InputOption
5960
*
6061
* @throws InvalidArgumentException If option mode is invalid or incompatible
6162
*/
62-
publicfunction__construct(string$name,string|array$shortcut =null,int$mode =null,string$description ='',string|bool|int|float|array$default =null)
63+
publicfunction__construct(string$name,string|array$shortcut =null,int$mode =null,string$description ='',string|bool|int|float|array$default =null,\Closure|iterable|null$values =null)
6364
{
6465
if (str_starts_with($name,'--')) {
6566
$name =substr($name,2);
@@ -105,6 +106,7 @@ public function __construct(string $name, string|array $shortcut = null, int $mo
105106
}
106107

107108
$this->setDefault($default);
109+
$this->setValues($values);
108110
}
109111

110112
/**
@@ -193,6 +195,23 @@ public function getDefault(): string|bool|int|float|array|null
193195
return$this->default;
194196
}
195197

198+
publicfunctionsetValues(\Closure|iterable|null$values =null)
199+
{
200+
if (self::VALUE_NONE === (self::VALUE_NONE &$this->mode) &&null !==$values) {
201+
thrownewLogicException('Cannot set a completion when using InputOption::VALUE_NONE mode.');
202+
}
203+
204+
$this->values =$values;
205+
}
206+
207+
/**
208+
* Returns suggestions for input completion.
209+
*/
210+
publicfunctiongetValues():\Closure|iterable|null
211+
{
212+
return$this->values;
213+
}
214+
196215
/**
197216
* Returns the description text.
198217
*/

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp