8000 Add specific replacement for help text in single command applications · symfony/symfony@2f46e24 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2f46e24

Browse files
committed
Add specific replacement for help text in single command applications
1 parent d1bf595 commit 2f46e24

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class Application
7474
private $dispatcher;
7575
private $terminal;
7676
private $defaultCommand;
77-
private $singleCommand;
77+
private $singleCommand = false;
7878
private $initialized;
7979

8080
/**
@@ -1162,6 +1162,16 @@ public function setDefaultCommand($commandName, $isSingleCommand = false)
11621162
return $this;
11631163
}
11641164

1165+
/**
1166+
* Returns whether the application is a single command application or not.
1167+
*
1168+
* @return bool Whether the application is a single command application or not
1169+
*/
1170+
public function isSingleCommandApplication()
1171+
{
1172+
return $this->singleCommand;
1173+
}
1174+
11651175
private function splitStringByWidth($string, $width)
11661176
{
11671177
// str_split is not suitable for multi-byte characters, we should use preg_split to get char array properly.

src/Symfony/Component/Console/Command/Command.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,14 +533,15 @@ public function getHelp()
533533
public function getProcessedHelp()
534534
{
535535
$name = $this->name;
536+
$isSingleCommand = $this->application && $this->application->isSingleCommandApplication();
536537

537538
$placeholders = array(
538539
'%command.name%',
539540
'%command.full_name%',
540541
);
541542
$replacements = array(
542543
$name,
543-
$_SERVER['PHP_SELF'].' '.$name,
544+
$isSingleCommand ? $_SERVER['PHP_SELF'] : $_SERVER['PHP_SELF'].' '.$name,
544545
);
545546

546547
return str_replace($placeholders, $replacements, $this->getHelp() ?: $this->getDescription());

src/Symfony/Component/Console/Tests/Command/CommandTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,14 @@ public function testGetProcessedHelp()
166166
$command = new \TestCommand();
167167
$command->setHelp('');
168168
$this->assertContains('description', $command->getProcessedHelp(), '->getProcessedHelp() falls back to the description');
169+
170+
$command = new \TestCommand();
171+
$command->setHelp('The %command.name% command does... Example: php %command.full_name%.');
172+
$application = new Application();
173+
$application->add($command);
174+
$application->setDefaultCommand('namespace:name', true);
175+
$this->assertContains('The namespace:name command does...', $command->getProcessedHelp(), '->getProcessedHelp() replaces %command.name% correctly in single command applications');
176+
$this->assertNotContains('%command.full_name%', $command->getProcessedHelp(), '->getProcessedHelp() replaces %command.full_name% in single command applications');
169177
}
170178

171179
public function testGetSetAliases()

0 commit comments

Comments
 (0)
0