From 89a45ff46d59ba2d006b839fca2bb59c992842ec Mon Sep 17 00:00:00 2001 From: Andrey Kutejko Date: Tue, 17 Dec 2013 22:58:44 +0200 Subject: [PATCH] cli. use reflection to check parameters and call command --- src/cli.php | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/cli.php b/src/cli.php index 89b5019..33d87e5 100644 --- a/src/cli.php +++ b/src/cli.php @@ -58,12 +58,18 @@ abstract class CLI foreach ($this->commands() as $cmd) { list($key, $method, $description) = $cmd; if ($key === $command) { - if (method_exists($this, $method)) { - return call_user_func_array(array($this, $method), $command_args); - } else { - echo "Method $method is not defined.\n"; + $m = new \ReflectionMethod(get_class($this), $method); + if ($m->getNumberOfRequiredParameters() > count($command_args)) { + $params = $m->getParameters(); + $names = array(); + for ($i = count($command_args); $i < $m->getNumberOfRequiredParameters(); ++$i) { + $names[] = $params[$i]->name; + } + echo 'Missing required parameters: '.implode(', ', $names)."\n"; return 1; } + + return $m->invokeArgs($this, $command_args); } } -- 2.49.0