8000 Rename to suggestedValues · symfony/symfony@e668ed4 · GitHub
[go: up one dir, main page]

Skip to content

Commit e668ed4

Browse files
committed
Rename to suggestedValues
1 parent ab40533 commit e668ed4

File tree

5 files changed

+52
-91
lines changed

5 files changed

+52
-91
lines changed

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

Lines changed: 10 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -320,25 +320,11 @@ public function run(InputInterface $input, OutputInterface $output): int
320320
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
321321
{
322322
$definition = $this->getDefinition();
323-
$values = null;
324323
if (CompletionInput::TYPE_OPTION_VALUE === $input->getCompletionType() && $definition->hasOption($input->getCompletionName())) {
325-
$values = $definition->getOption($input->getCompletionName())->getCompletionValues();
324+
$suggestions->suggestValues($definition->getOption($input->getCompletionName())->getSuggestedValues($input));
326325
} elseif (CompletionInput::TYPE_ARGUMENT_VALUE === $input->getCompletionType() && $definition->hasArgument($input->getCompletionName())) {
327-
$values = $definition->getArgument($input->getCompletionName())->getCompletionValues();
326+
$suggestions->suggestValues($definition->getArgument($input->getCompletionName())->getSuggestedValues($input));
328327
}
329-
if (null === $values) {
330-
return;
331-
}
332-
if ($values instanceof \Closure) {
333-
if (null === $values = $values($input)) {
334-
return;
335-
}
336-
if (!\is_array($values)) {
337-
throw new LogicException(sprintf('Closure for "%s" "%s" must return an array or null. Got "%s".', $input->getCompletionType(), $input->getCompletionName(), get_debug_type($values)));
338-
}
339-
}
340-
341-
$suggestions->suggestValues($values);
342328
}
343329

344330
/**
@@ -449,30 +435,16 @@ public function getNativeDefinition(): InputDefinition
449435
*
450436
* @param int|null $mode The argument mode: InputArgument::REQUIRED or InputArgument::OPTIONAL
451437
* @param mixed $default The default value (for InputArgument::OPTIONAL mode only)
438+
* @param array|Closure(CompletionInput):array $suggestedValues The values used for input completion
452439
*
453440
* @throws InvalidArgumentException When argument mode is not valid
454441
*
455442
* @return $this
456443
*/
457-
public function addArgument(string $name, int $mode = null, string $description = '', mixed $default = null): static
458-
{
459-
$this->definition->addArgument(new InputArgument($name, $mode, $description, $default));
460-
$this->fullDefinition?->addArgument(new InputArgument($name, $mode, $description, $default));
461-
462-
return $this;
463-
}
464-
465-
/**
466-
* Set values for input completion.
467-
*
468-
* @throws InvalidArgumentException If the argument is not defined
469-
*
470-
* @return $this
471-
*/
472-
public function setArgumentValues(string $name, \Closure|iterable|null $values): static
444+
public fun F438 ction addArgument(string $name, int $mode = null, string $description = '', mixed $default = null, array|\Closure $suggestedValues = null): static
473445
{
474-
$this->definition->getArgument($name)->setCompletionValues($values);
475-
$this->fullDefinition?->getArgument($name)->setCompletionValues($values);
446+
$this->definition->addArgument(new InputArgument($name, $mode, $description, $default, $suggestedValues));
447+
$this->fullDefinition?->addArgument(new InputArgument($name, $mode, $description, $default, $suggestedValues));
476448

477449
return $this;
478450
}
@@ -483,30 +455,16 @@ public function setArgumentValues(string $name, \Closure|iterable|null $values):
483455
* @param $shortcut The shortcuts, can be null, a string of shortcuts delimited by | or an array of shortcuts
484456
* @param $mode The option mode: One of the InputOption::VALUE_* constants
485457
* @param $default The default value (must be null for InputOption::VALUE_NONE)
458+
* @param array|Closure(CompletionInput):array $suggestedValues The values used for input completion
486459
*
487460
* @throws InvalidArgumentException If option mode is invalid or incompatible
488461
*
489462
* @return $this
490463
*/
491-
public function addOption(string $name, string|array $shortcut = null, int $mode = null, string $description = '', mixed $default = null): static
492-
{
493-
$this->definition->addOption(new InputOption($name, $shortcut, $mode, $description, $default));
494-
$this->fullDefinition?->addOption(new InputOption($name, $shortcut, $mode, $description, $default));
495-
496-
return $this;
497-
}
498-
499-
/**
500-
* Set values for input completion.
501-
*
502-
* @throws InvalidArgumentException If the option is not defined
503-
*
504-
* @return $this
505-
*/
506-
public function setOptionValues(string $name, \Closure|iterable|null $values): static
464+
public function addOption(string $name, string|array $shortcut = null, int $mode = null, string $description = '', mixed $default = null, array|\Closure $suggestedValues = null): static
507465
{
508-
$this->definition->getOption($name)->setCompletionValues($values);
509-
$this->fullDefinition?->getOption($name)->setCompletionValues($values);
466+
$this->definition->addOption(new InputOption($name, $shortcut, $mode, $description, $default, $suggestedValues));
467+
$this->fullDefinition?->addOption(new InputOption($name, $shortcut, $mode, $description, $default, $suggestedValues));
510468

511469
return $this;
512470
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ protected function configure()
7575
<info>eval "$(${fullCommand} completion bash)"</>
7676
EOH
7777
)
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')
79-
->setArgumentValues('shell', $this->getSupportedShells())
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, $this->getSupportedShells())
8079
->addOption('debug', null, InputOption::VALUE_NONE, 'Tail the completion debug log')
8180
;
8281
}

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

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

