Skip to content

Commit b77ca90

Browse files
committed
up: add more methods for render help
1 parent 97ad9b4 commit b77ca90

File tree

6 files changed

+80
-16
lines changed

6 files changed

+80
-16
lines changed

example/flags-demo.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@
7171
$arg->setDesc("this is an array arg,\n allow multi value,\n must define at last");
7272
$fs->addArgument($arg);
7373

74+
$fs->setMoreHelp('more help message ...');
75+
76+
$fs->setExampleHelp([
77+
'example usage 1',
78+
'example usage 2',
79+
]);
80+
7481
// edump($fs);
7582

7683
// do parsing

example/sflags-demo.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@
4040
$fs->setOptRules($optRules);
4141
$fs->setArgRules($argRules);
4242

43+
$fs->setMoreHelp('more help message ...');
44+
45+
$fs->setExample([
46+
'example usage 1',
47+
'example usage 2',
48+
]);
49+
4350
// do parsing
4451
try {
4552
if (!$fs->parse($flags)) {

src/Concern/HelperRenderTrait.php

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@
3030
trait HelperRenderTrait
3131
{
3232

33-
// -------------------- settings for render help --------------------
34-
3533
/**
3634
* Custom help renderer.
3735
*
@@ -46,12 +44,7 @@ trait HelperRenderTrait
4644
*/
4745
protected $autoRenderHelp = true;
4846

49-
/**
50-
* Show flag data type on render help
51-
*
52-
* @var bool
53-
*/
54-
protected $showTypeOnHelp = true;
47+
// -------------------- settings for built-in render help --------------------
5548

5649
/**
5750
* @var string|array|null
@@ -63,6 +56,18 @@ trait HelperRenderTrait
6356
*/
6457
protected $exampleHelp = '';
6558

59+
/**
60+
* Show flag data type on render help
61+
*
62+
* @var bool
63+
*/
64+
protected $showTypeOnHelp = true;
65+
66+
/**
67+
* @var callable
68+
*/
69+
private $beforePrintHelp;
70+
6671
/**
6772
* @param array $argDefines
6873
* @param array $optDefines
@@ -147,21 +152,28 @@ protected function doBuildHelp(array $argDefines, array $optDefines, bool $withC
147152
}
148153

149154
// --------------- extra: moreHelp, example -----------------
150-
if ($this->moreHelp) {
151-
$buf->writeln('<ylw>More Help:</ylw>');
155+
if ($this->exampleHelp) {
156+
$buf->writeln("\n<ylw>Examples:</ylw>");
152157

153-
$lines = is_array($this->moreHelp) ? $this->moreHelp : [$this->moreHelp];
158+
$lines = is_array($this->exampleHelp) ? $this->exampleHelp : [$this->exampleHelp];
154159
$buf->writeln(' ' . implode("\n ", $lines));;
155160
}
156161

157-
if ($this->exampleHelp) {
158-
$buf->writeln('<ylw>Examples:</ylw>');
162+
if ($this->moreHelp) {
163+
$buf->writeln("\n<ylw>More Help:</ylw>");
159164

160-
$lines = is_array($this->exampleHelp) ? $this->exampleHelp : [$this->exampleHelp];
161-
$buf->writeln(' ' . implode("\n ", $lines));;
165+
$lines = is_array($this->moreHelp) ? $this->moreHelp : [$this->moreHelp];
166+
$buf->writeln(' ' . implode("\n ", $lines));
162167
}
163168

164-
return $withColor ? $buf->clear() : ColorTag::clear($buf->clear());
169+
// fire event
170+
if ($fn = $this->beforePrintHelp) {
171+
$text = $fn($buf->getAndClear());
172+
} else {
173+
$text = $buf->getAndClear();
174+
}
175+
176+
return $withColor ? $text : ColorTag::clear($text);
165177
}
166178

167179
/**
@@ -372,11 +384,27 @@ public function getExampleHelp()
372384
return $this->exampleHelp;
373385
}
374386

387+
/**
388+
* @param array|string|null $example
389+
*/
390+
public function setExample($example): void
391+
{
392+
$this->setExampleHelp($example);
393+
}
394+
375395
/**
376396
* @param array|string|null $exampleHelp
377397
*/
378398
public function setExampleHelp($exampleHelp): void
379399
{
380400
$this->exampleHelp = $exampleHelp;
381401
}
402+
403+
/**
404+
* @param callable $beforePrintHelp
405+
*/
406+
public function setBeforePrintHelp(callable $beforePrintHelp): void
407+
{
408+
$this->beforePrintHelp = $beforePrintHelp;
409+
}
382410
}

src/Contract/ParserInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ public function getFlags(): array;
2424
*/
2525
public function getRawArgs(): array;
2626

27+
/**
28+
* @return bool
29+
*/
30+
public function isNotEmpty(): bool;
31+
2732
/**
2833
* @param array|null $flags
2934
*

src/Flags.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,14 @@ public function toArray(): array
434434
];
435435
}
436436

437+
/**
438+
* @return bool
439+
*/
440+
public function isNotEmpty(): bool
441+
{
442+
return count($this->options) > 0 || count($this->arguments) > 0;
443+
}
444+
437445
/**************************************************************************
438446
* arguments
439447
**************************************************************************/

src/SFlags.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Toolkit\PFlag\Exception\FlagParseException;
1515
use Toolkit\Stdlib\OS;
1616
use Toolkit\Stdlib\Str;
17+
use function count;
1718
use function current;
1819
use function explode;
1920
use function implode;
@@ -128,6 +129,14 @@ public function buildHelp(bool $withColor = true): string
128129
return $this->doBuildHelp($this->argDefines, $this->optDefines, $withColor);
129130
}
130131

132+
/**
133+
* @return bool
134+
*/
135+
public function isNotEmpty(): bool
136+
{
137+
return count($this->optDefines) > 0 || count($this->argDefines) > 0;
138+
}
139+
131140
/****************************************************************
132141
* parse options and arguments
133142
***************************************************************/

0 commit comments

Comments
 (0)