Skip to content

Commit 454ca17

Browse files
committed
breaking: refactor the get opt help data
1 parent 3bffa4e commit 454ca17

File tree

4 files changed

+61
-26
lines changed

4 files changed

+61
-26
lines changed

src/Concern/HelperRenderTrait.php

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use function strlen;
2424
use function strpos;
2525
use function trim;
26+
use function ucfirst;
2627

2728
/**
2829
* trait HelperRenderTrait
@@ -199,8 +200,7 @@ protected function doBuildHelp(array $argDefines, array $optDefines, bool $withC
199200
*/
200201
protected function formatDesc($define): array
201202
{
202-
$desc = $define['desc'];
203-
203+
$desc = $define['desc'] ?: 'No description';
204204
if ($define['required']) {
205205
$desc = '<red1>*</red1>' . $desc;
206206
}
@@ -337,6 +337,36 @@ protected function buildOptsForHelp(array $optDefines, bool $hasShortOpt): array
337337
return $fmtOpts;
338338
}
339339

340+
/**
341+
* @param string $name
342+
* @param array $opt
343+
*
344+
* @return array<string, string>
345+
*/
346+
protected function buildOptHelpLine(string $name, array $opt): array
347+
{
348+
$names = $opt['shorts'];
349+
$names[] = $name;
350+
351+
$helpName = FlagUtil::buildOptHelpName($names);
352+
353+
// show type name.
354+
if ($this->showTypeOnHelp) {
355+
$typeName = $opt['helpType'] ?: FlagType::getHelpName($opt['type']);
356+
$helpName .= $typeName ? " $typeName" : '';
357+
}
358+
359+
$opt['desc'] = $opt['desc'] ? ucfirst($opt['desc']) : "Option $name";
360+
361+
// format desc
362+
[$desc, $otherLines] = $this->formatDesc($opt);
363+
if ($otherLines) {
364+
$desc .= "\n" . implode("\n", $otherLines);
365+
}
366+
367+
return [$helpName, $desc];
368+
}
369+
340370
/****************************************************************
341371
* getter/setter methods
342372
***************************************************************/

src/Contract/ParserInterface.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,20 +247,32 @@ public function getOpts(): array;
247247
public function getArgs(): array;
248248

249249
/**
250-
* Get args help data
250+
* Get args help lines data
251+
*
252+
* ```php
253+
* [
254+
* helpName => format desc,
255+
* ]
256+
* ```
251257
*
252258
* @return array
253259
* @psalm-return array<string, string>
254260
*/
255-
public function getArgsHelpData(): array;
261+
public function getArgsHelpLines(): array;
256262

257263
/**
258-
* Get opts help data
264+
* Get opts help lines data
265+
*
266+
* ```php
267+
* [
268+
* helpName => format desc,
269+
* ]
270+
* ```
259271
*
260272
* @return array
261273
* @psalm-return array<string, string>
262274
*/
263-
public function getOptsHelpData(): array;
275+
public function getOptsHelpLines(): array;
264276

265277
public function lock(): void;
266278

src/Flags.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use function str_split;
2929
use function strlen;
3030
use function substr;
31+
use function ucfirst;
3132

3233
/**
3334
* Class Flags
@@ -1009,7 +1010,7 @@ public function getOpts(): array
10091010
/**
10101011
* @return array
10111012
*/
1012-
public function getArgsHelpData(): array
1013+
public function getArgsHelpLines(): array
10131014
{
10141015
$helpData = [];
10151016
foreach ($this->arguments as $arg) {
@@ -1024,20 +1025,16 @@ public function getArgsHelpData(): array
10241025
/**
10251026
* @return array
10261027
*/
1027-
public function getOptsHelpData(): array
1028+
public function getOptsHelpLines(): array
10281029
{
10291030
$helpData = [];
10301031
foreach ($this->options as $name => $opt) {
10311032
if ($this->showHiddenOpt === false && $opt['hidden']) {
10321033
continue;
10331034
}
10341035

1035-
$names = $opt['shorts'];
1036-
$names[] = $name;
1037-
1038-
$helpName = FlagUtil::buildOptHelpName($names);
1039-
// append
1040-
$helpData[$helpName] = $opt->getDesc(true);
1036+
[$helpName, $fmtDesc] = $this->buildOptHelpLine($name, $opt->toArray());
1037+
$helpData[$helpName] = $fmtDesc;
10411038
}
10421039

10431040
return $helpData;

src/SFlags.php

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,7 +1012,7 @@ public function getArgs(): array
10121012
/**
10131013
* @return array
10141014
*/
1015-
public function getArgsHelpData(): array
1015+
public function getArgsHelpLines(): array
10161016
{
10171017
$helpData = [];
10181018
foreach ($this->argDefines as $define) {
@@ -1027,23 +1027,19 @@ public function getArgsHelpData(): array
10271027
/**
10281028
* @return array
10291029
*/
1030-
public function getOptsHelpData(): array
1030+
public function getOptsHelpLines(): array
10311031
{
1032-
$helpData = [];
1033-
foreach ($this->optDefines as $name => $define) {
1034-
if ($this->showHiddenOpt === false && $define['hidden']) {
1032+
$helpLines = [];
1033+
foreach ($this->optDefines as $name => $opt) {
1034+
if ($this->showHiddenOpt === false && $opt['hidden']) {
10351035
continue;
10361036
}
10371037

1038-
$names = $define['shorts'];
1039-
$names[] = $name;
1040-
1041-
$helpName = FlagUtil::buildOptHelpName($names);
1042-
1043-
$helpData[$helpName] = ucfirst($define['desc']);
1038+
[$helpName, $fmtDesc] = $this->buildOptHelpLine($name, $opt);
1039+
$helpLines[$helpName] = $fmtDesc;
10441040
}
10451041

1046-
return $helpData;
1042+
return $helpLines;
10471043
}
10481044

10491045
/**

0 commit comments

Comments
 (0)