111-
public function addArgument(string $name, int $mode = null, string $description = '', mixed $default = null): static
111+
public function addArgument(string $name, int $mode = null, string $description = '', mixed $default = null, array|\Closure $suggestedValues = null): static
112112
{
113-
$this->getCommand()->addArgument($name, $mode, $description, $default);
113+
$this->getCommand()->addArgument($name, $mode, $description, $default, $suggestedValues);
114114

115115
return $this;
116116
}
117117

118-
public function addOption(string $name, string|array $shortcut = null, int $mode = null, string $description = '', mixed $default = null): static
118+
public function addOption(string $name, string|array $shortcut = null, int $mode = null, string $description = '', mixed $default = null, array|\Closure $suggestedValues = null): static
119119
{
120-
$this->getCommand()->addOption($name, $shortcut, $mode, $description, $default);
120+
$this->getCommand()->addOption($name, $shortcut, $mode, $description, $default, $suggestedValues);
121121

122122
return $this;
123123
}

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

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Console\Input;
1313

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

@@ -28,18 +29,19 @@ class InputArgument
2829
private string $name;
2930
private int $mode;
3031
private string|int|bool|array|null|float $default;
31-
private array|\Closure|null $completionValues;
32+
private array|\Closure|null $suggestedValues;
3233
private string $description;
3334

