Skip to content
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

[Console] Add specific replacement for help text in single command applications #29617

Merged
merged 1 commit into from Jan 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/Symfony/Component/Console/Application.php
Expand Up @@ -74,7 +74,7 @@ class Application
private $dispatcher;
private $terminal;
private $defaultCommand;
private $singleCommand;
private $singleCommand = false;
private $initialized;

/**
Expand Down Expand Up @@ -1162,6 +1162,14 @@ public function setDefaultCommand($commandName, $isSingleCommand = false)
return $this;
}

/**
* @internal
*/
public function isSingleCommand()
{
return $this->singleCommand;
}

private function splitStringByWidth($string, $width)
{
// str_split is not suitable for multi-byte characters, we should use preg_split to get char array properly.
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Component/Console/Command/Command.php
Expand Up @@ -533,14 +533,15 @@ public function getHelp()
public function getProcessedHelp()
{
$name = $this->name;
$isSingleCommand = $this->application && $this->application->isSingleCommand();

$placeholders = array(
'%command.name%',
'%command.full_name%',
);
$replacements = array(
$name,
$_SERVER['PHP_SELF'].' '.$name,
$isSingleCommand ? $_SERVER['PHP_SELF'] : $_SERVER['PHP_SELF'].' '.$name,
);

return str_replace($placeholders, $replacements, $this->getHelp() ?: $this->getDescription());
Expand Down
8 changes: 8 additions & 0 deletions src/Symfony/Component/Console/Tests/Command/CommandTest.php
Expand Up @@ -166,6 +166,14 @@ public function testGetProcessedHelp()
$command = new \TestCommand();
$command->setHelp('');
$this->assertContains('description', $command->getProcessedHelp(), '->getProcessedHelp() falls back to the description');

$command = new \TestCommand();
$command->setHelp('The %command.name% command does... Example: php %command.full_name%.');
$application = new Application();
$application->add($command);
$application->setDefaultCommand('namespace:name', true);
$this->assertContains('The namespace:name command does...', $command->getProcessedHelp(), '->getProcessedHelp() replaces %command.name% correctly in single command applications');
$this->assertNotContains('%command.full_name%', $command->getProcessedHelp(), '->getProcessedHelp() replaces %command.full_name% in single command applications');
}

public function testGetSetAliases()
Expand Down