fixes options parser to correctly report missing arguments

Sat, 12 May 2018 15:03:11 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 12 May 2018 15:03:11 +0200
changeset 375
39fd327f81da
parent 374
38ae05d46f9a
child 376
222997d48ae1

fixes options parser to correctly report missing arguments

dav/main.c file | annotate | diff | comparison | revisions
dav/optparser.c file | annotate | diff | comparison | revisions
--- a/dav/main.c	Sun Apr 29 11:48:12 2018 +0200
+++ b/dav/main.c	Sat May 12 15:03:11 2018 +0200
@@ -79,7 +79,6 @@
     CmdArgs *args = cmd_parse_args(argc - 2, argv + 2);
     if(!args) {
         print_usage(argv[0]);
-        cmd_args_free(args);
         return -1;
     }
     
--- a/dav/optparser.c	Sun Apr 29 11:48:12 2018 +0200
+++ b/dav/optparser.c	Sat May 12 15:03:11 2018 +0200
@@ -31,6 +31,7 @@
 #include <string.h>
 
 #include "optparser.h"
+#include "sopt.h"
 
 void cmd_args_free(CmdArgs *args) {
     if(args) {
@@ -55,6 +56,13 @@
         size_t len = strlen(arg);
         if(len > 1 && arg[0] == '-') {
             for(int c=1;c<len;c++) {
+                if (option) {
+                    fprintf(stderr,
+                            "Missing argument for option -%c\n\n", optchar);
+                    cmd_args_free(a);
+                    return NULL;
+                }
+                
                 switch(arg[c]) {
                     default: {
                         fprintf(stderr, "Unknown option -%c\n\n", arg[c]);
@@ -66,17 +74,8 @@
                         break;
                     }
                     case 'k': {
-                        if(!option) {
-                            option = "key";
-                            optchar = 'k';
-                        } else {
-                            fprintf(
-                                    stderr,
-                                    "Missing argument for option -%c\n",
-                                    optchar);
-                            cmd_args_free(a);
-                            return NULL;
-                        }
+                        option = "key";
+                        optchar = 'k';
                         break;
                     }
                     case 'p': {
@@ -141,87 +140,33 @@
                         break;
                     }
                     case 'o': {
-                        if(!option) {
-                            option = "output";
-                            optchar = 'o';
-                        } else {
-                            fprintf(
-                                    stderr,
-                                    "Missing argument for option -%c\n",
-                                    optchar);
-                            cmd_args_free(a);
-                            return NULL;
-                        }
+                        option = "output";
+                        optchar = 'o';
                         break;
                     }
                     case 'u': {
-                        if(!option) {
-                            option = "update";
-                            optchar = 'u';
-                        } else {
-                            fprintf(
-                                    stderr,
-                                    "Missing argument for option -%c\n",
-                                    optchar);
-                            cmd_args_free(a);
-                            return NULL;
-                        }
+                        option = "update";
+                        optchar = 'u';
                         break;
                     }
                     case 'n': {
-                        if(!option) {
-                            option = "namespace";
-                            optchar = 'n';
-                        } else {
-                            fprintf(
-                                    stderr,
-                                    "Missing argument for option -%c\n",
-                                    optchar);
-                            cmd_args_free(a);
-                            return NULL;
-                        }
+                        option = "namespace";
+                        optchar = 'n';
                         break;
                     }
                     case 'L': {
-                        if(!option) {
-                            option = "lock";
-                            optchar = 'L';
-                        } else {
-                            fprintf(
-                                    stderr,
-                                    "Missing argument for option -%c\n",
-                                    optchar);
-                            cmd_args_free(a);
-                            return NULL;
-                        }
+                        option = "lock";
+                        optchar = 'L';
                         break;
                     }
                     case 'T': {
-                        if(!option) {
-                            option = "timeout";
-                            optchar = 'T';
-                        } else {
-                            fprintf(
-                                    stderr,
-                                    "Missing argument for option -%c\n",
-                                    optchar);
-                            cmd_args_free(a);
-                            return NULL;
-                        }
+                        option = "timeout";
+                        optchar = 'T';
                         break;
                     }
                     case 'F': {
-                        if(!option) {
-                            option = "progressfile";
-                            optchar = 'F';
-                        } else {
-                            fprintf(
-                                    stderr,
-                                    "Missing argument for option -%c\n",
-                                    optchar);
-                            cmd_args_free(a);
-                            return NULL;
-                        }
+                        option = "progressfile";
+                        optchar = 'F';
                         break;
                     }
                 }
@@ -233,6 +178,12 @@
             a->argv[a->argc++] = arg;
         }
     }
+    if(option) {
+        fprintf(stderr,
+                "Missing argument for option -%c\n\n", optchar);
+        cmd_args_free(a);
+        return NULL;
+    }
     
     return a;
 }

mercurial