From: Andrey Kutejko Date: Tue, 17 Dec 2013 20:58:44 +0000 (+0200) Subject: cli. use reflection to check parameters and call command X-Git-Tag: 0.2~20 X-Git-Url: https://git.andy128k.dev/?a=commitdiff_plain;h=89a45ff46d59ba2d006b839fca2bb59c992842ec;p=missing-tools.git cli. use reflection to check parameters and call command --- 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); } }