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.
This commit is contained in:
Lubomir Rintel 2018-02-25 14:26:20 +01:00
parent c00e17578f
commit e6abc96e13

View file

@ -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++;
}