Skip to content

Commit 7093e49

Browse files
authored
Merge pull request #45 from RobSanders/optargs
Optargs
2 parents 96c7e7f + 812ec50 commit 7093e49

3 files changed

Lines changed: 38 additions & 8 deletions

File tree

clitest.c

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,24 @@ int side_length_validator(struct cli_def *cli, const char *name, const char *val
304304
return rc;
305305
}
306306

307+
int check1_validator(struct cli_def *cli, UNUSED(const char *name), UNUSED(const char *value)) {
308+
char *color;
309+
char *transparent;
310+
311+
printf("check1_validator called \n");
312+
color = cli_get_optarg_value(cli, "color", NULL);
313+
transparent = cli_get_optarg_value(cli, "transparent", NULL);
314+
315+
if (!color && !transparent) {
316+
cli_error(cli,"\nMust supply either a color or transparent!");
317+
return CLI_ERROR;
318+
} else if (color && !strcmp(color,"black") && transparent) {
319+
cli_error(cli, "\nCan not have a transparent black object!");
320+
return CLI_ERROR;
321+
}
322+
return CLI_OK;
323+
}
324+
307325
void run_child(int x) {
308326
struct cli_command *c;
309327
struct cli_def *cli;
@@ -352,11 +370,14 @@ void run_child(int x) {
352370
"Set transparent flag", NULL, NULL, NULL);
353371
cli_register_optarg(c, "verbose", CLI_CMD_OPTIONAL_FLAG | CLI_CMD_OPTION_MULTIPLE, PRIVILEGE_UNPRIVILEGED, MODE_EXEC,
354372
"Set transparent flag", NULL, NULL, NULL);
373+
cli_register_optarg(c, "color", CLI_CMD_OPTIONAL_ARGUMENT, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Set color",
374+
color_completor, color_validator, NULL);
375+
cli_register_optarg(c, "__check1__", CLI_CMD_SPOT_CHECK,PRIVILEGE_UNPRIVILEGED, MODE_EXEC,
376+
NULL, NULL, check1_validator,
377+
NULL);
355378
cli_register_optarg(c, "shape", CLI_CMD_ARGUMENT | CLI_CMD_ALLOW_BUILDMODE, PRIVILEGE_UNPRIVILEGED, MODE_EXEC,
356379
"Specify shape to calclate perimeter for", shape_completor, shape_validator,
357380
shape_transient_eval);
358-
cli_register_optarg(c, "color", CLI_CMD_OPTIONAL_ARGUMENT, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Set color",
359-
color_completor, color_validator, NULL);
360381
cli_register_optarg(c, "side_1", CLI_CMD_ARGUMENT, PRIVILEGE_UNPRIVILEGED, MODE_POLYGON_TRIANGLE,
361382
"Specify side 1 length", NULL, side_length_validator, NULL);
362383
cli_register_optarg(c, "side_1", CLI_CMD_ARGUMENT, PRIVILEGE_UNPRIVILEGED, MODE_POLYGON_RECTANGLE,

libcli.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2307,9 +2307,10 @@ int cli_int_enter_buildmode(struct cli_def *cli, struct cli_pipeline_stage *stag
23072307

23082308
// build new *limited* list of commands from this commands optargs
23092309
for (optarg = stage->command->optargs; optarg; optarg = optarg->next) {
2310-
// don't allow anything that could redefine our mode or buildmode mode, or redefine exit/cancel
2311-
if (!strcmp(optarg->name, "cancel") || (!strcmp(optarg->name, "exit"))) {
2312-
cli_error(cli, "Unable to build buildmode mode from optarg named %s", optarg->name);
2310+
// don't allow anything that could redefine our mode or buildmode mode, or redefine exit/cancel/show/unset
2311+
if (!strcmp(optarg->name, "cancel") || (!strcmp(optarg->name, "exit")) ||
2312+
!strcmp(optarg->name, "show") || (!strcmp(optarg->name, "unset"))) {
2313+
cli_error(cli, "Default buildmode command conflicts with optarg named %s", optarg->name);
23132314
rc = CLI_BUILDMODE_ERROR;
23142315
goto out;
23152316
}
@@ -2343,7 +2344,7 @@ int cli_int_enter_buildmode(struct cli_def *cli, struct cli_pipeline_stage *stag
23432344
}
23442345
}
23452346
cli->buildmode->cname = strdup(cli_command_name(cli, stage->command));
2346-
// and lastly two 'always there' commands to cancel current mode and to execute the command
2347+
// and lastly four 'always there' commands to cancel current mode and to execute the command, show settings, and unset
23472348
cli_int_register_buildmode_command(cli, NULL, "cancel", cli_int_buildmode_cancel_cback, PRIVILEGE_UNPRIVILEGED,
23482349
cli->mode, "Cancel command");
23492350
cli_int_register_buildmode_command(cli, NULL, "exit", cli_int_buildmode_exit_cback, PRIVILEGE_UNPRIVILEGED, cli->mode,
@@ -3000,7 +3001,14 @@ static void cli_int_parse_optargs(struct cli_def *cli, struct cli_pipeline_stage
30003001
* Otherwise if the word is 'blank', could be an argument, or matches 'enough' of an option/flag it is a candidate
30013002
* Once we accept an argument as a candidate, we're done looking for candidates as straight arguments are required
30023003
*/
3003-
if (stage->words[w_idx] && (oaptr->flags & (CLI_CMD_OPTIONAL_FLAG | CLI_CMD_OPTIONAL_ARGUMENT)) &&
3004+
if ((oaptr->flags & CLI_CMD_SPOT_CHECK) && (num_candidates == 0)) {
3005+
stage->status = (*oaptr->validator)(cli, NULL, NULL);
3006+
if (stage->status != CLI_OK) {
3007+
stage->error_word = stage->words[w_idx];
3008+
cli_reprompt(cli);
3009+
return;
3010+
}
3011+
} else if (stage->words[w_idx] && (oaptr->flags & (CLI_CMD_OPTIONAL_FLAG | CLI_CMD_OPTIONAL_ARGUMENT)) &&
30043012
!strcmp(oaptr->name, stage->words[w_idx])) {
30053013
candidates[0] = oaptr;
30063014
num_candidates = 1;
@@ -3035,7 +3043,7 @@ static void cli_int_parse_optargs(struct cli_def *cli, struct cli_pipeline_stage
30353043
/*
30363044
* So now we could have one or more candidates. We need to call get help/completions *only* if this is the
30373045
* 'last-word'
3038-
* Remember that last word for optinal arguments is last or next to last....
3046+
* Remember that last word for optional arguments is last or next to last....
30393047
*/
30403048
if (lastchar != '\0') {
30413049
int called_comphelp = 0;

libcli.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ enum optarg_flags {
132132
CLI_CMD_DO_NOT_RECORD = 1 << 7,
133133
CLI_CMD_REMAINDER_OF_LINE = 1 << 8,
134134
CLI_CMD_HYPHENATED_OPTION = 1 << 9,
135+
CLI_CMD_SPOT_CHECK = 1 << 10,
135136
};
136137

137138
struct cli_optarg {

0 commit comments

Comments
 (0)