Skip to content

Commit

Permalink
Merge pull request #87 from adhocore/85-show-param-default
Browse files Browse the repository at this point in the history
  • Loading branch information
adhocore authored Apr 20, 2023
2 parents 14d5127 + af20917 commit b3c34ad
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/Helper/OutputHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,15 @@ protected function showHelp(string $for, array $items, string $header = '', stri

$space = 4;
$group = $lastGroup = null;

$withDefault = $for === 'Options' || $for === 'Arguments';
foreach ($this->sortItems($items, $padLen) as $item) {
$name = $this->getName($item);
if ($for === 'Commands' && $lastGroup !== $group = $item->group()) {
$this->writer->boldYellow($group ?: '*', true);
$lastGroup = $group;
}
$desc = str_replace(["\r\n", "\n"], str_pad("\n", $padLen + $space + 3), $item->desc());
$desc = str_replace(["\r\n", "\n"], str_pad("\n", $padLen + $space + 3), $item->desc($withDefault));

$this->writer->bold(' ' . str_pad($name, $padLen + $space));
$this->writer->comment($desc, true);
Expand Down
11 changes: 9 additions & 2 deletions src/Input/Parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@

use Ahc\Cli\Helper\InflectsString;

use function json_encode;
use function ltrim;
use function strpos;
use function sprintf;

/**
* Cli Parameter.
Expand Down Expand Up @@ -75,9 +78,13 @@ public function name(): string
/**
* Get description.
*/
public function desc(): string
public function desc(bool $withDefault = false): string
{
return $this->desc;
if (!$withDefault || null === $this->default || '' === $this->default) {
return $this->desc;
}

return ltrim(sprintf('%s [default: %s]', $this->desc, json_encode($this->default)));
}

/**
Expand Down
6 changes: 3 additions & 3 deletions tests/Helper/OutputHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function test_show_arguments()
'Arg Header',
'',
'Arguments:',
' [config] ',
' [config] [default: "defaultConfig"]',
' <path> The path',
'',
'Arg Footer',
Expand All @@ -65,14 +65,14 @@ public function test_show_options()
{
$this->newHelper()->showOptionsHelp([
new Option('-h --help', 'Show help'),
new Option('-n|--full-name <name>', 'Full name'),
new Option('-n|--full-name <name>', 'Full name', 'John'),
], 'Opt Header', 'Opt Footer');

$this->assertSame([
'Opt Header',
'',
'Options:',
' <-n|--full-name> Full name',
' <-n|--full-name> Full name [default: "John"]',
' [-h|--help] Show help',
'',
'Opt Footer',
Expand Down

0 comments on commit b3c34ad

Please sign in to comment.