help: list all commands that match string

Restore behavior where help lists all commands that
match string passed to help.

Signed-off-by: Oyvind Harboe <oyvind.harboe@zylin.com>
This commit is contained in:
Oyvind Harboe 2009-12-20 22:14:10 +01:00
parent 34bbbe7961
commit b5962b23d8

View File

@ -860,13 +860,13 @@ static COMMAND_HELPER(command_help_find, struct command *head,
} }
static COMMAND_HELPER(command_help_show, struct command *c, unsigned n, static COMMAND_HELPER(command_help_show, struct command *c, unsigned n,
bool show_help); bool show_help, const char *match);
static COMMAND_HELPER(command_help_show_list, struct command *head, unsigned n, static COMMAND_HELPER(command_help_show_list, struct command *head, unsigned n,
bool show_help) bool show_help, const char *match)
{ {
for (struct command *c = head; NULL != c; c = c->next) for (struct command *c = head; NULL != c; c = c->next)
CALL_COMMAND_HANDLER(command_help_show, c, n, show_help); CALL_COMMAND_HANDLER(command_help_show, c, n, show_help, match);
return ERROR_OK; return ERROR_OK;
} }
@ -899,7 +899,7 @@ static void command_help_show_wrap(const char *str, unsigned n, unsigned n2)
} }
} }
static COMMAND_HELPER(command_help_show, struct command *c, unsigned n, static COMMAND_HELPER(command_help_show, struct command *c, unsigned n,
bool show_help) bool show_help, const char *match)
{ {
if (!command_can_run(CMD_CTX, c)) if (!command_can_run(CMD_CTX, c))
return ERROR_OK; return ERROR_OK;
@ -908,18 +908,30 @@ static COMMAND_HELPER(command_help_show, struct command *c, unsigned n,
if (NULL == cmd_name) if (NULL == cmd_name)
return -ENOMEM; return -ENOMEM;
command_help_show_indent(n); /* If the match string occurs anywhere, we print out
LOG_USER_N("%s", cmd_name); * stuff for this command. */
bool is_match = (strstr(cmd_name, match) != NULL) ||
((c->usage != NULL) && (strstr(c->usage, match) != NULL)) ||
((c->help != NULL) && (strstr(c->help, match) != NULL));
if (is_match)
{
command_help_show_indent(n);
LOG_USER_N("%s", cmd_name);
}
free(cmd_name); free(cmd_name);
if (c->usage) { if (is_match)
LOG_USER_N(" "); {
command_help_show_wrap(c->usage, 0, n + 5); if (c->usage) {
LOG_USER_N(" ");
command_help_show_wrap(c->usage, 0, n + 5);
}
else
LOG_USER_N("\n");
} }
else
LOG_USER_N("\n");
if (show_help) if (is_match && show_help)
{ {
const char *stage_msg; const char *stage_msg;
switch (c->mode) { switch (c->mode) {
@ -942,7 +954,7 @@ static COMMAND_HELPER(command_help_show, struct command *c, unsigned n,
return ERROR_OK; return ERROR_OK;
return CALL_COMMAND_HANDLER(command_help_show_list, return CALL_COMMAND_HANDLER(command_help_show_list,
c->children, n, show_help); c->children, n, show_help, match);
} }
COMMAND_HANDLER(handle_help_command) COMMAND_HANDLER(handle_help_command)
{ {
@ -950,14 +962,15 @@ COMMAND_HANDLER(handle_help_command)
struct command *c = CMD_CTX->commands; struct command *c = CMD_CTX->commands;
if (0 == CMD_ARGC) const char *match = "";
return CALL_COMMAND_HANDLER(command_help_show_list, c, 0, full); if (CMD_ARGC == 0)
match = "";
int retval = CALL_COMMAND_HANDLER(command_help_find, c, &c); else if (CMD_ARGC == 1)
if (ERROR_OK != retval) match = CMD_ARGV[0];
return retval; else
return ERROR_COMMAND_SYNTAX_ERROR;
return CALL_COMMAND_HANDLER(command_help_show, c, 0, full);
return CALL_COMMAND_HANDLER(command_help_show_list, c, 0, full, match);
} }
static int command_unknown_find(unsigned argc, Jim_Obj *const *argv, static int command_unknown_find(unsigned argc, Jim_Obj *const *argv,
@ -975,6 +988,7 @@ static int command_unknown_find(unsigned argc, Jim_Obj *const *argv,
return command_unknown_find(--argc, ++argv, (*out)->children, out, false); return command_unknown_find(--argc, ++argv, (*out)->children, out, false);
} }
static int command_unknown(Jim_Interp *interp, int argc, Jim_Obj *const *argv) static int command_unknown(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{ {
const char *cmd_name = Jim_GetString(argv[0], NULL); const char *cmd_name = Jim_GetString(argv[0], NULL);