Skip to content

Commit 52d86a0

Browse files
committed
Merge branch 'cli-help'
2 parents c8b4795 + d344c5c commit 52d86a0

File tree

12 files changed

+269
-203
lines changed

12 files changed

+269
-203
lines changed

lk-util/Command/Generate/Concept/GenerateCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,7 @@ protected function handleOutput($lines): void
812812
*/
813813
protected function code($value): string
814814
{
815-
return Convert::valueToCode($value, ",\n", ' => ', null, self::TAB);
815+
return Convert::valueToCode($value, ',' . \PHP_EOL, ' => ', null, self::TAB);
816816
}
817817

818818
/**
@@ -834,7 +834,7 @@ protected function indent($lines, int $levels = 1)
834834
}
835835

836836
$indent = str_repeat(self::TAB, $levels);
837-
return $indent . str_replace("\n", "\n{$indent}", $lines);
837+
return $indent . str_replace(\PHP_EOL, \PHP_EOL . $indent, $lines);
838838
}
839839

840840
/**

lk-util/Command/Http/SendHttpRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,6 @@ protected function run(string ...$args)
156156
$result = $array;
157157
}
158158

159-
echo Json::prettyPrint($result) . "\n";
159+
echo Json::prettyPrint($result) . PHP_EOL;
160160
}
161161
}

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: 17 additions & 13 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

@@ -392,7 +396,7 @@ public function run()
392396
if ($args && $args[0] === '_json_schema') {
393397
array_shift($args);
394398
$schema = $command->getJsonSchema($args[0] ?? trim($this->getProgramName() . " $name") . ' options');
395-
printf("%s\n", Json::prettyPrint($schema));
399+
echo Json::prettyPrint($schema) . PHP_EOL;
396400
return $this;
397401
}
398402

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();

src/Sync/Command/GetSyncEntities.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,11 @@ protected function run(string ...$args)
194194
$result = array_shift($result);
195195
}
196196

197-
echo Json::prettyPrint($result) . "\n";
197+
echo Json::prettyPrint($result) . PHP_EOL;
198198
} else {
199199
$count = 0;
200200
foreach ($result as $entity) {
201-
echo Json::prettyPrint($entity->toArrayWith($rules)) . "\n";
201+
echo Json::prettyPrint($entity->toArrayWith($rules)) . PHP_EOL;
202202
$count++;
203203
}
204204
}

src/Utility/Json.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ public static function stringify($value, int $flags = 0): string
4040
*/
4141
public static function prettyPrint($value, int $flags = 0): string
4242
{
43-
return json_encode($value, self::ENCODE_FLAGS | \JSON_PRETTY_PRINT | $flags);
43+
$json = json_encode($value, self::ENCODE_FLAGS | \JSON_PRETTY_PRINT | $flags);
44+
return \PHP_EOL === "\n"
45+
? $json
46+
: str_replace("\n", \PHP_EOL, $json);
4447
}
4548

4649
/**

0 commit comments

Comments
 (0)