From 8affee1e4bea5c6ae2bf72d30ca5ef64002b7925 Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Tue, 22 Jan 2019 22:14:10 +0000 Subject: [PATCH] Tidy Help command output --- src/Application.php | 18 +++++------------- src/Command/Command.php | 6 +++++- src/Command/HelpCommand.php | 11 ++++++++++- src/ErrorCode.php | 9 ++++++--- 4 files changed, 26 insertions(+), 18 deletions(-) diff --git a/src/Application.php b/src/Application.php index 2c62f71..baf168e 100644 --- a/src/Application.php +++ b/src/Application.php @@ -46,7 +46,7 @@ public function run():void { if(is_null($this->arguments)) { $this->stream->writeLine( - "Application received no arguments", + "Application has no commands", Stream::ERROR ); return; @@ -60,25 +60,17 @@ public function run():void { $argumentValueList = $command->getArgumentValueList( $this->arguments ); - } - catch(CliException $exception) { - $this->stream->writeLine( - $exception->getMessage(), - Stream::ERROR - ); - } - if(is_null($command)) { - return; - } - - try { $command->checkArguments( $this->arguments ); $command->run($argumentValueList); } catch(CliException $exception) { + $this->stream->writeLine( + $exception->getMessage(), + Stream::ERROR + ); $this->stream->writeLine( $command->getUsage(), Stream::ERROR diff --git a/src/Command/Command.php b/src/Command/Command.php index 853347b..5e8b46d 100644 --- a/src/Command/Command.php +++ b/src/Command/Command.php @@ -4,6 +4,7 @@ use Gt\Cli\Argument\Argument; use Gt\Cli\Argument\ArgumentList; use Gt\Cli\Argument\ArgumentValueList; +use Gt\Cli\Argument\CommandArgument; use Gt\Cli\Argument\NamedArgument; use Gt\Cli\Argument\NotEnoughArgumentsException; use Gt\Cli\Parameter\MissingRequiredParameterException; @@ -227,7 +228,10 @@ public function getArgumentValueList( $argumentValueList = new ArgumentValueList(); foreach($arguments as $argument) { - if($argument instanceof NamedArgument) { + if($argument instanceof CommandArgument) { + continue; + } + elseif($argument instanceof NamedArgument) { /** @var NamedParameter $parameter */ $parameter = $namedParameterList[ $namedParameterIndex diff --git a/src/Command/HelpCommand.php b/src/Command/HelpCommand.php index 39540ef..139f311 100644 --- a/src/Command/HelpCommand.php +++ b/src/Command/HelpCommand.php @@ -20,6 +20,7 @@ public function __construct( $this->applicationName = $applicationName; $this->applicationCommandList = $applicationCommandList; + $this->applicationCommandList []= $this; } public function run(ArgumentValueList $arguments = null): void { @@ -35,9 +36,17 @@ public function run(ArgumentValueList $arguments = null): void { $this->writeLine("Available commands:"); + $maxNameLength = 0; + foreach($this->applicationCommandList as $command) { + $nameLength = strlen($command->getName()); + if($nameLength > $maxNameLength) { + $maxNameLength = $nameLength; + } + } + foreach($this->applicationCommandList as $command) { $this->writeLine(" • " . - $command->getName() + str_pad($command->getName(), $maxNameLength, " ") . "\t" . $command->getDescription() ); diff --git a/src/ErrorCode.php b/src/ErrorCode.php index 53d1a7f..292fc26 100644 --- a/src/ErrorCode.php +++ b/src/ErrorCode.php @@ -12,7 +12,6 @@ class ErrorCode { const DEFAULT_CODE = 1000; protected static $classList = [ - "NO_ERROR", NotEnoughArgumentsException::class, CommandException::class, InvalidCommandException::class, @@ -20,9 +19,13 @@ class ErrorCode { MissingRequiredParameterException::class, ]; - public static function get(string $exceptionClassName):int { + public static function get($exception):int { + if($exception instanceof Exception) { + $exception = get_class($exception); + } + return array_search( - $exceptionClassName, + $exception, self::$classList ) ?: self::DEFAULT_CODE; }