Skip to content

Commit 191ed2b

Browse files
committed
update some for flag help render
1 parent 9f8c1d9 commit 191ed2b

File tree

3 files changed

+64
-50
lines changed

3 files changed

+64
-50
lines changed

example/flags-demo.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
'type' => FlagType::STRING,
4848
'desc' => 'option name is to lang, desc will print on newline',
4949
'shorts' => ['d','e'],
50+
'alias' => 'nv',
5051
// TIP: add validator limit input value.
5152
'validator' => EnumValidator::new(['one', 'two', 'three']),
5253
]);

src/AbstractParser.php

Lines changed: 58 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use Toolkit\PFlag\Contract\ParserInterface;
1010
use Toolkit\PFlag\Contract\ValidatorInterface;
1111
use Toolkit\PFlag\Exception\FlagException;
12+
use Toolkit\PFlag\Flag\Argument;
13+
use Toolkit\PFlag\Flag\Option;
1214
use Toolkit\Stdlib\Arr;
1315
use Toolkit\Stdlib\Helper\DataHelper;
1416
use Toolkit\Stdlib\Helper\IntHelper;
@@ -225,6 +227,10 @@ protected function parseRawArgs(array $rawArgs): array
225227
return $args;
226228
}
227229

230+
/****************************************************************
231+
* build and render help
232+
***************************************************************/
233+
228234
/**
229235
* display help messages
230236
*/
@@ -288,22 +294,7 @@ protected function doBuildHelp(array $argDefines, array $optDefines, bool $withC
288294

289295
$nameLen = $this->settings['argNameLen'];
290296
foreach ($fmtArgs as $hName => $arg) {
291-
$desc = $arg['desc'];
292-
if ($arg['required']) {
293-
$desc = '<red1>*</red1>' . $desc;
294-
}
295-
296-
// default value.
297-
if (isset($arg['default']) && $arg['default'] !== null) {
298-
$desc .= sprintf('(default <mga>%s</mga>)', DataHelper::toString($arg['default']));
299-
}
300-
301-
// desc has multi line
302-
$lines = [];
303-
if (strpos($desc, "\n") > 0) {
304-
$lines = explode("\n", $desc);
305-
$desc = array_shift($lines);
306-
}
297+
[$desc, $lines] = $this->formatDesc($arg);
307298

308299
// write to buffer.
309300
$hName = Str::padRight($hName, $nameLen);
@@ -331,34 +322,7 @@ protected function doBuildHelp(array $argDefines, array $optDefines, bool $withC
331322
$nameLen = $this->settings['optNameLen'];
332323
$maxWidth = $this->settings['descNlOnOptLen'];
333324
foreach ($fmtOpts as $hName => $opt) {
334-
$desc = $opt['desc'];
335-
336-
if ($opt['required']) {
337-
$desc = '<red1>*</red1>' . $desc;
338-
}
339-
340-
// validator limit
341-
if (!empty($opt['validator'])) {
342-
$v = $opt['validator'];
343-
344-
/** @see ValidatorInterface */
345-
if (is_object($v) && method_exists($v, '__toString')) {
346-
$limit = (string)$v;
347-
$desc .= $limit ? ' ' . $limit : '';
348-
}
349-
}
350-
351-
// default value.
352-
if (isset($opt['default']) && $opt['default'] !== null) {
353-
$desc .= sprintf('(default <mga>%s</mga>)', DataHelper::toString($opt['default']));
354-
}
355-
356-
// desc has multi line
357-
$lines = [];
358-
if (strpos($desc, "\n") > 0) {
359-
$lines = explode("\n", $desc);
360-
$desc = array_shift($lines);
361-
}
325+
[$desc, $lines] = $this->formatDesc($opt);
362326

363327
// need echo desc at newline.
364328
$hName = Str::padRight($hName, $nameLen);
@@ -381,6 +345,46 @@ protected function doBuildHelp(array $argDefines, array $optDefines, bool $withC
381345
return $withColor ? $buf->clear() : ColorTag::clear($buf->clear());
382346
}
383347

348+
/**
349+
* @see DEFINE_ITEM for array $define
350+
* @param array|Option|Argument $define
351+
*
352+
* @return array
353+
*/
354+
protected function formatDesc($define): array
355+
{
356+
$desc = $define['desc'];
357+
358+
if ($define['required']) {
359+
$desc = '<red1>*</red1>' . $desc;
360+
}
361+
362+
// validator limit
363+
if (!empty($define['validator'])) {
364+
$v = $define['validator'];
365+
366+
/** @see ValidatorInterface */
367+
if (is_object($v) && method_exists($v, '__toString')) {
368+
$limit = (string)$v;
369+
$desc .= $limit ? ' ' . $limit : '';
370+
}
371+
}
372+
373+
// default value.
374+
if (isset($define['default']) && $define['default'] !== null) {
375+
$desc .= sprintf('(default <mga>%s</mga>)', DataHelper::toString($define['default']));
376+
}
377+
378+
// desc has multi line
379+
$lines = [];
380+
if (strpos($desc, "\n") > 0) {
381+
$lines = explode("\n", $desc);
382+
$desc = array_shift($lines);
383+
}
384+
385+
return [$desc, $lines];
386+
}
387+
384388
/**
385389
* @param array $argDefines
386390
*
@@ -391,10 +395,9 @@ protected function buildArgsForHelp(array $argDefines): array
391395
$fmtArgs = [];
392396
$maxLen = $this->settings['argNameLen'];
393397

394-
/** @var array $arg {@see DEFINE_ITEM} */
398+
/** @var array|Argument $arg {@see DEFINE_ITEM} */
395399
foreach ($argDefines as $arg) {
396400
$helpName = $arg['name'] ?: 'arg' . $arg['index'];
397-
398401
if ($desc = $arg['desc']) {
399402
$desc = trim($desc);
400403
}
@@ -412,7 +415,7 @@ protected function buildArgsForHelp(array $argDefines): array
412415
$helpName .= $typeName ? " $typeName" : '';
413416
}
414417

415-
$maxLen = FlagUtil::getMaxInt($maxLen, strlen($helpName));
418+
$maxLen = IntHelper::getMax($maxLen, strlen($helpName));
416419

417420
// append
418421
$fmtArgs[$helpName] = $arg;
@@ -437,9 +440,14 @@ protected function buildOptsForHelp(array $optDefines): array
437440
$nameLen = $this->settings['optNameLen'];
438441
ksort($optDefines);
439442

440-
/** @var array $opt {@see DEFINE_ITEM} */
443+
/** @var array|Option $opt {@see DEFINE_ITEM} */
441444
foreach ($optDefines as $name => $opt) {
442-
$names = $opt['shorts'];
445+
$names = $opt['shorts'];
446+
/** @see Option support alias name. */
447+
if (isset($opt['alias']) && $opt['alias']) {
448+
$names[] = $opt['alias'];
449+
}
450+
// real name.
443451
$names[] = $name;
444452

445453
if ($desc = $opt['desc']) {
@@ -455,7 +463,7 @@ protected function buildOptsForHelp(array $optDefines): array
455463
$helpName .= $typeName ? " $typeName" : '';
456464
}
457465

458-
$nameLen = FlagUtil::getMaxInt($nameLen, strlen($helpName));
466+
$nameLen = IntHelper::getMax($nameLen, strlen($helpName));
459467
// append
460468
$fmtOpts[$helpName] = $opt;
461469
}

src/Flags.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use function array_shift;
1818
use function array_slice;
1919
use function count;
20+
use function is_array;
2021
use function is_string;
2122
use function ltrim;
2223
use function str_split;
@@ -616,6 +617,10 @@ public function addOptByRule(string $name, $rule): self
616617
$define = $this->parseRule($rule, $name);
617618
$option = Option::newByArray($define['name'], $define);
618619

620+
if (is_array($rule) && isset($rule['alias'])) {
621+
$option->setAlias($rule['alias']);
622+
}
623+
619624
return $this->addOption($option);
620625
}
621626

0 commit comments

Comments
 (0)