Improve CLI usage message

This commit is contained in:
Benjamin Renard 2020-11-18 18:33:39 +01:00
parent 21e4bc935d
commit 254ac22a64

View file

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