3435
/**
35-
* @param string $name The argument name
36-
* @param int|null $mode The argument mode: self::REQUIRED or self::OPTIONAL
37-
* @param string $description A description text
38-
* @param string|bool|int|float|array|null $default The default value (for self::OPTIONAL mode only)
36+
* @param string $name The argument name
37+
* @param int|null $mode The argument mode: self::REQUIRED or self::OPTIONAL
38+
* @param string $description A description text
39+
* @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
3941
*
4042
* @throws InvalidArgumentException When argument mode is not valid
4143
*/
42-
public function __construct(string $name, int $mode = null, string $description = '', string|bool|int|float|array $default = null, \Closure|array $completionValues = null)
44+
public function __construct(string $name, int $mode = null, string $description = '', string|bool|int|float|array $default = null, \Closure|array $suggestedValues = null)
4345
{
4446
if (null === $mode) {
4547
$mode = self::OPTIONAL;
@@ -50,9 +52,9 @@ public function __construct(string $name, int $mode = null, string $description
5052
$this->name = $name;
5153
$this->mode = $mode;
5254
$this->description = $description;
55+
$this->suggestedValues = $suggestedValues;
5356

5457
$this->setDefault($default);
55-
$this->setCompletionValues($completionValues);
5658
}
5759

5860
/**
@@ -113,17 +115,17 @@ public function getDefault(): string|bool|int|float|array|null
113115
return $this->default;
114116
}
115117

116-
public function setCompletionValues(array|\Closure $completionValues = null)
117-
{
118-
$this->completionValues = $completionValues;
119-
}
120-
121118
/**
122-
* Returns suggestions for input completion.
119+
* Returns suggested values for input completion.
123120
*/
124-
public function getCompletionValues(): array|\Closure|null
121+
public function getSuggestedValues(CompletionInput $input): array
125122
{
126-
return $this->completionValues;
123+
$values = $this->suggestedValues;
124+
if ($values instanceof \Closure && !\is_array($values = $values($input))) {
125+
throw new LogicException(sprintf('Closure for "%s" "%s" must return an array. Got "%s".', $input->getCompletionType(), $input->getCompletionName(), get_debug_type($values)));
126+
}
127+
128+
return $values ?? [];
127129
}
128130

129131
/**

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

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Console\Input;
1313

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

@@ -50,17 +51,18 @@ class InputOption
5051
private string|array|null $shortcut;
5152
private int $mode;
5253
private string|int|bool|array|null|float $default;
53-
private array|\Closure|null $completionValues;
54+
private array|\Closure|null $suggestedValues;
5455
private string $description;
5556

5657
/**
57-
* @param string|array|null $shortcut The shortcuts, can be null, a string of shortcuts delimited by | or an array of shortcuts
58-
* @param int|null $mode The option mode: One of the VALUE_* constants
59-
* @param string|bool|int|float|array|null $default The default value (must be null for self::VALUE_NONE)
58+
* @param string|array|null $shortcut The shortcuts, can be null, a string of shortcuts delimited by | or an array of shortcuts
59+
* @param int|null $mode The option mode: One of the VALUE_* constants
60+
* @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
6062
*
6163
* @throws InvalidArgumentException If option mode is invalid or incompatible
6264
*/
63-
public function __construct(string $name, string|array $shortcut = null, int $mode = null, string $description = '', string|bool|int|float|array $default = null, array|\Closure $completionValues = null)
65+
public function __construct(string $name, string|array $shortcut = null, int $mode = null, string $description = '', string|bool|int|float|array $default = null, array|\Closure $suggestedValues = null)
6466
{
6567
if (str_starts_with($name, '--')) {
6668
$name = substr($name, 2);
@@ -97,6 +99,11 @@ public function __construct(string $name, string|array $shortcut = null, int $mo
9799
$this->shortcut = $shortcut;
98100
$this->mode = $mode;
99101
$this->description = $description;
102+
$this->suggestedValues = $suggestedValues;
103+
104+
if (null !== $this->suggestedValues && !$this->acceptValue()) {
105+
throw new LogicException('Suggested values cannot be set if the option does not accept a value.');
106+
}
100107

101108
if ($this->isArray() && !$this->acceptValue()) {
102109
throw new InvalidArgumentException('Impossible to have an option mode VALUE_IS_ARRAY if the option does not accept a value.');
@@ -106,7 +113,6 @@ public function __construct(string $name, string|array $shortcut = null, int $mo
106113
}
107114

108115
$this->setDefault($default);
109-
$this->setCompletionValues($completionValues);
110116
}
111117

112118
/**
@@ -195,21 +201,17 @@ public function getDefault(): string|bool|int|float|array|null
195201
return $this->default;
196202
}
197203

198-
public function setCompletionValues(array|\Closure $completionValues = null): void
199-
{
200-
if (self::VALUE_NONE === (self::VALUE_NONE & $this->mode) && null !== $completionValues) {
201-
throw new LogicException('Cannot set a completion when using InputOption::VALUE_NONE mode.');
202-
}
203-
204-
$this->completionValues = $completionValues;
205-
}
206-
207204
/**
208-
* Returns suggestions for input completion.
205+
* Returns suggested values for input completion.
209206
*/
210-
public function getCompletionValues(): array|\Closure|null
207+
public function getSuggestedValues(CompletionInput $input): array
211208
{
212-
return $this->completionValues;
209+
$values = $this->suggestedValues;
210+
if ($values instanceof \Closure && \is_array($values = $values($input))) {
211+
throw new LogicException(sprintf('Closure for "%s" "%s" must return an array. Got "%s".', $input->getCompletionType(), $input->getCompletionName(), get_debug_type($values)));
212+
}
213+
214+
return $values ?? [];
213215
}
214216

215217
/**

0 commit comments

Comments
 (0)
0