Skip to content

Commit dcc69fd

Browse files
authored
Merge pull request #82 from dparrish/fix_optflag_tab_complete
Fix issue where tab completion on 'OPTIONAL_FLAG' items only matched …
2 parents 88a6688 + 6925262 commit dcc69fd

2 files changed

Lines changed: 33 additions & 10 deletions

File tree

clitest.c

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,32 @@ int side_length_validator(struct cli_def *cli, const char *name, const char *val
307307
return rc;
308308
}
309309

310+
const char *TransparentColors[] = {
311+
"clear",
312+
"transparent",
313+
"see-through",
314+
};
315+
316+
int transparent_completor(struct cli_def *cli, const char *name, const char *word, struct cli_comphelp *comphelp) {
317+
// Attempt to show matches against the following color strings
318+
const char **color;
319+
int rc = CLI_OK;
320+
printf("transparent_completor called with <%s>\n", word);
321+
for (color = TransparentColors; *color && (rc == CLI_OK); color++) {
322+
if (!word || !strncmp(*color, word, strlen(word))) {
323+
rc = cli_add_comphelp_entry(comphelp, *color);
324+
}
325+
}
326+
return rc;
327+
}
310328
int transparent_validator(struct cli_def *cli, const char *name, const char *value) {
311-
return strcasecmp("transparent", value) ? CLI_ERROR : CLI_OK;
329+
const char **color;
330+
int rc = CLI_ERROR;
331+
printf("color_validator called for %s\n", name);
332+
for (color = TransparentColors; *color; color++) {
333+
if (!strcmp(value, *color)) return CLI_OK;
334+
}
335+
return rc;
312336
}
313337

314338
int check1_validator(struct cli_def *cli, UNUSED(const char *name), UNUSED(const char *value)) {
@@ -419,7 +443,7 @@ void run_child(int x) {
419443
"Calculate perimeter of polygon\nhas embedded "
420444
"newline\nand_a_really_long_line_that_is_much_longer_than_80_columns_to_show_that_wrap_case");
421445
o = cli_register_optarg(c, "transparent", CLI_CMD_OPTIONAL_FLAG, PRIVILEGE_UNPRIVILEGED, MODE_EXEC,
422-
"Set transparent flag", NULL, NULL, NULL);
446+
"Set transparent flag", transparent_completor, transparent_validator, NULL);
423447
cli_optarg_addhelp(o, "transparent", "(any case)set to transparent");
424448

425449
cli_register_optarg(

libcli.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3110,17 +3110,17 @@ int cli_int_execute_pipeline(struct cli_def *cli, struct cli_pipeline *pipeline)
31103110
* Attempt quick dirty wrapping of helptext taking into account the offset from name, embedded
31113111
* cr/lf in helptext, and trying to split on last white-text before the right margin. If there is
31123112
* no identifiable whitespace to split on, then the split will be done on the last character to fit
3113-
* that line (currently max line with is 80 characters).
3114-
* The firstcolumn width will be a greater of 22 characters or the width of nameptr, which ever is
3113+
* that line (currently max line with is 80 characters).
3114+
* The firstcolumn width will be a greater of 22 characters or the width of nameptr, which ever is
31153115
* greater, and will be offset from the rest of the line by one space. However, if nameptr is
31163116
* greater than 22 characters it will be put on a line by itself. The first column will be formatted
31173117
* as spaces (22 of em) for all subsequent lines.
31183118
.
31193119
* This routine assumes any 'indenting' of the nameptr field has already been done, and is solely
3120-
* concerned about wrapping the combination of nameptr and helpptr to look 'nice'.
3120+
* concerned about wrapping the combination of nameptr and helpptr to look 'nice'.
31213121
*/
3122-
3123-
#define MAX(a,b) ((a) >(b) ? (a) : (b))
3122+
3123+
#define MAX(a, b) ((a) > (b) ? (a) : (b))
31243124
#define MAXWIDTHCOL1 22
31253125

31263126
void cli_int_wrap_help_line(char *nameptr, char *helpptr, struct cli_comphelp *comphelp) {
@@ -3147,8 +3147,8 @@ void cli_int_wrap_help_line(char *nameptr, char *helpptr, struct cli_comphelp *c
31473147
nameptr = emptystring;
31483148
namewidth = MAXWIDTHCOL1;
31493149
}
3150-
namewidth = MAX(MAXWIDTHCOL1,strlen(nameptr));
3151-
availwidth = maxwidth - namewidth -1; // subtract 1 for space separating col1 from rest of line
3150+
namewidth = MAX(MAXWIDTHCOL1, strlen(nameptr));
3151+
availwidth = maxwidth - namewidth - 1; // subtract 1 for space separating col1 from rest of line
31523152
toprint = strlen(helpptr);
31533153
if (toprint > availwidth) {
31543154
toprint = availwidth;
@@ -3196,7 +3196,6 @@ static void cli_get_optarg_comphelp(struct cli_def *cli, struct cli_optarg *opta
31963196
if (!(anchor_word && !strncmp(anchor_word, optarg->name, strlen(anchor_word)))) {
31973197
delim_start = DELIM_OPT_START;
31983198
delim_end = DELIM_OPT_END;
3199-
get_completions = NULL; // No point, completor of field is the name itself
32003199
}
32013200
} else if (optarg->flags & CLI_CMD_HYPHENATED_OPTION) {
32023201
delim_start = DELIM_OPT_START;

0 commit comments

Comments
 (0)