blobcli: refactor cmd_parser()
Moved the checking of additional args to within the switch statement and replaced hardcoded index values with optind to make their position relative to the switch as opposed to the full command list. There are also some minor fixes in here wrt the cmd_chosen value to fix issues with the 3 modes and how they handle the case when there are issues with the cmd line. Change-Id: Ic5d547298adec658fd572b9b35d72f588b843113 Signed-off-by: Paul Luse <paul.e.luse@intel.com> Reviewed-on: https://review.gerrithub.io/382910 Reviewed-by: Jim Harris <james.r.harris@intel.com> Reviewed-by: Ziye Yang <optimistyzy@gmail.com> Tested-by: SPDK Automated Test System <sys_sgsw@intel.com>
This commit is contained in:
parent
809dc4187e
commit
cf0d773601
@ -983,16 +983,24 @@ cmd_parser(int argc, char **argv, struct cli_context_t *cli_context)
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'd':
|
case 'd':
|
||||||
cmd_chosen++;
|
if (argv[optind] != NULL) {
|
||||||
cli_context->action = CLI_DUMP;
|
cmd_chosen++;
|
||||||
cli_context->blobid = atoll(optarg);
|
cli_context->action = CLI_DUMP;
|
||||||
snprintf(cli_context->file, BUFSIZE, "%s", argv[optind]);
|
cli_context->blobid = atoll(optarg);
|
||||||
|
snprintf(cli_context->file, BUFSIZE, "%s", argv[optind]);
|
||||||
|
} else {
|
||||||
|
usage(cli_context, "ERROR: missing parameter.\n");
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case 'f':
|
case 'f':
|
||||||
cmd_chosen++;
|
if (argv[optind] != NULL) {
|
||||||
cli_context->action = CLI_FILL;
|
cmd_chosen++;
|
||||||
cli_context->blobid = atoll(optarg);
|
cli_context->action = CLI_FILL;
|
||||||
cli_context->fill_value = atoi(argv[optind]);
|
cli_context->blobid = atoll(optarg);
|
||||||
|
cli_context->fill_value = atoi(argv[optind]);
|
||||||
|
} else {
|
||||||
|
usage(cli_context, "ERROR: missing parameter.\n");
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case 'h':
|
case 'h':
|
||||||
cmd_chosen++;
|
cmd_chosen++;
|
||||||
@ -1018,10 +1026,14 @@ cmd_parser(int argc, char **argv, struct cli_context_t *cli_context)
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'r':
|
case 'r':
|
||||||
cmd_chosen++;
|
if (argv[optind] != NULL) {
|
||||||
cli_context->action = CLI_REM_XATTR;
|
cmd_chosen++;
|
||||||
cli_context->blobid = atoll(optarg);
|
cli_context->action = CLI_REM_XATTR;
|
||||||
snprintf(cli_context->key, BUFSIZE, "%s", argv[optind]);
|
cli_context->blobid = atoll(optarg);
|
||||||
|
snprintf(cli_context->key, BUFSIZE, "%s", argv[optind]);
|
||||||
|
} else {
|
||||||
|
usage(cli_context, "ERROR: missing parameter.\n");
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case 'l':
|
case 'l':
|
||||||
if (strcmp("bdevs", optarg) == 0) {
|
if (strcmp("bdevs", optarg) == 0) {
|
||||||
@ -1035,15 +1047,19 @@ cmd_parser(int argc, char **argv, struct cli_context_t *cli_context)
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'm':
|
case 'm':
|
||||||
cmd_chosen++;
|
if (argv[optind] != NULL) {
|
||||||
cli_context->action = CLI_IMPORT;
|
cmd_chosen++;
|
||||||
cli_context->blobid = atoll(optarg);
|
cli_context->action = CLI_IMPORT;
|
||||||
snprintf(cli_context->file, BUFSIZE, "%s", argv[optind]);
|
cli_context->blobid = atoll(optarg);
|
||||||
|
snprintf(cli_context->file, BUFSIZE, "%s", argv[optind]);
|
||||||
|
} else {
|
||||||
|
usage(cli_context, "ERROR: missing parameter.\n");
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case 'n':
|
case 'n':
|
||||||
cmd_chosen++;
|
|
||||||
cli_context->num_clusters = atoi(optarg);
|
cli_context->num_clusters = atoi(optarg);
|
||||||
if (cli_context->num_clusters > 0) {
|
if (cli_context->num_clusters > 0) {
|
||||||
|
cmd_chosen++;
|
||||||
cli_context->action = CLI_CREATE_BLOB;
|
cli_context->action = CLI_CREATE_BLOB;
|
||||||
} else {
|
} else {
|
||||||
usage(cli_context, "ERROR: invalid option for new\n");
|
usage(cli_context, "ERROR: invalid option for new\n");
|
||||||
@ -1056,7 +1072,7 @@ cmd_parser(int argc, char **argv, struct cli_context_t *cli_context)
|
|||||||
break;
|
break;
|
||||||
case 'S':
|
case 'S':
|
||||||
if (cli_context->cli_mode == CLI_MODE_CMD) {
|
if (cli_context->cli_mode == CLI_MODE_CMD) {
|
||||||
cli_context->action = CLI_NONE;
|
cmd_chosen++;
|
||||||
cli_context->cli_mode = CLI_MODE_SHELL;
|
cli_context->cli_mode = CLI_MODE_SHELL;
|
||||||
}
|
}
|
||||||
cli_context->action = CLI_NONE;
|
cli_context->action = CLI_NONE;
|
||||||
@ -1089,11 +1105,15 @@ cmd_parser(int argc, char **argv, struct cli_context_t *cli_context)
|
|||||||
cli_context->action = CLI_SHELL_EXIT;
|
cli_context->action = CLI_SHELL_EXIT;
|
||||||
break;
|
break;
|
||||||
case 'x':
|
case 'x':
|
||||||
cmd_chosen++;
|
if (argv[optind] != NULL || argv[optind + 1] != NULL) {
|
||||||
cli_context->action = CLI_SET_XATTR;
|
cmd_chosen++;
|
||||||
cli_context->blobid = atoll(optarg);
|
cli_context->action = CLI_SET_XATTR;
|
||||||
snprintf(cli_context->key, BUFSIZE, "%s", argv[optind]);
|
cli_context->blobid = atoll(optarg);
|
||||||
snprintf(cli_context->value, BUFSIZE, "%s", argv[optind + 1]);
|
snprintf(cli_context->key, BUFSIZE, "%s", argv[optind]);
|
||||||
|
snprintf(cli_context->value, BUFSIZE, "%s", argv[optind + 1]);
|
||||||
|
} else {
|
||||||
|
usage(cli_context, "ERROR: missing parameter.\n");
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
usage(cli_context, "ERROR: invalid option\n");
|
usage(cli_context, "ERROR: invalid option\n");
|
||||||
@ -1106,25 +1126,6 @@ cmd_parser(int argc, char **argv, struct cli_context_t *cli_context)
|
|||||||
|
|
||||||
if (cli_context->cli_mode == CLI_MODE_CMD && cmd_chosen == 0) {
|
if (cli_context->cli_mode == CLI_MODE_CMD && cmd_chosen == 0) {
|
||||||
usage(cli_context, "Error: Please choose a command.\n");
|
usage(cli_context, "Error: Please choose a command.\n");
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* a few options require some extra paramters */
|
|
||||||
if (cli_context->action == CLI_SET_XATTR) {
|
|
||||||
snprintf(cli_context->key, BUFSIZE, "%s", argv[3]);
|
|
||||||
snprintf(cli_context->value, BUFSIZE, "%s", argv[4]);
|
|
||||||
}
|
|
||||||
if (cli_context->action == CLI_REM_XATTR) {
|
|
||||||
snprintf(cli_context->key, BUFSIZE, "%s", argv[3]);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (cli_context->action == CLI_DUMP ||
|
|
||||||
cli_context->action == CLI_IMPORT) {
|
|
||||||
snprintf(cli_context->file, BUFSIZE, "%s", argv[3]);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (cli_context->action == CLI_FILL) {
|
|
||||||
cli_context->fill_value = atoi(argv[3]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* in shell mode we'll call getopt multiple times so need to reset its index */
|
/* in shell mode we'll call getopt multiple times so need to reset its index */
|
||||||
|
Loading…
Reference in New Issue
Block a user