[Libguestfs] [nbdkit PATCH v2 4/5] main: Use new bool parser for --tls

Eric Blake eblake at redhat.com
Thu Nov 8 21:29:51 UTC 2018


Our use of --tls is a superset of boolean because we add a
force mode; but once we have checked for that, it's more
consistent if we accept the same set of other boolean values
as anything else (in this case, adding 'yes/no' and 'true/false'),
as well as consistently allowing case-insensitivity.  The error
message for an unrecognized spelling changes from:

$ nbdkit --tls huh
nbdkit: --tls flag must be off|on|require

to:

$ nbdkit --tls huh
nbdkit: error: could not decipher boolean (huh)

but I don't think that hurts too much.

Signed-off-by: Eric Blake <eblake at redhat.com>
---

 src/main.c | 16 ++++++----------
 1 file changed, 6 insertions(+), 10 deletions(-)

diff --git a/src/main.c b/src/main.c
index 7ebbba6..0a883e1 100644
--- a/src/main.c
+++ b/src/main.c
@@ -376,18 +376,14 @@ main (int argc, char *argv[])

     case TLS_OPTION:
       tls_set_on_cli = 1;
-      if (strcmp (optarg, "off") == 0 || strcmp (optarg, "0") == 0)
-        tls = 0;
-      else if (strcmp (optarg, "on") == 0 || strcmp (optarg, "1") == 0)
-        tls = 1;
-      else if (strcmp (optarg, "require") == 0 ||
-               strcmp (optarg, "required") == 0 ||
-               strcmp (optarg, "force") == 0)
+      if (strcasecmp (optarg, "require") == 0 ||
+          strcasecmp (optarg, "required") == 0 ||
+          strcasecmp (optarg, "force") == 0)
         tls = 2;
       else {
-        fprintf (stderr, "%s: --tls flag must be off|on|require\n",
-                 program_name);
-        exit (EXIT_FAILURE);
+        tls = nbdkit_parse_bool (optarg);
+        if (tls < 0)
+          exit (EXIT_FAILURE);
       }
       break;

-- 
2.17.2




More information about the Libguestfs mailing list