Skip to content

Commit

Permalink
Tidy Help command output
Browse files Browse the repository at this point in the history
  • Loading branch information
g105b committed Jan 22, 2019
1 parent c07a8c5 commit 8affee1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 18 deletions.
18 changes: 5 additions & 13 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
6 changes: 5 additions & 1 deletion src/Command/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion src/Command/HelpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function __construct(

$this->applicationName = $applicationName;
$this->applicationCommandList = $applicationCommandList;
$this->applicationCommandList []= $this;
}

public function run(ArgumentValueList $arguments = null): void {
Expand All @@ -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()
);
Expand Down
9 changes: 6 additions & 3 deletions src/ErrorCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,20 @@ class ErrorCode {
const DEFAULT_CODE = 1000;

protected static $classList = [
"NO_ERROR",
NotEnoughArgumentsException::class,
CommandException::class,
InvalidCommandException::class,
MissingRequiredParameterValueException::class,
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;
}
Expand Down

0 comments on commit 8affee1

Please sign in to comment.