Unverified Commit 8827aae8 authored by Huang Zhw's avatar Huang Zhw Committed by GitHub
Browse files

redis-cli: fix bugs in hints of commands with subcommands. (#8914)

There are two bugs in redis-cli hints:
* The hints of commands with subcommands lack first params.
* When search matching command of currently input, we should find the
command with longest matching prefix. If not COMMAND INFO will always
match COMMAND and display no hints.
parent 46d9f31e
...@@ -707,9 +707,10 @@ static void completionCallback(const char *buf, linenoiseCompletions *lc) { ...@@ -707,9 +707,10 @@ static void completionCallback(const char *buf, linenoiseCompletions *lc) {
static char *hintsCallback(const char *buf, int *color, int *bold) { static char *hintsCallback(const char *buf, int *color, int *bold) {
if (!pref.hints) return NULL; if (!pref.hints) return NULL;
int i, argc, buflen = strlen(buf); int i, rawargc, argc, buflen = strlen(buf), matchlen = 0;
sds *argv = sdssplitargs(buf,&argc); sds *rawargv, *argv = sdssplitargs(buf,&argc);
int endspace = buflen && isspace(buf[buflen-1]); int endspace = buflen && isspace(buf[buflen-1]);
helpEntry *entry = NULL;
/* Check if the argument list is empty and return ASAP. */ /* Check if the argument list is empty and return ASAP. */
if (argc == 0) { if (argc == 0) {
...@@ -717,38 +718,51 @@ static char *hintsCallback(const char *buf, int *color, int *bold) { ...@@ -717,38 +718,51 @@ static char *hintsCallback(const char *buf, int *color, int *bold) {
return NULL; return NULL;
} }
/* Search longest matching prefix command */
for (i = 0; i < helpEntriesLen; i++) { for (i = 0; i < helpEntriesLen; i++) {
if (!(helpEntries[i].type & CLI_HELP_COMMAND)) continue; if (!(helpEntries[i].type & CLI_HELP_COMMAND)) continue;
if (strcasecmp(argv[0],helpEntries[i].full) == 0 || rawargv = sdssplitargs(helpEntries[i].full,&rawargc);
strcasecmp(buf,helpEntries[i].full) == 0) if (rawargc <= argc) {
{ int j;
*color = 90; for (j = 0; j < rawargc; j++) {
*bold = 0; if (strcasecmp(rawargv[j],argv[j])) {
sds hint = sdsnew(helpEntries[i].org->params); break;
}
/* Remove arguments from the returned hint to show only the
* ones the user did not yet typed. */
int toremove = argc-1;
while(toremove > 0 && sdslen(hint)) {
if (hint[0] == '[') break;
if (hint[0] == ' ') toremove--;
sdsrange(hint,1,-1);
} }
if (j == rawargc && rawargc > matchlen) {
/* Add an initial space if needed. */ matchlen = rawargc;
if (!endspace) { entry = &helpEntries[i];
sds newhint = sdsnewlen(" ",1);
newhint = sdscatsds(newhint,hint);
sdsfree(hint);
hint = newhint;
} }
sdsfreesplitres(argv,argc);
return hint;
} }
sdsfreesplitres(rawargv,rawargc);
} }
sdsfreesplitres(argv,argc); sdsfreesplitres(argv,argc);
if (entry) {
*color = 90;
*bold = 0;
sds hint = sdsnew(entry->org->params);
/* Remove arguments from the returned hint to show only the
* ones the user did not yet type. */
int toremove = argc-matchlen;
while(toremove > 0 && sdslen(hint)) {
if (hint[0] == '[') break;
if (hint[0] == ' ') toremove--;
sdsrange(hint,1,-1);
}
/* Add an initial space if needed. */
if (!endspace) {
sds newhint = sdsnewlen(" ",1);
newhint = sdscatsds(newhint,hint);
sdsfree(hint);
hint = newhint;
}
return hint;
}
return NULL; return NULL;
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment