Skip to content

Commit f27edc4

Browse files
committed
add global options for a method help info
1 parent 605d64b commit f27edc4

File tree

5 files changed

+32
-14
lines changed

5 files changed

+32
-14
lines changed

src/Base/AbstractApplication.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,14 @@ protected function init()
112112
set_exception_handler([$this, 'handleException']);
113113
}
114114

115+
/**
116+
* @return array
117+
*/
118+
public static function getInternalOptions(): array
119+
{
120+
return self::$internalOptions;
121+
}
122+
115123
/**********************************************************
116124
* app run
117125
**********************************************************/

src/Base/AbstractCommand.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Inhere\Console\Traits\InputOutputAwareTrait;
1616
use Inhere\Console\Traits\UserInteractAwareTrait;
1717
use Inhere\Console\Utils\Annotation;
18+
use Inhere\Console\Utils\FormatUtil;
1819

1920
/**
2021
* Class AbstractCommand
@@ -413,7 +414,7 @@ protected function showHelpByMethodAnnotations($method, $action = null, array $a
413414
$help = [];
414415

415416
if ($aliases) {
416-
$help[] = sprintf("<comment>Alias Name:</comment> %s\n", implode(',', $aliases));
417+
$help['Alias Name:'] = implode(',', $aliases);
417418
}
418419

419420
foreach (array_keys(self::$annotationTags) as $tag) {
@@ -429,10 +430,21 @@ protected function showHelpByMethodAnnotations($method, $action = null, array $a
429430
$msg = self::getDescription();
430431
}
431432

432-
$help[] = "<comment>$tag:</comment>\n $msg\n";
433+
$help[$tag . ':'] = $msg;
433434
}
434435

435-
$this->output->write(implode("\n", $help), false);
436+
if (isset($help['Description:'])) {
437+
$description = $help['Description:'] ?: 'No description message for the command';
438+
$this->write(ucfirst($description) . PHP_EOL);
439+
unset($help['Description:']);
440+
}
441+
442+
$help['Global Options:'] = FormatUtil::alignmentOptions(Application::getInternalOptions());
443+
444+
$this->output->mList($help, [
445+
'sepChar' => ' ',
446+
'lastNewline' => 0,
447+
]);
436448

437449
return 0;
438450
}
@@ -525,17 +537,17 @@ public function getAnnotationVars(): array
525537
}
526538

527539
/**
528-
* @return ApplicationInterface
540+
* @return AbstractApplication
529541
*/
530-
public function getApp(): ApplicationInterface
542+
public function getApp(): AbstractApplication
531543
{
532544
return $this->app;
533545
}
534546

535547
/**
536-
* @param ApplicationInterface $app
548+
* @param AbstractApplication $app
537549
*/
538-
public function setApp(ApplicationInterface $app)
550+
public function setApp(AbstractApplication $app)
539551
{
540552
$this->app = $app;
541553
}

src/Base/BaseCommandInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ public function run($command = '');
2929
public function getDefinition();
3030

3131
/**
32-
* @return ApplicationInterface
32+
* @return AbstractApplication
3333
*/
34-
public function getApp(): ApplicationInterface;
34+
public function getApp(): AbstractApplication;
3535

3636
/**
3737
* @return string

src/Controller.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,7 @@ final public function showCommandList()
220220
$this->output->mList([
221221
'Usage:' => $usage,
222222
//'Group Name:' => "<info>$sName</info>",
223-
'Options:' => [
224-
'-h, --help' => 'Show help of the command group or specified command action',
225-
],
223+
'Options:' => FormatUtil::alignmentOptions(Application::getInternalOptions()),
226224
'Commands:' => $commands,
227225
], [
228226
'sepChar' => ' ',

src/Utils/Show.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,8 +439,8 @@ public static function multiList(array $data, array $opts = [])
439439
public static function mList(array $data, array $opts = [])
440440
{
441441
$buffer = [];
442-
$ignoreEmpty = $opts['ignoreEmpty'] ?? true;
443442
$opts['returned'] = true;
443+
$ignoreEmpty = $opts['ignoreEmpty'] ?? true;
444444

445445
foreach ($data as $title => $list) {
446446
if ($ignoreEmpty && !$list) {
@@ -450,7 +450,7 @@ public static function mList(array $data, array $opts = [])
450450
$buffer[] = self::aList($list, $title, $opts);
451451
}
452452

453-
self::write(implode("\n", $buffer));
453+
self::write(implode("\n", $buffer), $opts['lastNewline'] ?? true);
454454
}
455455

456456
/**

0 commit comments

Comments
 (0)