diff --git a/src/Symfony/Component/Console/Command/Command.php b/src/Symfony/Component/Console/Command/Command.php index c8dcf3befef88..38656fb35ab43 100644 --- a/src/Symfony/Component/Console/Command/Command.php +++ b/src/Symfony/Component/Console/Command/Command.php @@ -592,7 +592,8 @@ public function getSynopsis($short = false) $key = $short ? 'short' : 'long'; if (!isset($this->synopsis[$key])) { - $this->synopsis[$key] = trim(sprintf('%s %s', $this->name, $this->definition->getSynopsis($short))); + $partialSynopsis = $this->definition->getPartialSynopsis(array('command'), array(), $short); + $this->synopsis[$key] = trim(sprintf('%s %s', $this->name, $partialSynopsis)); } return $this->synopsis[$key]; diff --git a/src/Symfony/Component/Console/Input/InputDefinition.php b/src/Symfony/Component/Console/Input/InputDefinition.php index 19f8db33dac76..23de09ea5c3cf 100644 --- a/src/Symfony/Component/Console/Input/InputDefinition.php +++ b/src/Symfony/Component/Console/Input/InputDefinition.php @@ -349,13 +349,15 @@ private function shortcutToName($shortcut) } /** - * Gets the synopsis. + * Gets the synopsis without displaying given arguments and options. * - * @param bool $short Whether to return the short version (with options folded) or not + * @param array $exceptArguments arguments that won't be displayed + * @param array $exceptOptions options that won't be displayed + * @param bool $short Whether to return the short version (with options folded) or not * * @return string The synopsis */ - public function getSynopsis($short = false) + public function getPartialSynopsis(array $exceptArguments, array $exceptOptions = array(), $short = false) { $elements = array(); @@ -363,12 +365,17 @@ public function getSynopsis($short = false) $elements[] = '[options]'; } elseif (!$short) { foreach ($this->getOptions() as $option) { + $name = $option->getName(); + if (\in_array($name, $exceptOptions)) { + continue; + } + $value = ''; if ($option->acceptValue()) { $value = sprintf( ' %s%s%s', $option->isValueOptional() ? '[' : '', - strtoupper($option->getName()), + strtoupper($name), $option->isValueOptional() ? ']' : '' ); } @@ -383,7 +390,12 @@ public function getSynopsis($short = false) } foreach ($this->getArguments() as $argument) { - $element = '<'.$argument->getName().'>'; + $name = $argument->getName(); + if (\in_array($name, $exceptArguments)) { + continue; + } + + $element = '<'.$name.'>'; if (!$argument->isRequired()) { $element = '['.$element.']'; } elseif ($argument->isArray()) { @@ -399,4 +411,16 @@ public function getSynopsis($short = false) return implode(' ', $elements); } + + /** + * Gets the synopsis. + * + * @param bool $short Whether to return the short version (with options folded) or not + * + * @return string The synopsis + */ + public function getSynopsis($short = false) + { + return $this->getPartialSynopsis(array(), array(), $short); + } } diff --git a/src/Symfony/Component/Console/Tests/Command/CommandTest.php b/src/Symfony/Component/Console/Tests/Command/CommandTest.php index 6bc3f75b932a6..33d52be01449d 100644 --- a/src/Symfony/Component/Console/Tests/Command/CommandTest.php +++ b/src/Symfony/Component/Console/Tests/Command/CommandTest.php @@ -192,6 +192,23 @@ public function testGetSynopsis() $this->assertEquals('namespace:name [--foo] [--] []', $command->getSynopsis(), '->getSynopsis() returns the synopsis'); } + public function testGetSypnosisWithApplication() + { + $appDef = $this->getMockBuilder('Symfony\Component\Console\Input\InputDefinition')->getMock(); + $appDef->method('getArguments')->willReturn(array(new InputArgument('command'), new InputArgument('appbar'))); + $appDef->method('getOptions')->willReturn(array(new InputOption('appfoo'))); + $helperSet = $this->getMockBuilder('Symfony\Component\Console\Helper\HelperSet')->getMock(); + $application = $this->getMockBuilder('Symfony\Component\Console\Application')->getMock(); + $application->method('getDefinition')->willReturn($appDef); + $application->method('getHelperSet')->willReturn($helperSet); + $command = new \TestCommand(); + $command->setApplication($application); + $command->mergeApplicationDefinition(); + $command->addOption('foo'); + $command->addArgument('bar'); + $this->assertEquals('namespace:name [--appfoo] [--foo] [--] [] []', $command->getSynopsis()); + } + public function testAddGetUsages() { $command = new \TestCommand(); diff --git a/src/Symfony/Component/Console/Tests/Input/InputDefinitionTest.php b/src/Symfony/Component/Console/Tests/Input/InputDefinitionTest.php index d9ff5435b2a71..b3aa296affb39 100644 --- a/src/Symfony/Component/Console/Tests/Input/InputDefinitionTest.php +++ b/src/Symfony/Component/Console/Tests/Input/InputDefinitionTest.php @@ -387,6 +387,27 @@ public function testGetShortSynopsis() $this->assertEquals('[options] [--] []', $definition->getSynopsis(true), '->getSynopsis(true) groups options in [options]'); } + public function getGetPartialSynopsisData() + { + return array( + array(array(), array(), '[--foobar] [--] [] []'), + array(array('foo'), array(), '[--foobar] [--] []'), + array(array('bar'), array('foobar'), '[]'), + array(array('foo', 'bar'), array('foobar'), ''), + ); + } + + /** + * @dataProvider getGetPartialSynopsisData + */ + public function testGetPartialSynopsis($exceptArguments, $exceptOptions, $expectedSynopsis) + { + $definition = new InputDefinition( + array(new InputArgument('foo'), new InputArgument('bar'), new InputOption('foobar')) + ); + $this->assertEquals($expectedSynopsis, $definition->getPartialSynopsis($exceptArguments, $exceptOptions)); + } + protected function initializeArguments() { $this->foo = new InputArgument('foo');