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

Skip to content

Commit cf46638

Browse files
committed
Add specific replacement for help text in single command applications
1 parent 562448a commit cf46638

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
@@ -75,7 +75,7 @@ class Application
7575
private $dispatcher;
7676
private $terminal;
7777
private $defaultCommand;
78-
private $singleCommand;
78+
private $singleCommand = false;
7979
private $initialized;
8080

8181
/**
@@ -1098,6 +1098,16 @@ public function setDefaultCommand($commandName, $isSingleCommand = false)
10981098
return $this;
10991099
}
11001100

1101+
/**
1102+
* Returns whether the application is a single command application or not.
1103+
*
1104+
* @return bool Whether the application is a single command application or not
1105+
*/
1106+
public function isSingleCommand()
1107+
{
1108+
return $this->singleCommand;
1109+
}
1110+
11011111
private function splitStringByWidth($string, $width)
11021112
{
11031113
// 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
@@ -525,14 +525,15 @@ public function getHelp()
525525
public function getProcessedHelp()
526526
{
527527
$name = $this->name;
528+
$isSingleCommand = $this->application && $this->application->isSingleCommand();
528529

529530
$placeholders = array(
530531
'%command.name%',
531532
'%command.full_name%',
532533
);
533534
$replacements = array(
534535
$name,
535-
$_SERVER['PHP_SELF'].' '.$name,
536+
$isSingleCommand ? $_SERVER['PHP_SELF'] : $_SERVER['PHP_SELF'].' '.$name,
536537
);
537538

538539
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