Skip to content

Commit 315aac6

Browse files
committedJul 19, 2022
up: update the cmd, flags help render logic
1 parent 62bb1f2 commit 315aac6

File tree

3 files changed

+33
-29
lines changed

3 files changed

+33
-29
lines changed
 

‎src/CliApp.php

+4
Original file line numberDiff line numberDiff line change
@@ -313,13 +313,17 @@ protected function doHandle(array $args): int
313313
protected function initCommandFlags(string $command, $handler): FlagsParser
314314
{
315315
$cFlags = SFlags::new();
316+
/** @var $config = COMMAND_CONFIG */
316317
$config = $this->metadata[$command] ?? [];
317318

318319
$cFlags->setDesc($config['desc']);
319320
if (!empty($config['help'])) {
320321
$cFlags->setHelp($config['help']);
321322
}
322323

324+
// update script name.
325+
$cFlags->setScriptName($cFlags->getScriptName() . ' ' . $command);
326+
323327
$cFlags->addOptsByRules($config['options'] ?? []);
324328
$cFlags->addArgsByRules($config['arguments'] ?? []);
325329

‎src/Concern/HelperRenderTrait.php

+28-28
Original file line numberDiff line numberDiff line change
@@ -113,38 +113,11 @@ protected function doBuildHelp(array $argDefines, array $optDefines, bool $withC
113113
$hasOpts = count($optDefines) > 0;
114114

115115
// ------- usage -------
116-
$binName = $this->scriptName ?: FlagUtil::getBinName();
116+
$binName = $this->getScriptName();
117117
if ($hasArgs || $hasOpts) {
118118
$buf->writeln("<ylw>Usage:</ylw> $binName [--Options ...] [Arguments ...]\n");
119119
}
120120

121-
// ------- args -------
122-
$nameTag = 'info';
123-
$fmtArgs = $this->buildArgsForHelp($argDefines);
124-
125-
if ($hasArgs) {
126-
$buf->writeln('<ylw>Arguments:</ylw>');
127-
}
128-
129-
$nameLen = $this->settings['argNameLen'];
130-
foreach ($fmtArgs as $hName => $arg) {
131-
[$desc, $lines] = $this->formatDesc($arg);
132-
133-
// write to buffer.
134-
$hName = Str::padRight($hName, $nameLen);
135-
$buf->writef(" <%s>%s</%s> %s\n", $nameTag, $hName, $nameTag, $desc);
136-
137-
// remaining desc lines
138-
if ($lines) {
139-
$indent = Str::repeat(' ', $nameLen);
140-
foreach ($lines as $line) {
141-
$buf->writef(" %s%s\n", $indent, $line);
142-
}
143-
}
144-
}
145-
146-
$hasArgs && $buf->writeln('');
147-
148121
// ------- opts -------
149122
if ($hasOpts) {
150123
$buf->writeln('<ylw>Options:</ylw>');
@@ -176,6 +149,33 @@ protected function doBuildHelp(array $argDefines, array $optDefines, bool $withC
176149
}
177150
}
178151

152+
$hasOpts && $buf->writeln('');
153+
154+
// ------- args -------
155+
// $nameTag = 'info';
156+
$fmtArgs = $this->buildArgsForHelp($argDefines);
157+
158+
if ($hasArgs) {
159+
$buf->writeln('<ylw>Arguments:</ylw>');
160+
}
161+
162+
$nameLen = $this->settings['argNameLen'];
163+
foreach ($fmtArgs as $hName => $arg) {
164+
[$desc, $lines] = $this->formatDesc($arg);
165+
166+
// write to buffer.
167+
$hName = Str::padRight($hName, $nameLen);
168+
$buf->writef(" <%s>%s</%s> %s\n", $nameTag, $hName, $nameTag, $desc);
169+
170+
// remaining desc lines
171+
if ($lines) {
172+
$indent = Str::repeat(' ', $nameLen);
173+
foreach ($lines as $line) {
174+
$buf->writef(" %s%s\n", $indent, $line);
175+
}
176+
}
177+
}
178+
179179
// --------------- extra: moreHelp, example -----------------
180180
if ($this->exampleHelp) {
181181
$buf->writeln("\n<ylw>Examples:</ylw>");

‎src/FlagsParser.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ public function setSettings(array $settings): void
699699
*/
700700
public function getScriptName(): string
701701
{
702-
return $this->scriptName;
702+
return $this->scriptName ?: FlagUtil::getBinName();
703703
}
704704

705705
/**

0 commit comments

Comments
 (0)
Please sign in to comment.