66use Lkrms \Cli \Catalog \CliHelpType ;
77use Lkrms \Cli \Contract \ICliApplication ;
88use Lkrms \Cli \Exception \CliInvalidArgumentsException ;
9+ use Lkrms \Cli \Support \CliHelpStyle ;
910use Lkrms \Cli \CliCommand ;
1011use Lkrms \Console \Support \ConsoleTagFormats as TagFormats ;
1112use 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