]> git.andy128k.dev Git - missing-tools.git/commitdiff
cli. use reflection to check parameters and call command
authorAndrey Kutejko <andy128k@gmail.com>
Tue, 17 Dec 2013 20:58:44 +0000 (22:58 +0200)
committerAndrey Kutejko <andy128k@gmail.com>
Tue, 17 Dec 2013 20:58:44 +0000 (22:58 +0200)
src/cli.php

index 89b5019528161ffd1fb6f4a26be181b7599f6262..33d87e51bad563383c36c502c9d780724d094169 100644 (file)
@@ -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);
             }
         }