From 01f77d096e1c6542c2fa968c5ea9cf46b7d76ac0 Mon Sep 17 00:00:00 2001 From: Martin Price Date: Mon, 2 Sep 2024 19:21:05 +0100 Subject: [PATCH] Show command help for missing argument and required option (#427) * Show command help for missing argument and required option * Add info message before help text to give context. --- commands/user.bee.inc | 1 + includes/command.inc | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/commands/user.bee.inc b/commands/user.bee.inc index a86f6a29..1da97330 100644 --- a/commands/user.bee.inc +++ b/commands/user.bee.inc @@ -209,6 +209,7 @@ function user_create_bee_callback($arguments, $options) { } if (!isset($options['mail'])) { bee_message(bt("Mail option missing."), 'error'); + bee_command_show_help(); return; } if (!valid_email_address($options['mail'])) { diff --git a/includes/command.inc b/includes/command.inc index ce7e0fb3..ebf1da2f 100644 --- a/includes/command.inc +++ b/includes/command.inc @@ -291,6 +291,7 @@ function bee_validate_command(array $descriptor) { bee_message(bt("Argument '!name' is required.", array( '!name' => $name, )), 'error'); + bee_command_show_help(); return FALSE; } } @@ -327,3 +328,24 @@ function bee_validate_command(array $descriptor) { return TRUE; } + +/** + * Show the help text for a command. + */ +function bee_command_show_help() { + global $_bee_command, $_bee_output; + + // Show a message to give context to the following help. + bee_message(bt('To help you get it right, here is the help text for this command:'), 'info'); + + // Get the command descriptor. + $commands = bee_all_commands(); + $command_descriptor = $commands[$_bee_command]; + + // The help function we call expects the command key to exist within the + // descriptor containing the command. + $command_descriptor['command'] = $_bee_command; + + // Set the output to be the help text for the command. + $_bee_output = help_bee_command_help($command_descriptor); +}