99namespace Toolkit \Cli ;
1010
1111use InvalidArgumentException ;
12+ use RuntimeException ;
1213use Throwable ;
1314use function array_merge ;
1415use function array_shift ;
2324use function method_exists ;
2425use function str_pad ;
2526use function strlen ;
27+ use function strpos ;
28+ use function strtr ;
2629use function trim ;
2730use 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 */
0 commit comments