diff --git a/cli/src/utils.c b/cli/src/utils.c index d7b37e2ee..102b5ecce 100644 --- a/cli/src/utils.c +++ b/cli/src/utils.c @@ -327,6 +327,27 @@ nmc_string_to_uint (const char *str, return nmc_string_to_uint_base (str, 10, range_check, min, max, value); } +gboolean +nmc_string_to_bool (const char *str, gboolean *val_bool, GError **error) +{ + const char *s_true[] = { "true", "yes", "on", NULL }; + const char *s_false[] = { "false", "no", "off", NULL }; + + g_return_val_if_fail (error == NULL || *error == NULL, FALSE); + + if (nmc_string_is_valid (str, s_true, NULL)) + *val_bool = TRUE; + else if (nmc_string_is_valid (str, s_false, NULL)) + *val_bool = FALSE; + else { + g_set_error (error, 1, 0, + _("'%s' is not valid; use [%s] or [%s]"), + str, "true, yes, on", "false, no, off"); + return FALSE; + } + return TRUE; +} + /* * Ask user for input and return the string. * The caller is responsible for freeing the returned string. diff --git a/cli/src/utils.h b/cli/src/utils.h index 704b06737..cc1b126aa 100644 --- a/cli/src/utils.h +++ b/cli/src/utils.h @@ -62,6 +62,7 @@ gboolean nmc_string_to_uint (const char *str, unsigned long int min, unsigned long int max, unsigned long int *value); +gboolean nmc_string_to_bool (const char *str, gboolean *val_bool, 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);