cli: add nmc_string_to_tristate()

This commit is contained in:
Beniamino Galvani 2015-06-01 16:45:25 +02:00
parent 6f647fe689
commit 609f4f37c0
2 changed files with 37 additions and 0 deletions

View file

@ -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.

View file

@ -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);