cli: check for global options before each agument

Turn --ask and --show-secrets into global options. This is for user
comfort, see: rh #1351263.
This commit is contained in:
Lubomir Rintel
2017-03-24 14:35:56 +01:00
parent 5a71bc8022
commit f909ef421b
2 changed files with 26 additions and 13 deletions

View File

@@ -231,8 +231,8 @@ process_command_line (NmCli *nmc, int argc, char **argv)
if (argc == 1 && nmc->complete) {
nmc_complete_strings (opt, "--terse", "--pretty", "--mode", "--colors", "--escape",
"--fields", "--nocheck", "--ask", "--show-secrets",
"--get-values", "--wait", "--version", "--help", NULL);
"--fields", "--nocheck", "--get-values",
"--wait", "--version", "--help", NULL);
}
if (opt[1] == '-') {
@@ -360,10 +360,6 @@ process_command_line (NmCli *nmc, int argc, char **argv)
nmc->mode_specified = TRUE;
} else if (matches (opt, "-nocheck")) {
/* ignore for backward compatibility */
} else if (matches (opt, "-ask")) {
nmc->ask = TRUE;
} else if (matches (opt, "-show-secrets")) {
nmc->show_secrets = TRUE;
} else if (matches (opt, "-wait")) {
unsigned long timeout;
if (!argc) {

View File

@@ -28,6 +28,7 @@
#include <arpa/inet.h>
#include "utils.h"
#include "common.h"
gboolean
matches (const char *cmd, const char *pattern)
@@ -38,17 +39,34 @@ matches (const char *cmd, const char *pattern)
return memcmp (pattern, cmd, len) == 0;
}
static gboolean
parse_global_arg (NmCli *nmc, const char *arg)
{
if (nmc_arg_is_option (arg, "ask"))
nmc->ask = TRUE;
else if (nmc_arg_is_option (arg, "show-secrets"))
nmc->show_secrets = TRUE;
else
return FALSE;
return TRUE;
}
int
next_arg (NmCli *nmc, int *argc, char ***argv)
{
int arg_num = *argc;
if (arg_num > 0) {
(*argc)--;
(*argv)++;
}
if (arg_num <= 1)
return -1;
do {
if (arg_num > 0) {
(*argc)--;
(*argv)++;
}
if (nmc && nmc->complete && *argc == 1 && ***argv == '-')
nmc_complete_strings (**argv, "--ask", "--show-secrets", NULL);
if (arg_num <= 1)
return -1;
} while (nmc && parse_global_arg (nmc, **argv));
return 0;
}
@@ -82,7 +100,6 @@ nmc_arg_is_option (const char *str, const char *opt_name)
return (*p ? matches (p, opt_name) : FALSE);
}
/*
* Helper function to parse command-line arguments.
* arg_arr: description of arguments to look for