Skip to content

Commit bd025a3

Browse files
committed
[Console] Add InvalidOptionException to replace generic \InvalidArgumentException
1 parent 1783a98 commit bd025a3

File tree

4 files changed

+32
-11
lines changed

4 files changed

+32
-11
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Console\Exception;
13+
14+
/**
15+
* Represents an incorrect option name typed in the console.
16+
*/
17+
class InvalidOptionException extends \InvalidArgumentException
18+
{
19+
}

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

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

1212
namespace Symfony\Component\Console\Input;
1313

14+
use Symfony\Component\Console\Exception\InvalidOptionException;
15+
1416
/**
1517
* ArrayInput represents an input provided as an array.
1618
*
@@ -153,12 +155,12 @@ protected function parse()
153155
* @param string $shortcut The short option key
154156
* @param mixed $value The value for the option
155157
*
156-
* @throws \InvalidArgumentException When option given doesn't exist
158+
* @throws InvalidOptionException When option given doesn't exist
157159
*/
158160
private function addShortOption($shortcut, $value)
159161
{
160162
if (!$this->definition->hasShortcut($shortcut)) {
161-
throw new \InvalidArgumentException(sprintf('The "-%s" option does not exist.', $shortcut));
163+
throw new InvalidOptionException(sprintf('The "-%s" option does not exist.', $shortcut));
162164
}
163165

164166
$this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value);
@@ -170,20 +172,20 @@ private function addShortOption($shortcut, $value)
170172
* @param string $name The long option key
171173
* @param mixed $value The value for the option
172174
*
173-
* @throws \InvalidArgumentException When option given doesn't exist
174-
* @throws \InvalidArgumentException When a required value is missing
175+
* @throws InvalidOptionException When option given doesn't exist
176+
* @throws InvalidOptionException When a required value is missing
175177
*/
176178
private function addLongOption($name, $value)
177179
{
178180
if (!$this->definition->hasOption($name)) {
179-
throw new \InvalidArgumentException(sprintf('The "--%s" option does not exist.', $name));
181+
throw new InvalidOptionException(sprintf('The "--%s" option does not exist.', $name));
180182
}
181183

182184
$option = $this->definition->getOption($name);
183185

184186
if (null === $value) {
185187
if ($option->isValueRequired()) {
186-
throw new \InvalidArgumentException(sprintf('The "--%s" option requires a value.', $name));
188+
throw new InvalidOptionException(sprintf('The "--%s" option requires a value.', $name));
187189
}
188190

189191
$value = $option->isValueOptional() ? $option->getDefault() : true;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ public function testExecuteMethodNeedsToBeOverridden()
241241
}
242242

243243
/**
244-
* @expectedException \InvalidArgumentException
244+
* @expectedException Symfony\Component\Console\Exception\InvalidOptionException
245245
* @expectedExceptionMessage The "--bar" option does not exist.
246246
*/
247247
public function testRunWithInvalidOption()

src/Symfony/Component/Console/Tests/Fixtures/application_renderexception2.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11

22

3-
4-
[InvalidArgumentException]
5-
The "--foo" option does not exist.
6-
3+
4+
[Symfony\Component\Console\Exception\InvalidOptionException]
5+
The "--foo" option does not exist.
6+
77

88

99
list [--xml] [--raw] [--format FORMAT] [--] [<namespace>]

0 commit comments

Comments
 (0)