Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6563a42

Browse files
committed
fix: some error for cli utils
1 parent cd0a4d4 commit 6563a42

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

‎libs/cli-utils/src/App.php

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace Toolkit\Cli;
1010

1111
use InvalidArgumentException;
12+
use RuntimeException;
1213
use Throwable;
1314
use function array_merge;
1415
use function array_shift;
@@ -23,6 +24,8 @@
2324
use function method_exists;
2425
use function str_pad;
2526
use function strlen;
27+
use function strpos;
28+
use function strtr;
2629
use function trim;
2730
use function ucfirst;
2831

@@ -181,7 +184,7 @@ public function runHandler(string $command, $handler)
181184
return $handler($this);
182185
}
183186

184-
throw new InvalidArgumentException("Invalid handler of the command: $command");
187+
throw new RuntimeException("Invalid handler of the command: $command");
185188
}
186189

187190
/**
@@ -191,6 +194,11 @@ public function runHandler(string $command, $handler)
191194
*/
192195
protected function handleException(Throwable $e): int
193196
{
197+
if ($e instanceof InvalidArgumentException) {
198+
Color::println('ERROR: ' . $e->getMessage(), 'error');
199+
return 0;
200+
}
201+
194202
$code = $e->getCode() !== 0 ? $e->getCode() : -1;
195203

196204
printf("Exception(%d): %s\nFile: %s(Line %d)\nTrace:\n%s\n", $code, $e->getMessage(), $e->getFile(),
@@ -292,24 +300,40 @@ public function displayHelp(string $err = ''): void
292300
*/
293301
public function displayCommandHelp(string $name): void
294302
{
295-
$fullCmd = $this->script . "$name";
296-
$config = $this->messages[$name] ?? [];
297-
$usage = "$fullCmd [args ...] [--opts ...]";
303+
$checkVar = false;
304+
$fullCmd = $this->script . "$name";
305+
306+
$config = $this->messages[$name] ?? [];
307+
$usage = "$fullCmd [args ...] [--opts ...]";
298308

299309
if (!$config) {
300310
$nodes = [
301311
'No description for the command',
302312
"<comment>Usage:</comment> \n$usage"
303313
];
304314
} else {
315+
$checkVar = true;
316+
305317
$nodes = [
306318
ucfirst($config['desc']),
307319
"<comment>Usage:</comment> \n" . ($config['usage'] ?: $usage),
308320
$config['help']
309321
];
310322
}
311323

312-
echo Color::render(implode("\n", $nodes));
324+
$help = implode("\n", $nodes);
325+
326+
if ($checkVar && strpos($help, '{{')) {
327+
$help = strtr($help, [
328+
'{{command}}' => $name,
329+
'{{fullCmd}}' => $fullCmd,
330+
'{{workDir}}' => $this->pwd,
331+
'{{pwdDir}}' => $this->pwd,
332+
'{{script}}' => $this->script,
333+
]);
334+
}
335+
336+
echo Color::render($help);
313337
}
314338

315339
/**
@@ -380,7 +404,7 @@ public function getStrOpt(string $name, string $default = ''): string
380404

381405
/**
382406
* @param string $name
383-
* @param bool $default
407+
* @param bool $default
384408
*
385409
* @return bool
386410
*/

‎libs/cli-utils/src/Color.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,11 @@ public static function printf(string $format, ...$args): void
219219
* @param string|array $messages
220220
* @param string $style
221221
*/
222-
public function println($messages, string $style = 'info'): void
222+
public staticfunction println($messages, string $style = 'info'): void
223223
{
224224
$string = is_array($messages) ? implode("\n", $messages) : (string)$messages;
225225

226-
echo self::render($string, $style);
226+
echo self::render($string . "\n", $style);
227227
}
228228

229229
/*******************************************************************************

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /