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

Commit5d70c9b

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

File tree

12 files changed

+170
-62
lines changed

12 files changed

+170
-62
lines changed

‎UPGRADE-6.1.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Console
1111
-------
1212

1313
* Deprecate`Command::$defaultName` and`Command::$defaultDescription`, use the`AsCommand` attribute instead
14+
* Add argument`$suggestedValues` to`Command::addArgument` and`Command::addOption`
15+
* Add argument`$suggestedValues` to`InputArgument` and`InputOption` constructors
1416

1517
Serializer
1618
----------

‎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 suggested values for arguments and options in input definition, for input completion
910

1011
6.0
1112
---

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

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,12 @@ public function run(InputInterface $input, OutputInterface $output): int
319319
*/
320320
publicfunctioncomplete(CompletionInput$input,CompletionSuggestions$suggestions):void
321321
{
322+
$definition =$this->getDefinition();
323+
if (CompletionInput::TYPE_OPTION_VALUE ===$input->getCompletionType() &&$definition->hasOption($input->getCompletionName())) {
324+
$suggestions->suggestValues($definition->getOption($input->getCompletionName())->getSuggestedValues($input));
325+
}elseif (CompletionInput::TYPE_ARGUMENT_VALUE ===$input->getCompletionType() &&$definition->hasArgument($input->getCompletionName())) {
326+
$suggestions->suggestValues($definition->getArgument($input->getCompletionName())->getSuggestedValues($input));
327+
}
322328
}
323329

