diff --git a/includes/cli.php b/includes/cli.php index 0bb8c48..5ea3877 100644 --- a/includes/cli.php +++ b/includes/cli.php @@ -28,14 +28,17 @@ function add_cli_command($command, $handler, $short_desc, $usage_args=false, $lo ************************************************************************************************************** */ if (php_sapi_name() != "cli") - return; + return true; + +// Store current CLI command +$cli_command = null; /** * CLI Helpers **/ function usage($error=false) { - global $cli_commands, $argv; + global $cli_commands, $cli_command, $argv; if ($error) echo "$error\n\n"; @@ -47,6 +50,8 @@ function usage($error=false) { echo "Available commands :\n"; foreach ($cli_commands as $command => $info) { + if ($cli_command && $command != $cli_command) + continue; echo " $command : ".$info['short_desc']."\n"; echo " ".$argv[0]." $command ".($info['usage_args']?$info['usage_args']:'')."\n"; if ($info['long_desc']) { @@ -61,14 +66,14 @@ function usage($error=false) { } function handle_cli_args() { - global $log_level, $cli_commands, $argv; + global $log_level, $cli_commands, $cli_command, $argv; $log_level = 'INFO'; - $command = false; + $cli_command = false; $command_args = array(); for ($i=1; $i < count($argv); $i++) { if (array_key_exists($argv[$i], $cli_commands)) { - if (!$command) - $command = $argv[$i]; + if (!$cli_command) + $cli_command = $argv[$i]; else usage("Only one command could be executed !"); } @@ -87,7 +92,7 @@ function handle_cli_args() { $log_level = 'WARNING'; break; default: - if ($command) + if ($cli_command) $command_args[] = $argv[$i]; else usage("Invalid parameter \"".$argv[$i]."\".\nNote: Command's parameter/argument must be place after the command."); @@ -95,18 +100,18 @@ function handle_cli_args() { } } - if (!$command) + if (!$cli_command) usage(); - logging('DEBUG', 'Run '.basename($argv[0])." command $command with argument(s) '".implode("', '", $command_args)."'"); + logging('DEBUG', 'Run '.basename($argv[0])." command $cli_command with argument(s) '".implode("', '", $command_args)."'"); try { - $result = call_user_func($cli_commands[$command]['handler'], $command_args); + $result = call_user_func($cli_commands[$cli_command]['handler'], $command_args); exit($result?0:1); } catch(Exception $e) { - log_exception("An exception occured running command $command"); + log_exception("An exception occured running command $cli_command"); exit(1); } }