Commit cf0d7736 authored by Paul Luse's avatar Paul Luse Committed by Jim Harris
Browse files

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: default avatarPaul Luse <paul.e.luse@intel.com>
Reviewed-on: https://review.gerrithub.io/382910


Reviewed-by: default avatarJim Harris <james.r.harris@intel.com>
Reviewed-by: default avatarZiye Yang <optimistyzy@gmail.com>
Tested-by: default avatarSPDK Automated Test System <sys_sgsw@intel.com>
parent 809dc418
Loading
Loading
Loading
Loading
+43 −42
Original line number Diff line number Diff line
@@ -983,16 +983,24 @@ cmd_parser(int argc, char **argv, struct cli_context_t *cli_context)
			}
			break;
		case 'd':
			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':
			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':
			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':
			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':
			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 */