diff options
author | Beniamino Galvani <bgalvani@redhat.com> | 2015-06-01 16:45:25 +0200 |
---|---|---|
committer | Beniamino Galvani <bgalvani@redhat.com> | 2015-06-09 18:11:25 +0200 |
commit | 609f4f37c0db111e7f9e2f452e0d442b41aa0298 (patch) | |
tree | 2e167055f7cb026a3c550913955a36ea479f93f7 | |
parent | 6f647fe689ddc5102c7a4492740a96f043d4a478 (diff) | |
download | NetworkManager-609f4f37c0db111e7f9e2f452e0d442b41aa0298.tar.gz |
cli: add nmc_string_to_tristate()
-rw-r--r-- | clients/cli/utils.c | 30 | ||||
-rw-r--r-- | clients/cli/utils.h | 7 |
2 files changed, 37 insertions, 0 deletions
diff --git a/clients/cli/utils.c b/clients/cli/utils.c index 727e309d1c..ce4749bfd1 100644 --- a/clients/cli/utils.c +++ b/clients/cli/utils.c @@ -517,6 +517,36 @@ nmc_string_to_bool (const char *str, gboolean *val_bool, GError **error) return TRUE; } +gboolean +nmc_string_to_tristate (const char *str, NMCTriStateValue *val, GError **error) +{ + const char *s_true[] = { "true", "yes", "on", NULL }; + const char *s_false[] = { "false", "no", "off", NULL }; + const char *s_unknown[] = { "unknown", NULL }; + + g_return_val_if_fail (error == NULL || *error == NULL, FALSE); + + if (g_strcmp0 (str, "o") == 0) { + g_set_error (error, 1, 0, + _("'%s' is ambiguous (on x off)"), str); + return FALSE; + } + + if (nmc_string_is_valid (str, s_true, NULL)) + *val = NMC_TRI_STATE_YES; + else if (nmc_string_is_valid (str, s_false, NULL)) + *val = NMC_TRI_STATE_NO; + else if (nmc_string_is_valid (str, s_unknown, NULL)) + *val = NMC_TRI_STATE_UNKNOWN; + else { + g_set_error (error, 1, 0, + _("'%s' is not valid; use [%s], [%s] or [%s]"), + str, "true, yes, on", "false, no, off", "unknown"); + return FALSE; + } + return TRUE; +} + /* * Ask user for input and return the string. * The caller is responsible for freeing the returned string. diff --git a/clients/cli/utils.h b/clients/cli/utils.h index 6defb1de14..54f76b071a 100644 --- a/clients/cli/utils.h +++ b/clients/cli/utils.h @@ -32,6 +32,12 @@ typedef struct { gboolean found; } nmc_arg_t; +typedef enum { + NMC_TRI_STATE_NO, + NMC_TRI_STATE_YES, + NMC_TRI_STATE_UNKNOWN, +} NMCTriStateValue; + /* === Functions === */ int matches (const char *cmd, const char *pattern); int next_arg (int *argc, char ***argv); @@ -62,6 +68,7 @@ gboolean nmc_string_to_uint (const char *str, unsigned long int max, unsigned long int *value); gboolean nmc_string_to_bool (const char *str, gboolean *val_bool, GError **error); +gboolean nmc_string_to_tristate (const char *str, NMCTriStateValue *val, GError **error); char *nmc_ip4_address_as_string (guint32 ip, GError **error); char *nmc_ip6_address_as_string (const struct in6_addr *ip, GError **error); void nmc_terminal_erase_line (void); |