8000 feat: #43086 add blacklisting of reserved shortcuts for InputOptions in commands by Chris53897 · Pull Request #43091 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

feat: #43086 add blacklisting of reserved shortcuts for InputOptions in commands #43091

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

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: #43086 add blacklisting of reserved shortcuts for InputOptions …
…options
  • Loading branch information
Chris53897 committed Sep 19, 2021
commit 4d4820102fb34e1d220df81e342fe6d4e8e71199
11 changes: 10 additions & 1 deletion src/Symfony/Component/Console/Input/InputOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ class InputOption
private $default;
private $description;

/* @see Symfony\Component\Console\Application->getDefaultInputDefinition() */
public const RESERVED_NAMES = ["help", "quiet", "version", "ansi", "no-ansi", "no-interaction", "env", "no-debug", "verbose"];
public const RESERVED_COMMANDS = ["about", "help", "list"];
public const RESERVED_SHORTCUTS = ["h", "q", "V", "n", "e", "v", "vv", "vvv"];

/**
* @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
Expand Down Expand Up @@ -79,6 +84,10 @@ public function __construct(string $name, $shortcut = null, int $mode = null, st
}
$shortcuts = preg_split('{(\|)-?}', ltrim($shortcut, '-'));
$shortcuts = array_filter($shortcuts);
$used_reserved_shortcuts = array_intersect($shortcuts, self::RESERVED_SHORTCUTS);
if($used_reserved_shortcuts && !in_array($name, array_merge(self::RESERVED_NAMES, self::RESERVED_COMMANDS))) {
throw new InvalidArgumentException(sprintf('An option shortcut cannot include a reserved shortcut (%s).', implode('|', $used_reserved_shortcuts)));
}
$shortcut = implode('|', $shortcuts);

if (empty($shortcut)) {
Expand Down Expand Up @@ -226,6 +235,6 @@ public function equals(self $option)
&& $option->isArray() === $this->isArray()
&& $option->isValueRequired() === $this->isValueRequired()
&& $option->isValueOptional() === $this->isValueOptional()
;
;
}
}
35 changes: 35 additions & 0 deletions src/Symfony/Component/Console/Tests/Input/InputOptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,41 @@ public function testShortcut()
$this->assertNull($option->getShortcut(), '__construct() makes the shortcut null by default');
}

public function testInvalidShortcut()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('An option shortcut cannot include a reserved shortcut (e).');
$option = new InputOption('foo', 'e');

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('An option shortcut cannot include a reserved shortcut (v).');
$option = new InputOption('foo', 'x|v|z');

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('An option shortcut cannot include a reserved shortcut (v).');
$option = new InputOption('foo', ['x'|'v'|'z']);

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('An option shortcut cannot include a reserved shortcut (V).');
$option = new InputOption('foo', 'V');

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('An option shortcut cannot include a reserved shortcut (vvv).');
$option = new InputOption('foo', 'vvv');

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('An option shortcut cannot include a reserved shortcut (help).');
$option = new InputOption('foo', 'help');

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('An option shortcut cannot include a reserved shortcut (ansi).');
$option = new InputOption('foo', 'ansi');

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('An option shortcut cannot include a reserved shortcut (no-ansi).');
$option = new InputOption('foo', 'no-ansi');
}

public function testModes()
{
$option = new InputOption('foo', 'f');
Expand Down
0