nmcli: allow cmd specific option parsing in next_arg() func

Options dependant on specific commands (e.g., nmcli connection show
--active) are now allowed to be processed by the next_arg() function.
This would allow autocompletion to expand options belonging to specific
command first, and then global ones.

Note that global options ("--ask" and "--show-secrets") will be auto-completed
everywhere but only if at least a '-' is passed. Command specific ones
(--temporary, --active, --order) will be auto-completed only after the command
they belongs to but without requiring the user to pass a heading '-'.

Example:
'nmcli connection show -a'
will expand '-a' into '--active', but
'nmcli connection add -a`
will expand '-a' into '--ask' (as it is a global option)

This commit fixes also autocompletion for:
nmcli connection modify --temporary

(cherry picked from commit 6a3d77fbe6)
This commit is contained in:
Francesco Giudici
2017-03-30 16:09:46 +02:00
parent 77e1304f73
commit 72d5be1789
8 changed files with 189 additions and 79 deletions

View File

@@ -51,21 +51,66 @@ parse_global_arg (NmCli *nmc, const char *arg)
return TRUE;
}
/**
* next_arg:
* @nmc: NmCli data
* @*argc: pointer to left number of arguments to parse
* @***argv: pointer to const char *array of arguments still to parse
* @...: a %NULL terminated list of cmd options to match (e.g., "--active")
*
* Takes care of autocompleting options when needed and performs
* match against passed options while moving forward the pointer
* to the remaining arguments.
*
* Returns: the number of the matched option if a match is found against
* one of the custom options passed; 0 if no custom option matched and still
* some args need to be processed or autocompletion has been performed;
* -1 otherwise (no more args).
*/
int
next_arg (NmCli *nmc, int *argc, char ***argv)
next_arg (NmCli *nmc, int *argc, char ***argv, ...)
{
int arg_num = *argc;
va_list args;
const char *cmd_option;
g_assert (*argc >= 0);
do {
if (arg_num > 0) {
int cmd_option_pos = 1;
if (*argc > 0) {
(*argc)--;
(*argv)++;
}
if (nmc && nmc->complete && *argc == 1 && ***argv == '-')
nmc_complete_strings (**argv, "--ask", "--show-secrets", NULL);
if (arg_num <= 1)
if (*argc == 0)
return -1;
va_start (args, argv);
if (nmc && nmc->complete && *argc == 1) {
while ((cmd_option = va_arg (args, const char *)))
nmc_complete_strings (**argv, cmd_option, NULL);
if (***argv == '-')
nmc_complete_strings (**argv, "--ask", "--show-secrets", NULL);
va_end (args);
return 0;
}
/* 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;
}
cmd_option_pos++;
}
va_end (args);
} while (nmc && parse_global_arg (nmc, **argv));
return 0;
@@ -170,7 +215,7 @@ nmc_parse_args (nmc_arg_t *arg_arr, gboolean last, int *argc, char ***argv, GErr
return FALSE;
}
next_arg (NULL, argc, argv);
next_arg (NULL, argc, argv, NULL);
}
return TRUE;