Skip to content

Commit

Permalink
[Console] InputArgument and InputOption code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jnoordsij authored and fabpot committed Feb 3, 2024
1 parent 87f1a34 commit fdf207c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 16 deletions.
12 changes: 6 additions & 6 deletions src/Symfony/Component/Console/Command/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ public function run(InputInterface $input, OutputInterface $output): int
}

/**
* Adds suggestions to $suggestions for the current completion input (e.g. option or argument).
* Supplies suggestions when resolving possible completion options for input (e.g. option or argument).
*/
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
{
Expand Down Expand Up @@ -401,8 +401,8 @@ public function getNativeDefinition(): InputDefinition
/**
* Adds an argument.
*
* @param $mode The argument mode: InputArgument::REQUIRED or InputArgument::OPTIONAL
* @param $default The default value (for InputArgument::OPTIONAL mode only)
* @param $mode The argument mode: InputArgument::REQUIRED or InputArgument::OPTIONAL
* @param $default The default value (for InputArgument::OPTIONAL mode only)
* @param array|\Closure(CompletionInput,CompletionSuggestions):list<string|Suggestion> $suggestedValues The values used for input completion
*
* @return $this
Expand All @@ -420,9 +420,9 @@ public function addArgument(string $name, ?int $mode = null, string $description
/**
* Adds an option.
*
* @param $shortcut The shortcuts, can be null, a string of shortcuts delimited by | or an array of shortcuts
* @param $mode The option mode: One of the InputOption::VALUE_* constants
* @param $default The default value (must be null for InputOption::VALUE_NONE)
* @param $shortcut The shortcuts, can be null, a string of shortcuts delimited by | or an array of shortcuts
* @param $mode The option mode: One of the InputOption::VALUE_* constants
* @param $default The default value (must be null for InputOption::VALUE_NONE)
* @param array|\Closure(CompletionInput,CompletionSuggestions):list<string|Suggestion> $suggestedValues The values used for input completion
*
* @return $this
Expand Down
23 changes: 17 additions & 6 deletions src/Symfony/Component/Console/Input/InputArgument.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,26 @@
*/
class InputArgument
{
/**
* Providing an argument is required (e.g. just 'app:foo' is not allowed).
*/
public const REQUIRED = 1;

/**
* Providing an argument is optional (e.g. 'app:foo' and 'app:foo bar' are both allowed). This is the default behavior of arguments.
*/
public const OPTIONAL = 2;

/**
* The argument accepts multiple values and turn them into an array (e.g. 'app:foo bar baz' will result in value ['bar', 'baz']).
*/
public const IS_ARRAY = 4;

private int $mode;
private string|int|bool|array|float|null $default;

/**
* @param string $name The argument name
* @param int|null $mode The argument mode: a bit mask of self::REQUIRED, self::OPTIONAL and self::IS_ARRAY
* @param int-mask-of<InputArgument::*>|null $mode The argument mode: a bit mask of self::REQUIRED, self::OPTIONAL and self::IS_ARRAY
* @param string $description A description text
* @param string|bool|int|float|array|null $default The default value (for self::OPTIONAL mode only)
* @param array|\Closure(CompletionInput,CompletionSuggestions):list<string|Suggestion> $suggestedValues The values used for input completion
Expand All @@ -50,7 +60,7 @@ public function __construct(
) {
if (null === $mode) {
$mode = self::OPTIONAL;
} elseif ($mode > 7 || $mode < 1) {
} elseif ($mode >= (self::IS_ARRAY << 1) || $mode < 1) {
throw new InvalidArgumentException(sprintf('Argument mode "%s" is not valid.', $mode));
}

Expand Down Expand Up @@ -89,8 +99,6 @@ public function isArray(): bool

/**
* Sets the default value.
*
* @throws LogicException When incorrect default value is given
*/
public function setDefault(string|bool|int|float|array|null $default): void
{
Expand All @@ -117,13 +125,16 @@ public function getDefault(): string|bool|int|float|array|null
return $this->default;
}

/**
* Returns true if the argument has values for input completion.
*/
public function hasCompletion(): bool
{
return [] !== $this->suggestedValues;
}

/**
* Adds suggestions to $suggestions for the current completion input.
* Supplies suggestions when command resolves possible completion options for input.
*
* @see Command::complete()
*/
Expand Down
19 changes: 15 additions & 4 deletions src/Symfony/Component/Console/Input/InputOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,18 @@ class InputOption
public const VALUE_IS_ARRAY = 8;

/**
* The option may have either positive or negative value (e.g. --ansi or --no-ansi).
* The option allows passing a negated variant (e.g. --ansi or --no-ansi).
*/
public const VALUE_NEGATABLE = 16;

private string $name;
private string|array|null $shortcut;
private ?string $shortcut;
private int $mode;
private string|int|bool|array|float|null $default;

/**
* @param string|array|null $shortcut The shortcuts, can be null, a string of shortcuts delimited by | or an array of shortcuts
* @param int|null $mode The option mode: One of the VALUE_* constants
* @param int-mask-of<InputOption::*>|null $mode The option mode: One of the VALUE_* constants
* @param string|bool|int|float|array|null $default The default value (must be null for self::VALUE_NONE)
* @param array|\Closure(CompletionInput,CompletionSuggestions):list<string|Suggestion> $suggestedValues The values used for input completion
*
Expand Down Expand Up @@ -175,11 +175,19 @@ public function isArray(): bool
return self::VALUE_IS_ARRAY === (self::VALUE_IS_ARRAY & $this->mode);
}

/**
* Returns true if the option allows passing a negated variant.
*
* @return bool true if mode is self::VALUE_NEGATABLE, false otherwise
*/
public function isNegatable(): bool
{
return self::VALUE_NEGATABLE === (self::VALUE_NEGATABLE & $this->mode);
}

/**
* Sets the default value.
*/
public function setDefault(string|bool|int|float|array|null $default): void
{
if (self::VALUE_NONE === (self::VALUE_NONE & $this->mode) && null !== $default) {
Expand Down Expand Up @@ -213,13 +221,16 @@ public function getDescription(): string
return $this->description;
}

/**
* Returns true if the option has values for input completion.
*/
public function hasCompletion(): bool
{
return [] !== $this->suggestedValues;
}

/**
* Adds suggestions to $suggestions for the current completion input.
* Supplies suggestions when command resolves possible completion options for input.
*
* @see Command::complete()
*/
Expand Down

0 comments on commit fdf207c

Please sign in to comment.