Skip to content

Commit a6d0128

Browse files
committed
Merge branch 'cli-documentation'
2 parents 628e3b7 + b6868e7 commit a6d0128

File tree

6 files changed

+243
-125
lines changed

6 files changed

+243
-125
lines changed

src/Cli/Catalog/CliOptionVisibility.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,46 @@
55
use Lkrms\Concept\Enumeration;
66

77
/**
8-
* Command line option display locations
8+
* Command line option visibility flags
99
*
1010
* @extends Enumeration<int>
1111
*/
1212
final class CliOptionVisibility extends Enumeration
1313
{
14+
/**
15+
* Don't include the option in any help output
16+
*/
1417
public const NONE = 0;
1518

19+
/**
20+
* Include the option in command synopses
21+
*/
1622
public const SYNOPSIS = 1;
1723

24+
/**
25+
* Include the option when writing help to a terminal
26+
*/
1827
public const HELP = 2;
1928

29+
/**
30+
* Include the option when writing help as Markdown
31+
*/
2032
public const MARKDOWN = 4;
2133

34+
/**
35+
* Include the option when writing help as Markdown with man page extensions
36+
*/
2237
public const MAN_PAGE = 8;
2338

39+
/**
40+
* Include the option when generating shell completions
41+
*/
2442
public const COMPLETION = 16;
2543

44+
/**
45+
* Hide the option's default value if not writing help to a terminal
46+
*/
47+
public const HIDE_DEFAULT = 32;
48+
2649
public const ALL = CliOptionVisibility::SYNOPSIS | CliOptionVisibility::HELP | CliOptionVisibility::MARKDOWN | CliOptionVisibility::MAN_PAGE | CliOptionVisibility::COMPLETION;
2750
}

src/Cli/CliApplication.php

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Lkrms\Cli\Catalog\CliHelpType;
77
use Lkrms\Cli\Contract\ICliApplication;
88
use Lkrms\Cli\Exception\CliInvalidArgumentsException;
9+
use Lkrms\Cli\Support\CliHelpStyle;
910
use Lkrms\Cli\CliCommand;
1011
use Lkrms\Console\Support\ConsoleTagFormats as TagFormats;
1112
use Lkrms\Console\ConsoleFormatter as Formatter;
@@ -39,6 +40,11 @@ class CliApplication extends Application implements ICliApplication
3940
*/
4041
private $HelpType = CliHelpType::TTY;
4142

43+
/**
44+
* @var CliHelpStyle|null
45+
*/
46+
private $HelpStyle;
47+
4248
public function __construct(string $basePath = null)
4349
{
4450
parent::__construct($basePath);
@@ -203,13 +209,16 @@ public function command(array $name, string $id)
203209
private function getUsage(string $name, $node, bool $terse = false): ?string
204210
{
205211
$width = $this->getHelpWidth($terse);
212+
213+
$command = $this->getNodeCommand($name, $node);
214+
if ($command && !$terse) {
215+
return $this->buildHelp($command->getHelp(true, $width));
216+
}
217+
206218
$progName = $this->getProgramName();
207219
$fullName = trim("$progName $name");
208220

209-
if ($command = $this->getNodeCommand($name, $node)) {
210-
if (!$terse) {
211-
return $command->getHelp(true, $width);
212-
}
221+
if ($command) {
213222
return Formatter::escapeTags($command->getSynopsis(false, $width)
214223
. "\n\nSee '"
215224
. ($name ? "$progName help $name" : "$progName --help")
@@ -261,6 +270,15 @@ public function getHelpType(): int
261270
return $this->HelpType;
262271
}
263272

273+
/**
274+
* @inheritDoc
275+
*/
276+
public function getHelpStyle(): CliHelpStyle
277+
{
278+
return $this->HelpStyle
279+
?? ($this->HelpStyle = new CliHelpStyle($this->HelpType));
280+
}
281+
264282
/**
265283
* @inheritDoc
266284
*/
@@ -425,13 +443,14 @@ public function runAndExit()
425443

426444
/**
427445
* @param array<string,class-string<CliCommand>|mixed[]>|class-string<CliCommand> $node
428-
* @param CliHelpType::* $type
446+
* @param CliHelpType::* $helpType
429447
*/
430-
private function generateHelp(string $name, $node, int $type, string ...$args): int
448+
private function generateHelp(string $name, $node, int $helpType, string ...$args): int
431449
{
432-
$this->HelpType = $type;
450+
$this->HelpType = $helpType;
451+
$this->HelpStyle = null;
433452

434-
switch ($type) {
453+
switch ($helpType) {
435454
case CliHelpType::MARKDOWN:
436455
$formats = TagFormats::getMarkdownFormats();
437456
break;
@@ -442,13 +461,13 @@ private function generateHelp(string $name, $node, int $type, string ...$args):
442461
"%% %s(%d) %s | %s\n\n",
443462
strtoupper(str_replace(' ', '-', trim($this->getProgramName() . " $name"))),
444463
(int) ($args[0] ?? '1'),
445-
$args[1] ?? Composer::getRootPackageVersion(true, true),
464+
$args[1] ?? Composer::getRootPackageVersion(true),
446465
$args[2] ?? (Composer::getRootPackageName() . ' Documentation'),
447466
);
448467
break;
449468

450469
default:
451-
throw new LogicException(sprintf('Invalid help type: %d', $type));
470+
throw new LogicException(sprintf('Invalid help type: %d', $helpType));
452471
}
453472

454473
$formatter = new Formatter($formats);

0 commit comments

Comments
 (0)