Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
bug #29617 [Console] Add specific replacement for help text in single…
… command applications (codedmonkey)

This PR was merged into the 3.4 branch.

Discussion
----------

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

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | #...   <!-- #-prefixed issue number(s), if any -->
| License       | MIT
| Doc PR        | <!-- required for new features -->

Simply omits the command name in the help text of single command applications which was wrongly displayed before.

For example, if the default command of an application is `echo` and the application is located at `bin/echo`, previously the help text would display `php bin/echo echo <text>` which is incorrect for single command applications since the command name ~~can~~ **must** be omitted: `php bin/echo <text>`.

Commits
-------

7058f55 [Console] Fix help text for single command applications
  • Loading branch information
Robin Chalas committed Jan 1, 2019
2 parents f82beb5 + 7058f55 commit 5c71d7b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
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

0 comments on commit 5c71d7b

Please sign in to comment.