diff --git a/subsys/shell/Kconfig b/subsys/shell/Kconfig index e1c465da0c9..094224347c9 100644 --- a/subsys/shell/Kconfig +++ b/subsys/shell/Kconfig @@ -121,7 +121,17 @@ config SHELL_HELP bool "Enable help message" default y if !SHELL_MINIMAL help - Enables formatting help message when requested with '-h' or '--help'. + Enables shell functions for printing formatted help message. + +config SHELL_HELP_OPT_PARSE + bool "Parse -h and --help options" + depends on SHELL_HELP + depends on !SHELL_GETOPT + default y + help + Shell parses command to find '-h' or '--help' string. If the shell + finds the string, it will automatically print a help message + for a command. config SHELL_HELP_ON_WRONG_ARGUMENT_COUNT bool "Enable printing help on wrong argument count" diff --git a/subsys/shell/shell.c b/subsys/shell/shell.c index ce4c9bb984e..5d6eb684595 100644 --- a/subsys/shell/shell.c +++ b/subsys/shell/shell.c @@ -662,8 +662,7 @@ static int execute(const struct shell *shell) } if (IS_ENABLED(CONFIG_SHELL_HELP) && (cmd_lvl > 0) && - (!strcmp(argvp[0], "-h") || - !strcmp(argvp[0], "--help"))) { + z_shell_help_request(argvp[0])) { /* Command called with help option so it makes no sense * to search deeper commands. */ diff --git a/subsys/shell/shell_help.c b/subsys/shell/shell_help.c index e4230ffea22..4f67f44fddb 100644 --- a/subsys/shell/shell_help.c +++ b/subsys/shell/shell_help.c @@ -187,3 +187,16 @@ void z_shell_help_cmd_print(const struct shell *shell, formatted_text_print(shell, cmd->help, field_width, false); } + +bool z_shell_help_request(const char *str) +{ + if (!IS_ENABLED(CONFIG_SHELL_HELP_OPT_PARSE)) { + return false; + } + + if (!strcmp(str, "-h") || !strcmp(str, "--help")) { + return true; + } + + return false; +} diff --git a/subsys/shell/shell_help.h b/subsys/shell/shell_help.h index dc2484620b4..8f11d605de3 100644 --- a/subsys/shell/shell_help.h +++ b/subsys/shell/shell_help.h @@ -22,6 +22,9 @@ void z_shell_help_subcmd_print(const struct shell *shell, const struct shell_static_entry *cmd, const char *description); +/* Function returns true if str == -h or --help */ +bool z_shell_help_request(const char *str); + /** * @} */