Skip to content

Commit

Permalink
Merge pull request #8 from inhere/master
Browse files Browse the repository at this point in the history
fixed: do not throw ex on command not found
  • Loading branch information
inhere authored Apr 12, 2018
2 parents d87c52f + c02503f commit 1f3d3cc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
24 changes: 16 additions & 8 deletions src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,21 @@ public function annotationVars(): array
*/
public function run()
{
$cmd = input()->getCommand();
if (empty($cmd)) {
if (!$cmd = \input()->getCommand()) {
$this->baseCommand();

return;
}

/* @var HandlerMapping $router */
$router = App::getBean('commandRoute');
$handler = $router->getHandler();

if (!$handler = $router->getHandler()) {
\output()->colored("The entered command does not exist! command = $cmd", 'error');
$this->showCommandList(false);

return;
}

list($className, $method) = $handler;

Expand Down Expand Up @@ -113,14 +118,14 @@ private function indexCommand(string $className)
// 命令显示结构
$commandList = [
'Description:' => [$classDesc],
'Usage:' => [ \input()->getCommand() . ':{command} [arguments] [options]'],
'Usage:' => [\input()->getCommand() . ':{command} [arguments] [options]'],
'Commands:' => $methodCommands,
'Options:' => [
'-h, --help' => 'Show help of the command group or specified command action',
],
];

output()->writeList($commandList);
\output()->writeList($commandList);
}

/**
Expand Down Expand Up @@ -168,15 +173,16 @@ private function showCommandHelp(string $controllerClass, string $commandMethod)
$commands['Example:'] = [$docs['Example']];
}

output()->writeList($commands);
\output()->writeList($commands);
}

/**
* show all commands for the console app
*
* @param bool $showLogo
* @throws \ReflectionException
*/
private function showCommandList()
public function showCommandList(bool $showLogo = true)
{
$commands = $this->parserCmdAndDesc();

Expand All @@ -190,7 +196,9 @@ private function showCommandList()
];

// show logo
\output()->writeLogo();
if ($showLogo) {
\output()->writeLogo();
}

// output list
\output()->writeList($commandList, 'comment', 'info');
Expand Down
8 changes: 4 additions & 4 deletions src/Router/HandlerMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class HandlerMapping implements HandlerMappingInterface
* @return mixed
* @throws \InvalidArgumentException
*/
public function getHandler(...$params)
public function getHandler(...$params): array
{
list($group, $command) = $this->getGroupAndCommand();
$route = $this->getCommandString($group, $command);
Expand Down Expand Up @@ -122,10 +122,10 @@ private function getGroupAndCommand(): array
* @return mixed
* @throws \InvalidArgumentException
*/
private function match($route)
public function match(string $route): array
{
if (! isset($this->routes[$route])) {
throw new \InvalidArgumentException('the func of command is not exist,command=' . $route);
if (!isset($this->routes[$route])) {
return [];
}

return $this->routes[$route];
Expand Down

0 comments on commit 1f3d3cc

Please sign in to comment.