Skip to content

[Console] Fix synopsis when an error occurs #29320

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
Closed
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion src/Symfony/Component/Console/Command/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
34 changes: 29 additions & 5 deletions src/Symfony/Component/Console/Input/InputDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,26 +349,33 @@ 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();

if ($short && $this->getOptions()) {
$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() ? ']' : ''
);
}
Expand All @@ -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()) {
Expand All @@ -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);
}
}
17 changes: 17 additions & 0 deletions src/Symfony/Component/Console/Tests/Command/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,23 @@ public function testGetSynopsis()
$this->assertEquals('namespace:name [--foo] [--] [<bar>]', $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] [--] [<appbar>] [<bar>]', $command->getSynopsis());
}

public function testAddGetUsages()
{
$command = new \TestCommand();
Expand Down
21 changes: 21 additions & 0 deletions src/Symfony/Component/Console/Tests/Input/InputDefinitionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,27 @@ public function testGetShortSynopsis()
$this->assertEquals('[options] [--] [<cat>]', $definition->getSynopsis(true), '->getSynopsis(true) groups options in [options]');
}

public function getGetPartialSynopsisData()
{
return array(
array(array(), array(), '[--foobar] [--] [<foo>] [<bar>]'),
array(array('foo'), array(), '[--foobar] [--] [<bar>]'),
array(array('bar'), array('foobar'), '[<foo>]'),
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');
Expand Down