From e6abc96e133e238831634d925f6fd7c993729f24 Mon Sep 17 00:00:00 2001 From: Lubomir Rintel Date: Sun, 25 Feb 2018 14:26:20 +0100 Subject: [PATCH] cli/utils: make next_arg() recognize arguments that are not in "--option" form This is going to make it possible to parse and complete argument lists in one go: -nmc_complete_strings (*argv, "ifname", "bssid", NULL); -next_arg (nmc, &argc, &argv, NULL); -if (strcmp (*argv, "ifname") == 0) -... -else if (strcmp (*argv, "bssid") == 0) -... +option = next_arg (nmc, &argc, &argv, "ifname", "bssid", NULL) +switch (option) { +case 1: +... +case 2: +... Beautiful. --- clients/cli/utils.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/clients/cli/utils.c b/clients/cli/utils.c index 9cc01c7240..7d687c2c29 100644 --- a/clients/cli/utils.c +++ b/clients/cli/utils.c @@ -199,10 +199,18 @@ next_arg (NmCli *nmc, int *argc, char ***argv, ...) /* Check command dependent options first */ while ((cmd_option = va_arg (args, const char *))) { - /* strip heading "--" form cmd_option */ - if (nmc_arg_is_option (**argv, cmd_option + 2)) { - va_end (args); - return cmd_option_pos; + if (cmd_option[0] == '-' && cmd_option[1] == '-') { + /* Match as an option (leading "--" stripped) */ + if (nmc_arg_is_option (**argv, cmd_option + 2)) { + va_end (args); + return cmd_option_pos; + } + } else { + /* Match literally. */ + if (strcmp (**argv, cmd_option) == 0) { + va_end (args); + return cmd_option_pos; + } } cmd_option_pos++; }