Skip to content

Commit d344c5c

Browse files
committed
Cli: fix regression where terse synopses are double-escaped
- Always escape help messages and synopses, and rename `CliHelpTarget::INTERNAL` to `PLAIN` to better reflect this
1 parent 7a3f7e5 commit d344c5c

File tree

5 files changed

+35
-29
lines changed

5 files changed

+35
-29
lines changed

src/Cli/Catalog/CliHelpTarget.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
final class CliHelpTarget extends Enumeration
1313
{
1414
/**
15-
* Help is used internally
15+
* Help is written to a terminal with minimal formatting
1616
*/
17-
public const INTERNAL = 0;
17+
public const PLAIN = 0;
1818

1919
/**
2020
* Help is written to a terminal

src/Cli/CliApplication.php

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -257,16 +257,16 @@ private function getHelp(string $name, $node, ?CliHelpStyle $style = null): ?str
257257
*/
258258
private function getUsage(string $name, $node): ?string
259259
{
260-
$style = new CliHelpStyle(CliHelpTarget::INTERNAL, CliHelpStyle::getConsoleWidth());
260+
$style = new CliHelpStyle(CliHelpTarget::PLAIN, CliHelpStyle::getConsoleWidth());
261261

262262
$command = $this->getNodeCommand($name, $node);
263263
$progName = $this->getProgramName();
264264

265265
if ($command) {
266-
return Formatter::escapeTags($command->getSynopsis($style)
267-
. "\n\nSee '"
268-
. ($name === '' ? "$progName --help" : "$progName help $name")
269-
. "' for more information.");
266+
return $command->getSynopsis($style)
267+
. Formatter::escapeTags("\n\nSee '"
268+
. ($name === '' ? "$progName --help" : "$progName help $name")
269+
. "' for more information.");
270270
}
271271

272272
if (!is_array($node)) {
@@ -279,16 +279,20 @@ private function getUsage(string $name, $node): ?string
279279
foreach ($node as $childName => $childNode) {
280280
$command = $this->getNodeCommand(trim("$name $childName"), $childNode);
281281
if ($command) {
282-
$synopses[] = $command->getSynopsis($style);
282+
$synopsis = $command->getSynopsis($style);
283283
} elseif (is_array($childNode)) {
284-
$synopses[] = "$fullName $childName <command>";
284+
$synopsis = "$fullName $childName <command>";
285+
$synopsis = Formatter::escapeTags($synopsis);
286+
} else {
287+
continue;
285288
}
289+
$synopses[] = $synopsis;
286290
}
287291

288-
return Formatter::escapeTags(implode("\n", $synopses)
289-
. "\n\nSee '"
290-
. Arr::implode(' ', ["$progName help", $name, '<command>'])
291-
. "' for more information.");
292+
return implode("\n", $synopses)
293+
. Formatter::escapeTags("\n\nSee '"
294+
. Arr::implode(' ', ["$progName help", $name, '<command>'])
295+
. "' for more information.");
292296
}
293297

294298
/**
@@ -319,7 +323,7 @@ public function run()
319323
if ($arg === '--version' && !$args) {
320324
$appName = $this->getAppName();
321325
$version = Package::version(true, true);
322-
Console::stdout('__' . $appName . "__ $version");
326+
Console::stdout('__' . $appName . '__ ' . $version);
323327
return $this;
324328
}
325329

src/Cli/CliCommand.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Lkrms\Cli\Exception\CliInvalidArgumentsException;
1212
use Lkrms\Cli\Exception\CliUnknownValueException;
1313
use Lkrms\Cli\Support\CliHelpStyle;
14-
use Lkrms\Console\ConsoleFormatter as Formatter;
1514
use Lkrms\Facade\Console;
1615
use Lkrms\Utility\Arr;
1716
use Lkrms\Utility\Package;
@@ -213,7 +212,7 @@ final public function __invoke(string ...$args): int
213212
if ($this->HasVersionArgument) {
214213
$appName = $this->App->getAppName();
215214
$version = Package::version(true, true);
216-
Console::stdout("__{$appName}__ $version");
215+
Console::stdout('__' . $appName . '__ ' . $version);
217216
return 0;
218217
}
219218

@@ -616,11 +615,7 @@ private function getOptionsSynopsis(CliHelpStyle $style, ?string &$collapsed = n
616615
}
617616

618617
$valueName = $option->formatValueName();
619-
// Preserve angle brackets around value names if they won't be
620-
// formatted differently
621-
if (!$style->HasMarkup) {
622-
$valueName = Formatter::escapeTags($valueName);
623-
}
618+
$valueName = $style->maybeEscapeTags($valueName);
624619

625620
if ($option->IsPositional) {
626621
if ($option->MultipleAllowed) {
@@ -655,7 +650,7 @@ private function getOptionsSynopsis(CliHelpStyle $style, ?string &$collapsed = n
655650
. ($option->ValueRequired
656651
? $esc . ' ' . $valueName . $suffix
657652
: $esc . '[' . $valueName . ']' . $suffix)
658-
: $prefix . $b . "--{$option->Long}" . $b
653+
: $prefix . $b . '--' . $option->Long . $b
659654
. ($option->ValueRequired
660655
? $esc . ' ' . $valueName . $suffix
661656
: $esc . '[=' . $valueName . ']' . $suffix);
@@ -668,8 +663,8 @@ private function getOptionsSynopsis(CliHelpStyle $style, ?string &$collapsed = n
668663
}
669664

670665
$collapsed = Arr::implode(' ', [
671-
$optionalCount > 1 ? $esc . '[<options>]' : '',
672-
$optionalCount === 1 ? $esc . '[<option>]' : '',
666+
$optionalCount > 1 ? $esc . '[' . $style->maybeEscapeTags('<options>') . ']' : '',
667+
$optionalCount === 1 ? $esc . '[' . $style->maybeEscapeTags('<option>') . ']' : '',
673668
$required ? implode(' ', $required) : '',
674669
$positional ? $esc . '[' . $b . '--' . $b . '] ' . implode(' ', $positional) : '',
675670
]);

src/Cli/CliOption.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ public function __construct(
363363
$this->Short = $this->IsPositional ? null : Str::coalesce($short, null);
364364
$this->Key = $this->IsPositional ? (string) $this->Name : ($short . '|' . $long);
365365
$this->ValueName = $this->IsFlag ? null : Str::coalesce($valueName, $this->IsPositional ? Str::toKebabCase((string) $this->Name, '=') : null, 'value');
366-
$this->DisplayName = $this->IsPositional ? $this->formatValueName(false) : ($this->Long !== null ? "--{$long}" : "-{$short}");
366+
$this->DisplayName = $this->IsPositional ? $this->formatValueName(false) : ($this->Long !== null ? '--' . $long : '-' . $short);
367367
$this->ValueType = $this->IsFlag ? ($multipleAllowed ? CliOptionValueType::INTEGER : CliOptionValueType::BOOLEAN) : $valueType;
368368
$this->Delimiter = $multipleAllowed && !$this->IsFlag ? Str::coalesce($delimiter, null) : null;
369369
$this->Description = $description;

src/Cli/Support/CliHelpStyle.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ final class CliHelpStyle
5656
/**
5757
* @readonly
5858
*/
59-
public string $Escape = '';
59+
public string $Escape = '\\';
6060

6161
/**
6262
* @readonly
@@ -109,21 +109,20 @@ final class CliHelpStyle
109109
* @param CliHelpTarget::* $target
110110
*/
111111
public function __construct(
112-
int $target = CliHelpTarget::INTERNAL,
112+
int $target = CliHelpTarget::PLAIN,
113113
?int $width = null,
114114
?Formatter $formatter = null
115115
) {
116116
$this->Target = $target;
117117
$this->Width = $width;
118118

119-
if ($target === CliHelpTarget::INTERNAL) {
119+
if ($target === CliHelpTarget::PLAIN) {
120120
$this->Formatter = $formatter ?: LoopbackFormat::getFormatter();
121121
return;
122122
}
123123

124124
$this->HasMarkup = true;
125125
$this->Italic = '_';
126-
$this->Escape = '\\';
127126

128127
switch ($target) {
129128
case CliHelpTarget::TTY:
@@ -230,6 +229,14 @@ public function buildHelp(array $sections): string
230229
return Pcre::replace('/^\h++$/m', '', rtrim($help));
231230
}
232231

232+
public function maybeEscapeTags(string $string): string
233+
{
234+
if ($this->HasMarkup) {
235+
return $string;
236+
}
237+
return $this->Formatter->escapeTags($string);
238+
}
239+
233240
public static function getConsoleWidth(): ?int
234241
{
235242
$width = Console::getWidth();

0 commit comments

Comments
 (0)