shell: help functions update

Functions shell_help_subcmd_print and shell_help_cmd_print
are more generic. Now they can operate on command passed as an
argument not hard coded active_cmd.

Signed-off-by: Jakub Rzeszutko <jakub.rzeszutko@nordisemi.no>
This commit is contained in:
Jakub Rzeszutko 2020-10-01 10:38:00 +02:00 committed by Anas Nashif
commit 137b7ec5bd
3 changed files with 24 additions and 17 deletions

View file

@ -49,8 +49,9 @@ static void shell_internal_help_print(const struct shell *shell)
return;
}
shell_help_cmd_print(shell);
shell_help_subcmd_print(shell);
shell_help_cmd_print(shell, &shell->ctx->active_cmd);
shell_help_subcmd_print(shell, &shell->ctx->active_cmd,
"Subcommands:\n");
}
/**

View file

@ -142,13 +142,14 @@ static void help_item_print(const struct shell *shell, const char *item_name,
formatted_text_print(shell, item_help, offset, false);
}
/* Function is printing command help, its subcommands name and subcommands
* help string.
/* Function prints all subcommands of the parent command together with their
* help string
*/
void shell_help_subcmd_print(const struct shell *shell)
void shell_help_subcmd_print(const struct shell *shell,
const struct shell_static_entry *parent,
const char *description)
{
const struct shell_static_entry *entry = NULL;
const struct shell_static_entry *parent = &shell->ctx->active_cmd;
struct shell_static_entry dloc;
uint16_t longest = 0U;
size_t idx = 0;
@ -163,7 +164,9 @@ void shell_help_subcmd_print(const struct shell *shell)
return;
}
shell_internal_fprintf(shell, SHELL_NORMAL, "Subcommands:\n");
if (description != NULL) {
shell_internal_fprintf(shell, SHELL_NORMAL, description);
}
/* Printing subcommands and help string (if exists). */
idx = 0;
@ -173,16 +176,16 @@ void shell_help_subcmd_print(const struct shell *shell)
}
}
void shell_help_cmd_print(const struct shell *shell)
void shell_help_cmd_print(const struct shell *shell,
const struct shell_static_entry *cmd)
{
static const char cmd_sep[] = " - "; /* commands separator */
static const char cmd_sep[] = " - "; /* commands separator */
uint16_t field_width;
uint16_t field_width = shell_strlen(shell->ctx->active_cmd.syntax) +
shell_strlen(cmd_sep);
field_width = shell_strlen(cmd->syntax) + shell_strlen(cmd_sep);
shell_internal_fprintf(shell, SHELL_NORMAL, "%s%s",
shell->ctx->active_cmd.syntax, cmd_sep);
cmd->syntax, cmd_sep);
formatted_text_print(shell, shell->ctx->active_cmd.help,
field_width, false);
formatted_text_print(shell, cmd->help, field_width, false);
}

View file

@ -14,10 +14,13 @@ extern "C" {
#endif
/* Function is printing command help string. */
void shell_help_cmd_print(const struct shell *shell);
void shell_help_cmd_print(const struct shell *shell,
const struct shell_static_entry *cmd);
/* Function is printing subcommands help string. */
void shell_help_subcmd_print(const struct shell *shell);
/* Function is printing subcommands and help string. */
void shell_help_subcmd_print(const struct shell *shell,
const struct shell_static_entry *cmd,
const char *description);
/**
* @}