From f909ef421bbfdeba4619490aa99bd22dba734ddc Mon Sep 17 00:00:00 2001 From: Lubomir Rintel Date: Fri, 24 Mar 2017 14:35:56 +0100 Subject: [PATCH] cli: check for global options before each agument Turn --ask and --show-secrets into global options. This is for user comfort, see: rh #1351263. --- clients/cli/nmcli.c | 8 ++------ clients/cli/utils.c | 31 ++++++++++++++++++++++++------- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/clients/cli/nmcli.c b/clients/cli/nmcli.c index a5c2359b9..ac8ae9a22 100644 --- a/clients/cli/nmcli.c +++ b/clients/cli/nmcli.c @@ -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) { diff --git a/clients/cli/utils.c b/clients/cli/utils.c index 08039e6f3..a6f18c5f0 100644 --- a/clients/cli/utils.c +++ b/clients/cli/utils.c @@ -28,6 +28,7 @@ #include #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