324330
/**
@@ -427,17 +433,22 @@ public function getNativeDefinition(): InputDefinition
427433
/**
428434
* Adds an argument.
429435
*
430-
* @param int|null $mode The argument mode: InputArgument::REQUIRED or InputArgument::OPTIONAL
431-
* @param mixed $default The default value (for InputArgument::OPTIONAL mode only)
436+
* @param $mode The argument mode: InputArgument::REQUIRED or InputArgument::OPTIONAL
437+
* @param $default The default value (for InputArgument::OPTIONAL mode only)
438+
* @param array|\Closure(CompletionInput):array $suggestedValues The values used for input completion
432439
*
433440
* @throws InvalidArgumentException When argument mode is not valid
434441
*
435442
* @return $this
436443
*/
437-
publicfunctionaddArgument(string$name,int$mode =null,string$description ='',mixed$default =null):static
444+
publicfunctionaddArgument(string$name,int$mode =null,string$description ='',mixed$default =null,/*array|\Closure $suggestedValues = null*/):static
438445
{
439-
$this->definition->addArgument(newInputArgument($name,$mode,$description,$default));
440-
$this->fullDefinition?->addArgument(newInputArgument($name,$mode,$description,$default));
446+
$suggestedValues =5 <=\func_num_args() ?func_get_arg(4) : [];
447+
if (!\is_array($suggestedValues) && !$suggestedValuesinstanceof \Closure) {
448+
thrownew \TypeError(sprintf('Argument 5 passed to "%s()" must be array or \Closure, "%s" given.',__METHOD__,get_debug_type($suggestedValues)));
449+
}
450+
$this->definition->addArgument(newInputArgument($name,$mode,$description,$default,$suggestedValues));
451+
$this->fullDefinition?->addArgument(newInputArgument($name,$mode,$description,$default,$suggestedValues));
441452

442453
return$this;
443454
}
@@ -448,15 +459,20 @@ public function addArgument(string $name, int $mode = null, string $description
448459
* @param $shortcut The shortcuts, can be null, a string of shortcuts delimited by | or an array of shortcuts
449460
* @param $mode The option mode: One of the InputOption::VALUE_* constants
450461
* @param $default The default value (must be null for InputOption::VALUE_NONE)
462+
* @param array|\Closure(CompletionInput):array $suggestedValues The values used for input completion
451463
*
452464
* @throws InvalidArgumentException If option mode is invalid or incompatible
453465
*
454466
* @return $this
455467
*/
456-
publicfunctionaddOption(string$name,string|array$shortcut =null,int$mode =null,string$description ='',mixed$default =null):static
468+
publicfunctionaddOption(string$name,string|array$shortcut =null,int$mode =null,string$description ='',mixed$default =null,/*array|\Closure $suggestedValues = []*/):static
457469
{
458-
$this->definition->addOption(newInputOption($name,$shortcut,$mode,$description,$default));
459-
$this->fullDefinition?->addOption(newInputOption($name,$shortcut,$mode,$description,$default));
470+
$suggestedValues =6 <=\func_num_args() ?func_get_arg(5) : [];
471+
if (!\is_array($suggestedValues) && !$suggestedValuesinstanceof \Closure) {
472+
thrownew \TypeError(sprintf('Argument 5 passed to "%s()" must be array or \Closure, "%s" given.',__METHOD__,get_debug_type($suggestedValues)));
473+
}
474+
$this->definition->addOption(newInputOption($name,$shortcut,$mode,$description,$default,$suggestedValues));
475+
$this->fullDefinition?->addOption(newInputOption($name,$shortcut,$mode,$description,$default,$suggestedValues));
460476

461477
return$this;
462478
}

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

Lines changed: 3 additions & 10 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
{
@@ -82,7 +75,7 @@ protected function configure()
8275
<info>eval "$(${fullCommand} completion bash)"</>
8376
EOH
8477
)
85-
->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')
78+
->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',null,fn () =>$this->getSupportedShells())
8679
->addOption('debug',null, InputOption::VALUE_NONE,'Tail the completion debug log')
8780
;
8881
}
@@ -135,7 +128,7 @@ private function tailDebugLog(string $commandName, OutputInterface $output): voi
135128
*/
136129
privatefunctiongetSupportedShells():array
137130
{
138-
returnarray_map(function ($f) {
131+
return$this->supportedShells ??=array_map(function ($f) {
139132
returnpathinfo($f, \PATHINFO_EXTENSION);
140133
},glob(__DIR__.'/../Resources/completion.*'));
141134
}

‎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/LazyCommand.php‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,16 +108,28 @@ public function getNativeDefinition(): InputDefinition
108108
return$this->getCommand()->getNativeDefinition();
109109
}
110110

111-
publicfunctionaddArgument(string$name,int$mode =null,string$description ='',mixed$default =null):static
111+
/**
112+
* {@inheritdoc}
113+
*
114+
* @param array|\Closure(CompletionInput):array $suggestedValues The values used for input completion
115+
*/
116+
publicfunctionaddArgument(string$name,int$mode =null,string$description ='',mixed$default =null,/*array|\Closure $suggestedValues = []*/):static
112117
{
113-
$this->getCommand()->addArgument($name,$mode,$description,$default);
118+
$suggestedValues =5 <=\func_num_args() ?func_get_arg(4) : [];
119+
$this->getCommand()->addArgument($name,$mode,$description,$default,$suggestedValues);
114120

115121
return$this;
116122
}
117123

118-
publicfunctionaddOption(string$name,string|array$shortcut =null,int$mode =null,string$description ='',mixed$default =null):static
124+
/**
125+
* {@inheritdoc}
126+
*
127+
* @param array|\Closure(CompletionInput):array $suggestedValues The values used for input completion
128+
*/
129+
publicfunctionaddOption(string$name,string|array$shortcut =null,int$mode =null,string$description ='',mixed$default =null,/*array|\Closure $suggestedValues = []*/):static
119130
{
120-
$this->getCommand()->addOption($name,$shortcut,$mode,$description,$default);
131+
$suggestedValues =6 <=\func_num_args() ?func_get_arg(5) : [];
132+
$this->getCommand()->addOption($name,$shortcut,$mode,$description,$default,$suggestedValues);
121133

122134
return$this;
123135
}

‎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: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespaceSymfony\Component\Console\Input;
1313

14+
useSymfony\Component\Console\Completion\CompletionInput;
1415
useSymfony\Component\Console\Exception\InvalidArgumentException;
1516
useSymfony\Component\Console\Exception\LogicException;
1617

@@ -28,17 +29,19 @@ class InputArgument
2829
privatestring$name;
2930
privateint$mode;
3031
privatestring|int|bool|array|null|float$default;
32+
privatearray|\Closure$suggestedValues;
3133
privatestring$description;
3234

3335
/**
3436
* @param string $name The argument name
3537
* @param int|null $mode The argument mode: self::REQUIRED or self::OPTIONAL
3638
* @param string $description A description text
3739
* @param string|bool|int|float|array|null $default The default value (for self::OPTIONAL mode only)
40+
* @param array|\Closure(CompletionInput):array $suggestedValues The values used for input completion
3841
*
3942
* @throws InvalidArgumentException When argument mode is not valid
4043
*/
41-
publicfunction__construct(string$name,int$mode =null,string$description ='',string|bool|int|float|array$default =null)
44+
publicfunction__construct(string$name,int$mode =null,string$description ='',string|bool|int|float|array$default =null,\Closure|array$suggestedValues = [])
4245
{
4346
if (null ===$mode) {
4447
$mode =self::OPTIONAL;
@@ -49,6 +52,7 @@ public function __construct(string $name, int $mode = null, string $description
4952
$this->name =$name;
5053
$this->mode =$mode;
5154
$this->description =$description;
55+
$this->suggestedValues =$suggestedValues;
5256

5357
$this->setDefault($default);
5458
}
@@ -111,6 +115,19 @@ public function getDefault(): string|bool|int|float|array|null
111115
return$this->default;
112116
}
113117

118+
/**
119+
* Returns suggested values for input completion.
120+
*/
121+
publicfunctiongetSuggestedValues(CompletionInput$input):array
122+
{
123+
$values =$this->suggestedValues;
124+
if ($valuesinstanceof \Closure && !\is_array($values =$values($input))) {
125+
thrownewLogicException(sprintf('Closure for argument "%s" must return an array. Got "%s".',$this->name,get_debug_type($values)));
126+
}
127+
128+
return$values;
129+
}
130+
114131
/**
115132
* Returns the description text.
116133
*/

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespaceSymfony\Component\Console\Input;
1313

14+
useSymfony\Component\Console\Completion\CompletionInput;
1415
useSymfony\Component\Console\Exception\InvalidArgumentException;
1516
useSymfony\Component\Console\Exception\LogicException;
1617

@@ -50,16 +51,18 @@ class InputOption
5051
privatestring|array|null$shortcut;
5152
privateint$mode;
5253
privatestring|int|bool|array|null|float$default;
54+
privatearray|\Closure$suggestedValues;
5355
privatestring$description;
5456

5557
/**
5658
* @param string|array|null $shortcut The shortcuts, can be null, a string of shortcuts delimited by | or an array of shortcuts
5759
* @param int|null $mode The option mode: One of the VALUE_* constants
5860
* @param string|bool|int|float|array|null $default The default value (must be null for self::VALUE_NONE)
61+
* @param array|\Closure(CompletionInput):array $suggestedValues The values used for input completion
5962
*
6063
* @throws InvalidArgumentException If option mode is invalid or incompatible
6164
*/
62-
publicfunction__construct(string$name,string|array$shortcut =null,int$mode =null,string$description ='',string|bool|int|float|array$default =null)
65+
publicfunction__construct(string$name,string|array$shortcut =null,int$mode =null,string$description ='',string|bool|int|float|array$default =null,array|\Closure$suggestedValues = [])
6366
{
6467
if (str_starts_with($name,'--')) {
6568
$name =substr($name,2);
@@ -96,7 +99,11 @@ public function __construct(string $name, string|array $shortcut = null, int $mo
9699
$this->shortcut =$shortcut;
97100
$this->mode =$mode;
98101
$this->description =$description;
102+
$this->suggestedValues =$suggestedValues;
99103

104+
if ($suggestedValues && !$this->acceptValue()) {
105+
thrownewLogicException('Cannot set suggested values if the option does not accept a value.');
106+
}
100107
if ($this->isArray() && !$this->acceptValue()) {
101108
thrownewInvalidArgumentException('Impossible to have an option mode VALUE_IS_ARRAY if the option does not accept a value.');
102109
}
@@ -193,6 +200,19 @@ public function getDefault(): string|bool|int|float|array|null
193200
return$this->default;
194201
}
195202

203+
/**
204+
* Returns suggested values for input completion.
205+
*/
206+
publicfunctiongetSuggestedValues(CompletionInput$input):array
207+
{
208+
$values =$this->suggestedValues;
209+
if ($valuesinstanceof \Closure && !\is_array($values =$values($input))) {
210+
thrownewLogicException(sprintf('Closure for option "%s" must return an array. Got "%s".',$this->name,get_debug_type($values)));
211+
}
212+
213+
return$values;
214+
}
215+
196216
/**
197217
* Returns the description text.
198218
*/

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp