From cf0d7736012b5d4414e0da75ec6bcbc57dec4856 Mon Sep 17 00:00:00 2001 From: Paul Luse Date: Tue, 17 Oct 2017 19:15:55 -0700 Subject: [PATCH] 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 Reviewed-on: https://review.gerrithub.io/382910 Reviewed-by: Jim Harris Reviewed-by: Ziye Yang Tested-by: SPDK Automated Test System --- examples/blob/cli/blobcli.c | 85 +++++++++++++++++++------------------ 1 file changed, 43 insertions(+), 42 deletions(-) diff --git a/examples/blob/cli/blobcli.c b/examples/blob/cli/blobcli.c index 5e7a8bdf7..ee0b2d277 100644 --- a/examples/blob/cli/blobcli.c +++ b/examples/blob/cli/blobcli.c @@ -983,16 +983,24 @@ cmd_parser(int argc, char **argv, struct cli_context_t *cli_context) } break; case 'd': - cmd_chosen++; - cli_context->action = CLI_DUMP; - cli_context->blobid = atoll(optarg); - snprintf(cli_context->file, BUFSIZE, "%s", argv[optind]); + if (argv[optind] != NULL) { + cmd_chosen++; + cli_context->action = CLI_DUMP; + cli_context->blobid = atoll(optarg); + snprintf(cli_context->file, BUFSIZE, "%s", argv[optind]); + } else { + usage(cli_context, "ERROR: missing parameter.\n"); + } break; case 'f': - cmd_chosen++; - cli_context->action = CLI_FILL; - cli_context->blobid = atoll(optarg); - cli_context->fill_value = atoi(argv[optind]); + if (argv[optind] != NULL) { + cmd_chosen++; + cli_context->action = CLI_FILL; + cli_context->blobid = atoll(optarg); + cli_context->fill_value = atoi(argv[optind]); + } else { + usage(cli_context, "ERROR: missing parameter.\n"); + } break; case 'h': cmd_chosen++; @@ -1018,10 +1026,14 @@ cmd_parser(int argc, char **argv, struct cli_context_t *cli_context) } break; case 'r': - cmd_chosen++; - cli_context->action = CLI_REM_XATTR; - cli_context->blobid = atoll(optarg); - snprintf(cli_context->key, BUFSIZE, "%s", argv[optind]); + if (argv[optind] != NULL) { + cmd_chosen++; + cli_context->action = CLI_REM_XATTR; + cli_context->blobid = atoll(optarg); + snprintf(cli_context->key, BUFSIZE, "%s", argv[optind]); + } else { + usage(cli_context, "ERROR: missing parameter.\n"); + } break; case 'l': if (strcmp("bdevs", optarg) == 0) { @@ -1035,15 +1047,19 @@ cmd_parser(int argc, char **argv, struct cli_context_t *cli_context) } break; case 'm': - cmd_chosen++; - cli_context->action = CLI_IMPORT; - cli_context->blobid = atoll(optarg); - snprintf(cli_context->file, BUFSIZE, "%s", argv[optind]); + if (argv[optind] != NULL) { + cmd_chosen++; + cli_context->action = CLI_IMPORT; + cli_context->blobid = atoll(optarg); + snprintf(cli_context->file, BUFSIZE, "%s", argv[optind]); + } else { + usage(cli_context, "ERROR: missing parameter.\n"); + } break; case 'n': - cmd_chosen++; cli_context->num_clusters = atoi(optarg); if (cli_context->num_clusters > 0) { + cmd_chosen++; cli_context->action = CLI_CREATE_BLOB; } else { 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; case 'S': if (cli_context->cli_mode == CLI_MODE_CMD) { - cli_context->action = CLI_NONE; + cmd_chosen++; cli_context->cli_mode = CLI_MODE_SHELL; } 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; break; case 'x': - cmd_chosen++; - cli_context->action = CLI_SET_XATTR; - cli_context->blobid = atoll(optarg); - snprintf(cli_context->key, BUFSIZE, "%s", argv[optind]); - snprintf(cli_context->value, BUFSIZE, "%s", argv[optind + 1]); + if (argv[optind] != NULL || argv[optind + 1] != NULL) { + cmd_chosen++; + cli_context->action = CLI_SET_XATTR; + cli_context->blobid = atoll(optarg); + 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; default: 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) { 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 */