Skip to content

Print error when missing command arguments #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
use DI\ContainerBuilder;
use PhpSchool\PhpWorkshop\Check\CheckInterface;
use PhpSchool\PhpWorkshop\Check\CheckRepository;
use PhpSchool\PhpWorkshop\Exception\MissingArgumentException;
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
use PhpSchool\PhpWorkshop\Output\OutputInterface;
use PhpSchool\PhpWorkshop\ResultRenderer\ResultRendererInterface;

/**
Expand Down Expand Up @@ -161,7 +163,21 @@ public function run()
$checkRepository->registerCheck($check);
});

$router = $container->get(CommandRouter::class);
return $router->route();
try {
$exitCode = $container->get(CommandRouter::class)->route();
} catch (MissingArgumentException $e) {
$container
->get(OutputInterface::class)
->printError(
sprintf(
'Argument%s: "%s" %s missing!',
count($e->getMissingArguments()) > 1 ? 's' : '',
implode('", "', $e->getMissingArguments()),
count($e->getMissingArguments()) > 1 ? 'are' : 'is'
)
);
return 1;
}
return $exitCode;
}
}
14 changes: 14 additions & 0 deletions src/Exception/MissingArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,18 @@
*/
class MissingArgumentException extends RuntimeException
{
/**
* @var array
*/
private $missingArguments = [];

/**
* @param $commandName
* @param array $missingArguments
*/
public function __construct($commandName, array $missingArguments)
{
$this->missingArguments = $missingArguments;
parent::__construct(
sprintf(
'Command: "%s" is missing the following arguments: "%s"',
Expand All @@ -25,4 +31,12 @@ public function __construct($commandName, array $missingArguments)
)
);
}

/**
* @return array
*/
public function getMissingArguments()
{
return $this->missingArguments;
}
}
2 changes: 2 additions & 0 deletions test/Exception/MissingArgumentExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@ public function testException()
'Command: "some-route" is missing the following arguments: "arg1", "arg2"',
$e->getMessage()
);

$this->assertSame(['arg1', 'arg2'], $e->getMissingArguments());
}
}