Skip to content

Commit

Permalink
[Console] Add support for multiple InputOption shortcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek committed Apr 25, 2013
1 parent f22b036 commit 9456f5c
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -9,6 +9,7 @@ CHANGELOG
* added support for events in `Application` * added support for events in `Application`
* added a way to normalize EOLs in `ApplicationTester::getDisplay()` and `CommandTester::getDisplay()` * added a way to normalize EOLs in `ApplicationTester::getDisplay()` and `CommandTester::getDisplay()`
* added a way to set the progress bar progress via the `setCurrent` method * added a way to set the progress bar progress via the `setCurrent` method
* added support for multiple InputOption shortcuts, written as `'-a|-b|-c'`


2.2.0 2.2.0
----- -----
Expand Down
8 changes: 7 additions & 1 deletion Descriptor/XmlDescriptor.php
Expand Up @@ -57,7 +57,13 @@ protected function describeInputOption(InputOption $option, array $options = arr


$dom->appendChild($objectXML = $dom->createElement('option')); $dom->appendChild($objectXML = $dom->createElement('option'));
$objectXML->setAttribute('name', '--'.$option->getName()); $objectXML->setAttribute('name', '--'.$option->getName());
$objectXML->setAttribute('shortcut', $option->getShortcut() ? '-'.$option->getShortcut() : ''); $pos = strpos($option->getShortcut(), '|');
if (false !== $pos) {
$objectXML->setAttribute('shortcut', '-'.substr($option->getShortcut(), 0, $pos));
$objectXML->setAttribute('shortcuts', '-'.implode('|-', explode('|', $option->getShortcut())));
} else {
$objectXML->setAttribute('shortcut', $option->getShortcut() ? '-'.$option->getShortcut() : '');
}
$objectXML->setAttribute('accept_value', $option->acceptValue() ? 1 : 0); $objectXML->setAttribute('accept_value', $option->acceptValue() ? 1 : 0);
$objectXML->setAttribute('is_value_required', $option->isValueRequired() ? 1 : 0); $objectXML->setAttribute('is_value_required', $option->isValueRequired() ? 1 : 0);
$objectXML->setAttribute('is_multiple', $option->isArray() ? 1 : 0); $objectXML->setAttribute('is_multiple', $option->isArray() ? 1 : 0);
Expand Down
14 changes: 11 additions & 3 deletions Input/InputDefinition.php
Expand Up @@ -266,13 +266,21 @@ public function addOption(InputOption $option)
{ {
if (isset($this->options[$option->getName()]) && !$option->equals($this->options[$option->getName()])) { if (isset($this->options[$option->getName()]) && !$option->equals($this->options[$option->getName()])) {
throw new \LogicException(sprintf('An option named "%s" already exists.', $option->getName())); throw new \LogicException(sprintf('An option named "%s" already exists.', $option->getName()));
} elseif (isset($this->shortcuts[$option->getShortcut()]) && !$option->equals($this->options[$this->shortcuts[$option->getShortcut()]])) { }
throw new \LogicException(sprintf('An option with shortcut "%s" already exists.', $option->getShortcut()));
if ($option->getShortcut()) {
foreach (explode('|', $option->getShortcut()) as $shortcut) {
if (isset($this->shortcuts[$shortcut]) && !$option->equals($this->options[$this->shortcuts[$shortcut]])) {
throw new \LogicException(sprintf('An option with shortcut "%s" already exists.', $shortcut));
}
}
} }


$this->options[$option->getName()] = $option; $this->options[$option->getName()] = $option;
if ($option->getShortcut()) { if ($option->getShortcut()) {
$this->shortcuts[$option->getShortcut()] = $option->getName(); foreach (explode('|', $option->getShortcut()) as $shortcut) {
$this->shortcuts[$shortcut] = $option->getName();
}
} }
} }


Expand Down
6 changes: 3 additions & 3 deletions Input/InputOption.php
Expand Up @@ -59,9 +59,9 @@ public function __construct($name, $shortcut = null, $mode = null, $description
} }


if (null !== $shortcut) { if (null !== $shortcut) {
if ('-' === $shortcut[0]) { $shortcuts = preg_split('{(\|)-?}', ltrim($shortcut, '-'));
$shortcut = substr($shortcut, 1); $shortcuts = array_filter($shortcuts);
} $shortcut = implode('|', $shortcuts);


if (empty($shortcut)) { if (empty($shortcut)) {
throw new \InvalidArgumentException('An option shortcut cannot be empty.'); throw new \InvalidArgumentException('An option shortcut cannot be empty.');
Expand Down
10 changes: 10 additions & 0 deletions Tests/Input/InputDefinitionTest.php
Expand Up @@ -308,6 +308,15 @@ public function testGetOptionForShortcut()
$this->assertEquals($this->foo, $definition->getOptionForShortcut('f'), '->getOptionForShortcut() returns a InputOption by its shortcut'); $this->assertEquals($this->foo, $definition->getOptionForShortcut('f'), '->getOptionForShortcut() returns a InputOption by its shortcut');
} }


public function testGetOptionForMultiShortcut()
{
$this->initializeOptions();

$definition = new InputDefinition(array($this->multi));
$this->assertEquals($this->multi, $definition->getOptionForShortcut('m'), '->getOptionForShortcut() returns a InputOption by its shortcut');
$this->assertEquals($this->multi, $definition->getOptionForShortcut('mmm'), '->getOptionForShortcut() returns a InputOption by its shortcut');
}

/** /**
* @expectedException \InvalidArgumentException * @expectedException \InvalidArgumentException
* @expectedExceptionMessage The "-l" option does not exist. * @expectedExceptionMessage The "-l" option does not exist.
Expand Down Expand Up @@ -406,5 +415,6 @@ protected function initializeOptions()
$this->bar = new InputOption('bar', 'b'); $this->bar = new InputOption('bar', 'b');
$this->foo1 = new InputOption('fooBis', 'f'); $this->foo1 = new InputOption('fooBis', 'f');
$this->foo2 = new InputOption('foo', 'p'); $this->foo2 = new InputOption('foo', 'p');
$this->multi = new InputOption('multi', 'm|mm|mmm');
} }
} }
6 changes: 4 additions & 2 deletions Tests/Input/InputOptionTest.php
Expand Up @@ -36,8 +36,10 @@ public function testShortcut()
{ {
$option = new InputOption('foo', 'f'); $option = new InputOption('foo', 'f');
$this->assertEquals('f', $option->getShortcut(), '__construct() can take a shortcut as its second argument'); $this->assertEquals('f', $option->getShortcut(), '__construct() can take a shortcut as its second argument');
$option = new InputOption('foo', '-f'); $option = new InputOption('foo', '-f|-ff|fff');
$this->assertEquals('f', $option->getShortcut(), '__construct() removes the leading - of the shortcut'); $this->assertEquals('f|ff|fff', $option->getShortcut(), '__construct() removes the leading - of the shortcuts');
$option = new InputOption('foo', 'f|ff|-fff');
$this->assertEquals('f|ff|fff', $option->getShortcut(), '__construct() removes the leading - of the shortcuts');
$option = new InputOption('foo'); $option = new InputOption('foo');
$this->assertNull($option->getShortcut(), '__construct() makes the shortcut null by default'); $this->assertNull($option->getShortcut(), '__construct() makes the shortcut null by default');
} }
Expand Down

0 comments on commit 9456f5c

Please sign in to comment.