@@ -358,46 +358,73 @@ final protected function getOption(string $name): ?CliOption
358358 /**
359359 * @inheritDoc
360360 */
361- final public function getSynopsis (bool $ withMarkup = true , ?int $ width = 80 ): string
361+ final public function getSynopsis (bool $ withMarkup = true , ?int $ width = 80 , bool $ collapse = false ): string
362362 {
363363 $ pre = '' ;
364364 $ b = '' ;
365365 $ n = "\n " ;
366+
366367 if ($ withMarkup ) {
367- if ($ this ->App ->getHelpType () === CliHelpType::MAN_PAGE ) {
368- $ pre = '| ' ;
369- $ b = '` ' ;
370- $ n = "\n{$ pre } " ;
371- } else {
372- $ b = '__ ' ;
373- $ n = " \\\n\ \ \ \ " ;
368+ $ b = '__ ' ;
369+ $ n = " \\\n\ \ \ \ " ;
370+
371+ switch ($ this ->App ->getHelpType ()) {
372+ case CliHelpType::MARKDOWN :
373+ $ b = '` ' ;
374+ break ;
375+
376+ case CliHelpType::MAN_PAGE :
377+ $ pre = '| ' ;
378+ $ b = '` ' ;
379+ $ n = "\n{$ pre } " ;
380+ break ;
374381 }
375382 }
376383
377384 $ synopsis = Convert::sparseToString (' ' , [
378385 $ b . $ this ->getNameWithProgram () . $ b ,
379- $ this ->getOptionsSynopsis ($ withMarkup , true ),
386+ $ this ->getOptionsSynopsis ($ withMarkup , true , $ collapsed ),
380387 ]);
381388
382389 if ($ width !== null ) {
383- return $ pre . str_replace (
390+ $ wrapped = $ pre . str_replace (
384391 "\n" , $ n , $ this
385392 ->getLoopbackFormatter ()
386393 ->formatTags ($ synopsis , false , [$ width , $ width - 4 ], !$ withMarkup )
387394 );
395+
396+ if (!$ collapse || strpos ($ wrapped , "\n" ) === false ) {
397+ return $ wrapped ;
398+ }
399+
400+ $ synopsis = Convert::sparseToString (' ' , [
401+ $ b . $ this ->getNameWithProgram () . $ b ,
402+ $ collapsed ,
403+ ]);
388404 }
389405
390406 return $ pre . $ this
391407 ->getLoopbackFormatter ()
392408 ->formatTags ($ synopsis , false , null , !$ withMarkup );
393409 }
394410
395- private function getOptionsSynopsis (bool $ withMarkup = true , bool $ withEscapes = false ): string
411+ private function getOptionsSynopsis (bool $ withMarkup = true , bool $ withEscapes = false , ? string & $ collapsed = null ): string
396412 {
397413 $ withEscapes = $ withEscapes && !$ withMarkup ;
398- $ b = $ withMarkup ? '__ ' : '' ;
414+
415+ $ b = '' ;
399416 $ esc = ($ withMarkup || $ withEscapes ) ? '\\' : '' ;
400417
418+ if ($ withMarkup ) {
419+ $ b = '__ ' ;
420+ switch ($ this ->App ->getHelpType ()) {
421+ case CliHelpType::MARKDOWN :
422+ case CliHelpType::MAN_PAGE :
423+ $ b = '` ' ;
424+ break ;
425+ }
426+ }
427+
401428 // Produce this:
402429 //
403430 // [-ny] [--exclude PATTERN] [--verbose] --from SOURCE DEST
@@ -414,12 +441,17 @@ private function getOptionsSynopsis(bool $withMarkup = true, bool $withEscapes =
414441 $ required = [];
415442 $ positional = [];
416443
444+ $ count = 0 ;
417445 foreach ($ this ->getOptions () as $ option ) {
418446 if (!($ option ->Visibility & CliOptionVisibility::SYNOPSIS )) {
419447 continue ;
420448 }
421449
422450 if ($ option ->IsFlag ) {
451+ $ count ++;
452+ if ($ option ->MultipleAllowed ) {
453+ $ count ++;
454+ }
423455 if ($ option ->Short !== null ) {
424456 $ shortFlag [] = $ option ->Short ;
425457 continue ;
@@ -441,10 +473,13 @@ private function getOptionsSynopsis(bool $withMarkup = true, bool $withEscapes =
441473 continue ;
442474 }
443475
476+ $ count ++;
477+
444478 $ prefix = '' ;
445479 $ suffix = '' ;
446480 $ ellipsis = '' ;
447481 if ($ option ->MultipleAllowed ) {
482+ $ count ++;
448483 if ($ option ->Delimiter ) {
449484 $ valueName .= "{$ option ->Delimiter }... " ;
450485 } elseif ($ option ->ValueRequired ) {
@@ -476,42 +511,61 @@ private function getOptionsSynopsis(bool $withMarkup = true, bool $withEscapes =
476511 }
477512 }
478513
514+ $ collapsed = implode (' ' , array_filter ([
515+ $ count > 1 ? "{$ esc }[<option>]... " : '' ,
516+ $ count === 1 ? "{$ esc }[<option>] " : '' ,
517+ $ positional ? "{$ esc }[ {$ b }-- {$ b }] " . implode (' ' , $ positional ) : '' ,
518+ ]));
519+
479520 return implode (' ' , array_filter ([
480521 $ shortFlag ? "{$ esc }[ {$ b }- " . implode ('' , $ shortFlag ) . "{$ b }] " : '' ,
481522 $ optional ? implode (' ' , $ optional ) : '' ,
482523 $ required ? implode (' ' , $ required ) : '' ,
483- $ positional ? implode (' ' , $ positional ) : '' ,
524+ $ positional ? "{ $ esc } [ { $ b } -- { $ b } ] " . implode (' ' , $ positional ) : '' ,
484525 ]));
485526 }
486527
487528 final public function getHelp (bool $ withMarkup = true , ?int $ width = 80 ): string
488529 {
489- $ b = $ withMarkup ? '__ ' : '' ;
490- $ em = $ withMarkup ? '_ ' : '' ;
491- $ esc = $ withMarkup ? '\\' : '' ;
492-
493- $ indent = '' ;
494- $ beforeDescription = "\n\n" ;
495- $ beforeList = "\n\n" ;
496-
497- switch ($ this ->App ->getHelpType ()) {
498- case CliHelpType::TTY :
499- $ indent = ' ' ;
500- $ beforeDescription = "\n" . $ indent ;
501- $ beforeList = "\n" ;
502- break ;
530+ $ b = '' ;
531+ $ em = '' ;
532+ $ esc = '' ;
533+ $ indent = ' ' ;
534+ $ beforeSynopsis = '' ;
535+ $ beforeDescription = "\n" . $ indent ;
536+ $ visibility = CliOptionVisibility::HELP ;
537+ $ collapse = false ;
503538
504- case CliHelpType::MAN_PAGE :
505- $ indent = ' ' ;
506- $ beforeDescription .= ': ' ;
507- break ;
539+ if ($ withMarkup ) {
540+ $ b = '__ ' ;
541+ $ em = '_ ' ;
542+ $ esc = '\\' ;
543+
544+ switch ($ this ->App ->getHelpType ()) {
545+ case CliHelpType::MARKDOWN :
546+ $ b = '` ' ;
547+ $ indent = ' ' ;
548+ $ beforeSynopsis = '- ' ;
549+ $ beforeDescription = "\n\n" . $ indent ;
550+ $ visibility = CliOptionVisibility::MARKDOWN ;
551+ $ collapse = true ;
552+ break ;
553+
554+ case CliHelpType::MAN_PAGE :
555+ $ b = '` ' ;
556+ $ indent = ' ' ;
557+ $ beforeDescription = "\n\n: " ;
558+ $ visibility = CliOptionVisibility::MAN_PAGE ;
559+ $ collapse = true ;
560+ break ;
561+ }
508562 }
509563
510564 $ formatter = $ this ->getLoopbackFormatter ();
511565
512566 $ options = [];
513567 foreach ($ this ->getOptions () as $ option ) {
514- if (!($ option ->Visibility & CliOptionVisibility:: HELP )) {
568+ if (!($ option ->Visibility & $ visibility )) {
515569 continue ;
516570 }
517571
@@ -595,6 +649,11 @@ final public function getHelp(bool $withMarkup = true, ?int $width = 80): string
595649 $ synopsis = $ line ;
596650 }
597651
652+ if ($ valueName !== null ) {
653+ $ valueName = $ formatter ->removeTags ($ valueName );
654+ $ valueName = strtolower (Convert::splitWords ($ valueName , null , ' ' ));
655+ }
656+
598657 $ lines = [];
599658 if ($ option ->Description !== null &&
600659 ($ description = trim ($ option ->Description )) !== '' ) {
@@ -603,18 +662,21 @@ final public function getHelp(bool $withMarkup = true, ?int $width = 80): string
603662
604663 if ($ allowed ) {
605664 foreach ($ allowed as &$ value ) {
606- $ value = sprintf (" %s- { $ esc } %s " , $ indent , $ value );
665+ $ value = sprintf (' %s- %s ' , $ indent , $ value );
607666 }
608- $ lines [] = "{$ indent }{$ valueName } can be: "
609- . $ beforeList
610- . implode ("\n" , $ allowed );
667+ $ lines [] = sprintf (
668+ "%sThe %s can be: \n\n%s " ,
669+ $ indent ,
670+ $ valueName ,
671+ implode ("\n" , $ allowed )
672+ );
611673 }
612674
613675 if (!$ option ->IsFlag &&
614676 $ option ->DefaultValue !== null &&
615677 $ option ->DefaultValue !== []) {
616678 foreach ((array ) $ option ->DefaultValue as $ value ) {
617- $ default [] = sprintf ( "{ $ em} %s { $ em }" , $ formatter ->escapeTags ((string ) $ value )) ;
679+ $ default [] = $ em . $ formatter ->escapeTags ((string ) $ value ) . $ em ;
618680 }
619681 $ lines [] = sprintf (
620682 "%sThe default %s is: {$ esc } %s " ,
@@ -624,7 +686,7 @@ final public function getHelp(bool $withMarkup = true, ?int $width = 80): string
624686 );
625687 }
626688
627- $ options [] = $ synopsis
689+ $ options [] = $ beforeSynopsis . $ synopsis
628690 . ($ lines ? $ beforeDescription . ltrim (implode ("\n\n" , $ lines )) : '' );
629691 }
630692
@@ -638,7 +700,7 @@ final public function getHelp(bool $withMarkup = true, ?int $width = 80): string
638700
639701 $ sections = [
640702 'NAME ' => $ name . ' - ' . $ this ->description (),
641- 'SYNOPSIS ' => $ this ->getSynopsis ($ withMarkup , $ width ),
703+ 'SYNOPSIS ' => $ this ->getSynopsis ($ withMarkup , $ width, $ collapse ),
642704 'DESCRIPTION ' => $ this ->prepareUsage ($ this ->getLongDescription (), $ formatter , $ width ),
643705 'OPTIONS ' => implode ("\n\n" , $ options ),
644706 ] + $ sections ;
0 commit comments