summaryrefslogtreecommitdiffstats
path: root/cli
diff options
context:
space:
mode:
authorCsaba Henk <csaba@gluster.com>2010-09-15 08:11:15 +0000
committerVijay Bellur <vijay@dev.gluster.com>2010-09-16 23:07:27 -0700
commit63ec13f022ae99c1664fbdbc9e6623903ba843af (patch)
treefdde04563f5081e4322c0bf75541631293100fd8 /cli
parent47bc630dca589235d7c10e2748cebaa5e419d5d7 (diff)
cli: add a simplistic option parser, convert "mode script" to "--mode=script"
Signed-off-by: Csaba Henk <csaba@gluster.com> Signed-off-by: Vijay Bellur <vijay@dev.gluster.com> BUG: 1570 (geosync related changes) URL: http://bugs.gluster.com/cgi-bin/bugzilla3/show_bug.cgi?id=1570
Diffstat (limited to 'cli')
-rw-r--r--cli/src/cli-cmd-misc.c10
-rw-r--r--cli/src/cli-cmd-volume.c2
-rw-r--r--cli/src/cli.c38
-rw-r--r--cli/src/cli.h5
-rw-r--r--cli/src/input.c21
5 files changed, 44 insertions, 32 deletions
diff --git a/cli/src/cli-cmd-misc.c b/cli/src/cli-cmd-misc.c
index 7833456b730..306a7628faa 100644
--- a/cli/src/cli-cmd-misc.c
+++ b/cli/src/cli-cmd-misc.c
@@ -61,10 +61,8 @@ cli_cmd_display_help (struct cli_state *state, struct cli_cmd_word *in_word,
for (cmd = cli_probe_cmds; cmd->pattern; cmd++)
cli_out ("%s - %s", cmd->pattern, cmd->desc);
- for (cmd = cli_misc_cmds; cmd->pattern; cmd++) {
- if (cmd->cbk)
- cli_out ("%s - %s", cmd->pattern, cmd->desc);
- }
+ for (cmd = cli_misc_cmds; cmd->pattern; cmd++)
+ cli_out ("%s - %s", cmd->pattern, cmd->desc);
if (!state->rl_enabled)
exit (0);
@@ -81,10 +79,6 @@ struct cli_cmd cli_misc_cmds[] = {
cli_cmd_display_help,
"display command options"},
- { "mode script",
- NULL,
- "assign gluster execution mode"},
-
{ NULL, NULL, NULL }
};
diff --git a/cli/src/cli-cmd-volume.c b/cli/src/cli-cmd-volume.c
index 539c7eeac11..b8a5df6d642 100644
--- a/cli/src/cli-cmd-volume.c
+++ b/cli/src/cli-cmd-volume.c
@@ -242,7 +242,7 @@ cli_cmd_get_confirmation (struct cli_state *state, const char *question)
char answer = '\0';
char flush = '\0';
- if (GLUSTER_MODE_SCRIPT == state->mode)
+ if (state->mode & GLUSTER_MODE_SCRIPT)
return GF_ANSWER_YES;
printf ("%s (y/n) ", question);
answer = getchar ();
diff --git a/cli/src/cli.c b/cli/src/cli.c
index 58cb61bb417..4e25417f9ba 100644
--- a/cli/src/cli.c
+++ b/cli/src/cli.c
@@ -325,13 +325,49 @@ cli_rpc_notify (struct rpc_clnt *rpc, void *mydata, rpc_clnt_event_t event,
}
int
+cli_opt_parse (char *opt, struct cli_state *state)
+{
+ char *oarg;
+
+ oarg = strtail (opt, "mode=");
+ if (oarg) {
+ if (strcmp (oarg, "script") == 0) {
+ state->mode |= GLUSTER_MODE_SCRIPT;
+ return 0;
+ }
+ if (strcmp (oarg, "interactive") == 0)
+ return 0;
+ return -1;
+ }
+
+ return -1;
+}
+
+int
parse_cmdline (int argc, char *argv[], struct cli_state *state)
{
int ret = 0;
+ int i = 0;
+ int j = 0;
+ char *opt = NULL;
- state->argc=argc-1;
+ state->argc=argc-1;
state->argv=&argv[1];
+ for (i = 0; i < state->argc; i++) {
+ opt = strtail (state->argv[i], "--");
+ if (opt) {
+ ret = cli_opt_parse (opt, state);
+ if (ret == -1) {
+ cli_out ("unrecognized option --%s", opt);
+ return ret;
+ }
+ for (j = i; j < state->argc - 1; j++)
+ state->argv[j] = state->argv[j + 1];
+ state->argc--;
+ }
+ }
+
return ret;
}
diff --git a/cli/src/cli.h b/cli/src/cli.h
index eaa638c29d6..62aa02079da 100644
--- a/cli/src/cli.h
+++ b/cli/src/cli.h
@@ -39,9 +39,8 @@ enum argp_option_keys {
ARGP_PORT_KEY = 'p',
};
-enum gluster_mode {
- GLUSTER_MODE_SCRIPT = 1
-};
+#define GLUSTER_MODE_SCRIPT (1 << 0)
+
struct cli_state;
struct cli_cmd_word;
struct cli_cmd_tree;
diff --git a/cli/src/input.c b/cli/src/input.c
index 2c67b354d4c..7c3e78b16ca 100644
--- a/cli/src/input.c
+++ b/cli/src/input.c
@@ -41,10 +41,7 @@ cli_batch (void *d)
state = d;
- if (state->mode == GLUSTER_MODE_SCRIPT)
- ret = cli_cmd_process (state, state->argc - 2, state->argv + 2);
- else
- ret = cli_cmd_process (state, state->argc, state->argv);
+ ret = cli_cmd_process (state, state->argc, state->argv);
gf_log ("", GF_LOG_NORMAL, "Exiting with: %d", ret);
exit (ret);
@@ -86,22 +83,8 @@ int
cli_input_init (struct cli_state *state)
{
int ret = 0;
- gf_boolean_t is_batch = _gf_false;
-
- if (1 < state->argc) {
- if (!strcmp ("mode", state->argv[0]) &&
- !strcmp ("script", state->argv[1])) {
- state->mode = GLUSTER_MODE_SCRIPT;
- if (2 < state->argc)
- is_batch = _gf_true;
- } else {
- is_batch = _gf_true;
- }
- } else if (1 == state->argc) {
- is_batch = _gf_true;
- }
- if (is_batch) {
+ if (state->argc) {
ret = pthread_create (&state->input, NULL, cli_batch, state);
return ret;
}