8000 [Console] added an exception when an option name or shortcut is inval… · symfony/symfony@517ae43 · GitHub
[go: up one dir, main page]

Skip to content

Commit 517ae43

Browse files
committed
[Console] added an exception when an option name or shortcut is invalid (refs #4346)
1 parent c1e868f commit 517ae43

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ public function __construct($name, $shortcut = null, $mode = null, $description
5050
$name = substr($name, 2);
5151
}
5252

53+
if (empty($name)) {
54+
throw new \InvalidArgumentException('An option name cannot be empty.');
55+
}
56+
5357
if (empty($shortcut)) {
5458
$shortcut = null;
5559
}
@@ -60,7 +64,7 @@ public function __construct($name, $shortcut = null, $mode = null, $description
6064
}
6165

6266
if (empty($shortcut)) {
63-
$shortcut = null;
67+
throw new \InvalidArgumentException('An option shortcut cannot be empty.');
6468
}
6569
}
6670

src/Symfony/Component/Console/Tests/Input/InputOptionTest.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ public function testConstructor()
3737
$this->assertEquals('f', $option->getShortcut(), '__construct() removes the leading - of the shortcut');
3838
$option = new InputOption('foo');
3939
$this->assertNull($option->getShortcut(), '__construct() makes the shortcut null by default');
40-
$option = new InputOption('foo', '-');
41-
$this->assertNull($option->getShortcut(), '__construct() makes the shortcut null if a single dash is specified as its name');
4240

4341
// mode argument
4442
$option = new InputOption('foo', 'f');
@@ -82,6 +80,30 @@ public function testConstructor()
8280
}
8381
}
8482

83+
/**
84+
* @expectedException \InvalidArgumentException
85+
*/
86+
public function testEmptyNameIsInvalid()
87+
{
88+
new InputOption('');
89+
}
90+
91+
/**
92+
* @expectedException \InvalidArgumentException
93+
*/
94+
public function testDoubleDashNameIsInvalid()
95+
{
96+
new InputOption('--');
97+
}
98+
99+
/**
100+
* @expectedException \InvalidArgumentException
101+
*/
102+
public function testSingleDashOptionIsInvalid()
103+
{
104+
new InputOption('foo', '-');
105+
}
106+
85107
public function testIsArray()
86108
{
87109
$option = new InputOption('foo', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY);

0 commit comments

Comments
 (0)
0