Skip to content

Commit

Permalink
fix: some error for cli utils
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jun 24, 2019
1 parent cd0a4d4 commit 6563a42
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
36 changes: 30 additions & 6 deletions libs/cli-utils/src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace Toolkit\Cli;

use InvalidArgumentException;
use RuntimeException;
use Throwable;
use function array_merge;
use function array_shift;
Expand All @@ -23,6 +24,8 @@
use function method_exists;
use function str_pad;
use function strlen;
use function strpos;
use function strtr;
use function trim;
use function ucfirst;

Expand Down Expand Up @@ -181,7 +184,7 @@ public function runHandler(string $command, $handler)
return $handler($this);
}

throw new InvalidArgumentException("Invalid handler of the command: $command");
throw new RuntimeException("Invalid handler of the command: $command");
}

/**
Expand All @@ -191,6 +194,11 @@ public function runHandler(string $command, $handler)
*/
protected function handleException(Throwable $e): int
{
if ($e instanceof InvalidArgumentException) {
Color::println('ERROR: ' . $e->getMessage(), 'error');
return 0;
}

$code = $e->getCode() !== 0 ? $e->getCode() : -1;

printf("Exception(%d): %s\nFile: %s(Line %d)\nTrace:\n%s\n", $code, $e->getMessage(), $e->getFile(),
Expand Down Expand Up @@ -292,24 +300,40 @@ public function displayHelp(string $err = ''): void
*/
public function displayCommandHelp(string $name): void
{
$fullCmd = $this->script . " $name";
$config = $this->messages[$name] ?? [];
$usage = "$fullCmd [args ...] [--opts ...]";
$checkVar = false;
$fullCmd = $this->script . " $name";

$config = $this->messages[$name] ?? [];
$usage = "$fullCmd [args ...] [--opts ...]";

if (!$config) {
$nodes = [
'No description for the command',
"<comment>Usage:</comment> \n $usage"
];
} else {
$checkVar = true;

$nodes = [
ucfirst($config['desc']),
"<comment>Usage:</comment> \n " . ($config['usage'] ?: $usage),
$config['help']
];
}

echo Color::render(implode("\n", $nodes));
$help = implode("\n", $nodes);

if ($checkVar && strpos($help, '{{')) {
$help = strtr($help, [
'{{command}}' => $name,
'{{fullCmd}}' => $fullCmd,
'{{workDir}}' => $this->pwd,
'{{pwdDir}}' => $this->pwd,
'{{script}}' => $this->script,
]);
}

echo Color::render($help);
}

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

/**
* @param string $name
* @param bool $default
* @param bool $default
*
* @return bool
*/
Expand Down
4 changes: 2 additions & 2 deletions libs/cli-utils/src/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,11 @@ public static function printf(string $format, ...$args): void
* @param string|array $messages
* @param string $style
*/
public function println($messages, string $style = 'info'): void
public static function println($messages, string $style = 'info'): void
{
$string = is_array($messages) ? implode("\n", $messages) : (string)$messages;

echo self::render($string, $style);
echo self::render($string . "\n", $style);
}

/*******************************************************************************
Expand Down

0 comments on commit 6563a42

Please sign in to comment.