]> mj.ucw.cz Git - libucw.git/commitdiff
Opt: fixed nothing-supplied detection
authorJan 'Moskyt' Matejka <mq@ucw.cz>
Mon, 13 May 2013 07:58:59 +0000 (09:58 +0200)
committerJan 'Moskyt' Matejka <mq@ucw.cz>
Mon, 13 May 2013 07:58:59 +0000 (09:58 +0200)
ucw/opt.c

index 858891a1aacdd396bbb157d1e71363b3d66a2884..ed84799393a663bdfa9802a7fb7aa24cf7eadd7e 100644 (file)
--- a/ucw/opt.c
+++ b/ucw/opt.c
@@ -313,6 +313,8 @@ static int opt_longopt(char ** argv, int index, struct opt_precomputed * pre) {
       value = name_in + pos + 1;
     else {
       value = argv[index+1];
+      if (!value)
+       opt_failure("Argument --%s must have a value but nothing supplied.", opt->name);
       eaten++;
     }
   }
@@ -322,7 +324,7 @@ static int opt_longopt(char ** argv, int index, struct opt_precomputed * pre) {
   }
   else {
     if (pos < strlen(name_in))
-      opt_failure("Argument %s must not have any value.", opt->name);
+      opt_failure("Argument --%s must not have any value.", opt->name);
   }
   opt_parse_value(opt, value, 1);
   return eaten;
@@ -336,29 +338,26 @@ static int opt_shortopt(char ** argv, int index, struct opt_precomputed * pre) {
       opt_parse_value(opt, NULL, 0);
       continue;
     }
-    if (chr == 1 && (opt->flags & OPT_REQUIRED_VALUE)) {
-      if (argv[index][2]) {
+    else if (opt->flags & OPT_REQUIRED_VALUE) {
+      if (chr == 1 && argv[index][2])
         opt_parse_value(opt, argv[index] + 2, 0);
-       return 0;
-      }
+      else if (argv[index][chr+1])
+       opt_failure("Option -%c must have a value but found inside a bunch of short opts.", opt->item->letter);
+      else if (!argv[index+1])
+       opt_failure("Option -%c must have a value but nothing supplied.", opt->item->letter);
       else {
        opt_parse_value(opt, argv[index+1], 0);
        return 1;
       }
     }
-    else if (chr == 1 && (opt->flags & OPT_MAYBE_VALUE)) {
-      if (argv[index][2])
+    else if (opt->flags & OPT_MAYBE_VALUE) {
+      if (chr == 1 && argv[index][2])
         opt_parse_value(opt, argv[index] + 2, 0);
       else
        opt_parse_value(opt, NULL, 0);
     }
-    else if (opt->flags & (OPT_REQUIRED_VALUE | OPT_MAYBE_VALUE)) {
-      if (argv[index][chr+1] || (opt->flags | OPT_MAYBE_VALUE))
-       opt_failure("Option -%c may or must have a value but found inside a bunch of short opts.", opt->item->letter);
-      else {
-       opt_parse_value(opt, argv[index+1], 0);
-       return 1;
-      }
+    else {
+      ASSERT(0);
     }
   }