Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Console][RFC] Add named arguments (aka required options) #48244

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/Symfony/Component/Console/Input/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public function validate()
{
$definition = $this->definition;
$givenArguments = $this->arguments;
$givenOptions = $this->options;

$missingArguments = array_filter(array_keys($definition->getArguments()), function ($argument) use ($definition, $givenArguments) {
return !\array_key_exists($argument, $givenArguments) && $definition->getArgument($argument)->isRequired();
Expand All @@ -69,6 +70,14 @@ public function validate()
if (\count($missingArguments) > 0) {
throw new RuntimeException(sprintf('Not enough arguments (missing: "%s").', implode(', ', $missingArguments)));
}

$missingOptions = array_filter(array_keys($definition->getOptions()), function ($option) use ($definition, $givenOptions) {
return !\array_key_exists($option, $givenOptions) && $definition->getOption($option)->isRequired();
});

if (\count($missingOptions) > 0) {
throw new RuntimeException(sprintf('Missing options (missing: "%s").', implode(', ', $missingOptions)));
}
}

public function isInteractive(): bool
Expand Down
18 changes: 17 additions & 1 deletion src/Symfony/Component/Console/Input/InputOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ class InputOption
*/
public const VALUE_NEGATABLE = 16;

/**
* The option aka "named argument" must be passed.
*/
public const REQUIRED = 32;

private string $name;
private string|array|null $shortcut;
private int $mode;
Expand Down Expand Up @@ -94,7 +99,7 @@ public function __construct(string $name, string|array $shortcut = null, int $mo

if (null === $mode) {
$mode = self::VALUE_NONE;
} elseif ($mode >= (self::VALUE_NEGATABLE << 1) || $mode < 1) {
} elseif ($mode >= (self::REQUIRED << 1) || $mode < 1) {
throw new InvalidArgumentException(sprintf('Option mode "%s" is not valid.', $mode));
}

Expand Down Expand Up @@ -133,6 +138,16 @@ public function getName(): string
return $this->name;
}

/**
* Returns true if the option is required.
*
* @return bool true if parameter mode is self::REQUIRED, false otherwise
*/
public function isRequired(): bool
{
return self::REQUIRED === (self::REQUIRED & $this->mode);
}

/**
* Returns true if the option accepts a value.
*
Expand Down Expand Up @@ -247,6 +262,7 @@ public function equals(self $option): bool
&& $option->isArray() === $this->isArray()
&& $option->isValueRequired() === $this->isValueRequired()
&& $option->isValueOptional() === $this->isValueOptional()
&& $option->isRequired() === $this->isRequired()
;
}
}
14 changes: 14 additions & 0 deletions src/Symfony/Component/Console/Tests/Input/InputOptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,26 +67,37 @@ public function testModes()
$this->assertFalse($option->acceptValue(), '__construct() gives a "InputOption::VALUE_NONE" mode by default');
$this->assertFalse($option->isValueRequired(), '__construct() gives a "InputOption::VALUE_NONE" mode by default');
$this->assertFalse($option->isValueOptional(), '__construct() gives a "InputOption::VALUE_NONE" mode by default');
$this->assertFalse($option->isRequired(), '__construct() can take "InputOption::VALUE_NONE" as its mode');

$option = new InputOption('foo', 'f', null);
$this->assertFalse($option->acceptValue(), '__construct() can take "InputOption::VALUE_NONE" as its mode');
$this->assertFalse($option->isValueRequired(), '__construct() can take "InputOption::VALUE_NONE" as its mode');
$this->assertFalse($option->isValueOptional(), '__construct() can take "InputOption::VALUE_NONE" as its mode');
$this->assertFalse($option->isRequired(), '__construct() can take "InputOption::VALUE_NONE" as its mode');

$option = new InputOption('foo', 'f', InputOption::VALUE_NONE);
$this->assertFalse($option->acceptValue(), '__construct() can take "InputOption::VALUE_NONE" as its mode');
$this->assertFalse($option->isValueRequired(), '__construct() can take "InputOption::VALUE_NONE" as its mode');
$this->assertFalse($option->isValueOptional(), '__construct() can take "InputOption::VALUE_NONE" as its mode');
$this->assertFalse($option->isRequired(), '__construct() can take "InputOption::VALUE_NONE" as its mode');

$option = new InputOption('foo', 'f', InputOption::VALUE_REQUIRED);
$this->assertTrue($option->acceptValue(), '__construct() can take "InputOption::VALUE_REQUIRED" as its mode');
$this->assertTrue($option->isValueRequired(), '__construct() can take "InputOption::VALUE_REQUIRED" as its mode');
$this->assertFalse($option->isValueOptional(), '__construct() can take "InputOption::VALUE_REQUIRED" as its mode');
$this->assertFalse($option->isRequired(), '__construct() can take "InputOption::VALUE_REQUIRED" as its mode');

$option = new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL);
$this->assertTrue($option->acceptValue(), '__construct() can take "InputOption::VALUE_OPTIONAL" as its mode');
$this->assertFalse($option->isValueRequired(), '__construct() can take "InputOption::VALUE_OPTIONAL" as its mode');
$this->assertTrue($option->isValueOptional(), '__construct() can take "InputOption::VALUE_OPTIONAL" as its mode');
$this->assertFalse($option->isRequired(), '__construct() can take "InputOption::VALUE_OPTIONAL" as its mode');

$option = new InputOption('foo', 'f', InputOption::REQUIRED);
$this->assertFalse($option->acceptValue(), '__construct() can take "InputOption::REQUIRED" as its mode');
$this->assertFalse($option->isValueRequired(), '__construct() can take "InputOption::REQUIRED" as its mode');
$this->assertTrue($option->isValueOptional(), '__construct() can take "InputOption::REQUIRED" as its mode');
$this->assertTrue($option->isRequired(), '__construct() can take "InputOption::REQUIRED" as its mode');
}

public function testInvalidModes()
Expand Down Expand Up @@ -145,6 +156,9 @@ public function testGetDefault()

$option = new InputOption('foo', null, InputOption::VALUE_NONE);
$this->assertFalse($option->getDefault(), '->getDefault() returns false if the option does not take a value');

$option = new InputOption('foo', null, InputOption::REQUIRED);
$this->assertFalse($option->getDefault(), '->getDefault() returns false if the option is required');
}

public function testSetDefault()
Expand Down