From d3ffe15f17daa65446b3b628a9f481f01dadd35d Mon Sep 17 00:00:00 2001 From: Lubomir Rintel Date: Tue, 14 Feb 2017 16:38:16 +0100 Subject: cli: add nmcli g logging completion This is sort of ugly, because it includes the domain and log levels verbatim. They're just plain strings on the API, there's no way the client would know which ones are valid. On the other hand this kills one of two uses of nmc_parse_args(), which probably means it's not a very good abstraction and maybe we should get rid of it altogether. It is in particular unfriendly to argument completion. --- clients/cli/general.c | 61 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 52 insertions(+), 9 deletions(-) diff --git a/clients/cli/general.c b/clients/cli/general.c index 512ec29193..70a40f1cae 100644 --- a/clients/cli/general.c +++ b/clients/cli/general.c @@ -632,6 +632,23 @@ show_general_logging (NmCli *nmc) return TRUE; } +static void +nmc_complete_strings_nocase (const char *prefix, ...) +{ + va_list args; + const char *candidate; + int len; + + len = strlen (prefix); + + va_start (args, prefix); + while ((candidate = va_arg (args, const char *))) { + if (strncasecmp (prefix, candidate, len) == 0) + g_print ("%s\n", candidate); + } + va_end (args); +} + static NMCResultCode do_general_logging (NmCli *nmc, int argc, char **argv) { @@ -652,19 +669,45 @@ do_general_logging (NmCli *nmc, int argc, char **argv) /* arguments provided -> set logging level and domains */ const char *level = NULL; const char *domains = NULL; - nmc_arg_t exp_args[] = { {"level", TRUE, &level, TRUE}, - {"domains", TRUE, &domains, TRUE}, - {NULL} }; - /* TODO: nmc_parse_args needs completion */ + do { + if (argc == 1 && nmc->complete) + nmc_complete_strings (*argv, "level", "domains", NULL); + + if (matches (*argv, "level") == 0) { + if (next_arg (&argc, &argv) != 0) { + g_string_printf (nmc->return_text, _("Error: '%s' argument is missing."), *(argv-1)); + return NMC_RESULT_ERROR_USER_INPUT; + } + if (argc == 1 && nmc->complete) { + nmc_complete_strings_nocase (*argv, "TRACE", "DEBUG", "INFO", "WARN", + "ERR", "OFF", "KEEP", NULL); + } + level = *argv; + } else if (matches (*argv, "domains") == 0) { + if (next_arg (&argc, &argv) != 0) { + g_string_printf (nmc->return_text, _("Error: '%s' argument is missing."), *(argv-1)); + return NMC_RESULT_ERROR_USER_INPUT; + } + if (argc == 1 && nmc->complete) { + nmc_complete_strings_nocase (*argv, "PLATFORM", "RFKILL", "ETHER", "WIFI", "BT", + "MB", "DHCP4", "DHCP6", "PPP", "WIFI_SCAN", "IP4", + "IP6", "AUTOIP4", "DNS", "VPN", "SHARING", "SUPPLICANT", + "AGENTS", "SETTINGS", "SUSPEND", "CORE", "DEVICE", "OLPC", + "INFINIBAND", "FIREWALL", "ADSL", "BOND", "VLAN", "BRIDGE", + "DBUS_PROPS", "TEAM", "CONCHECK", "DCB", "DISPATCH", "AUDIT", + "SYSTEMD", "VPN_PLUGIN", "PROXY", NULL); + } + domains = *argv; + } else { + g_string_printf (nmc->return_text, _("Error: property '%s' is not known."), *argv); + return NMC_RESULT_ERROR_USER_INPUT; + } + } while (next_arg (&argc, &argv) == 0); + if (nmc->complete) return nmc->return_value; - if (!nmc_parse_args (exp_args, FALSE, &argc, &argv, &error)) { - g_string_assign (nmc->return_text, error->message); - return error->code; - } - nm_client_set_logging (nmc->client, level, domains, &error); if (error) { g_string_printf (nmc->return_text, _("Error: failed to set logging: %s"), -- cgit v1.2.1