Skip to content

Commit b3c34ad

Browse files
authored
Merge pull request #87 from adhocore/85-show-param-default
2 parents 14d5127 + af20917 commit b3c34ad

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

src/Helper/OutputHelper.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,13 +195,15 @@ protected function showHelp(string $for, array $items, string $header = '', stri
195195

196196
$space = 4;
197197
$group = $lastGroup = null;
198+
199+
$withDefault = $for === 'Options' || $for === 'Arguments';
198200
foreach ($this->sortItems($items, $padLen) as $item) {
199201
$name = $this->getName($item);
200202
if ($for === 'Commands' && $lastGroup !== $group = $item->group()) {
201203
$this->writer->boldYellow($group ?: '*', true);
202204
$lastGroup = $group;
203205
}
204-
$desc = str_replace(["\r\n", "\n"], str_pad("\n", $padLen + $space + 3), $item->desc());
206+
$desc = str_replace(["\r\n", "\n"], str_pad("\n", $padLen + $space + 3), $item->desc($withDefault));
205207

206208
$this->writer->bold(' ' . str_pad($name, $padLen + $space));
207209
$this->writer->comment($desc, true);

src/Input/Parameter.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313

1414
use Ahc\Cli\Helper\InflectsString;
1515

16+
use function json_encode;
17+
use function ltrim;
1618
use function strpos;
19+
use function sprintf;
1720

1821
/**
1922
* Cli Parameter.
@@ -75,9 +78,13 @@ public function name(): string
7578
/**
7679
* Get description.
7780
*/
78-
public function desc(): string
81+
public function desc(bool $withDefault = false): string
7982
{
80-
return $this->desc;
83+
if (!$withDefault || null === $this->default || '' === $this->default) {
84+
return $this->desc;
85+
}
86+
87+
return ltrim(sprintf('%s [default: %s]', $this->desc, json_encode($this->default)));
8188
}
8289

8390
/**

tests/Helper/OutputHelperTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function test_show_arguments()
5454
'Arg Header',
5555
'',
5656
'Arguments:',
57-
' [config] ',
57+
' [config] [default: "defaultConfig"]',
5858
' <path> The path',
5959
'',
6060
'Arg Footer',
@@ -65,14 +65,14 @@ public function test_show_options()
6565
{
6666
$this->newHelper()->showOptionsHelp([
6767
new Option('-h --help', 'Show help'),
68-
new Option('-n|--full-name <name>', 'Full name'),
68+
new Option('-n|--full-name <name>', 'Full name', 'John'),
6969
], 'Opt Header', 'Opt Footer');
7070

7171
$this->assertSame([
7272
'Opt Header',
7373
'',
7474
'Options:',
75-
' <-n|--full-name> Full name',
75+
' <-n|--full-name> Full name [default: "John"]',
7676
' [-h|--help] Show help',
7777
'',
7878
'Opt Footer',

0 commit comments

Comments
 (0)