Welcome to little lamb

Code » limb » commit 09dd442

parseopt: Allow required arg for shortopt as next element

author Olivier Brunel
2023-04-14 12:00:11 UTC
committer Olivier Brunel
2023-05-20 18:06:36 UTC
parent 0702fd0027778e40a8278c2fad47f409c44fe5a0

parseopt: Allow required arg for shortopt as next element

src/doc/parseopt.h/parseopt.3.md +4 -3
src/liblimb/parseopt.h/parseopt.c +1 -3

diff --git a/src/doc/parseopt.h/parseopt.3.md b/src/doc/parseopt.h/parseopt.3.md
index 6cbfdbb..76dc32a 100644
--- a/src/doc/parseopt.h/parseopt.3.md
+++ b/src/doc/parseopt.h/parseopt.3.md
@@ -72,13 +72,14 @@ be recognized as long as there's no other match possible. (This behavior can be
 disabled, see [[FLAGS]] below.)
 
 When an argument is required, it can be specified within the same element,
-following a '\=', or as the next element. Optional arguments can only be
-specified after a '\=' within the same element.
+following a '\=' for long options, or as the next element. Optional arguments
+can only be specified within the same element.
 
 An element for short options can specify more than one option in a row, so long
 as they don't accept argument. When an option accepts an argument (whether
 optional or not), what follows next within the element will be treated as the
-option's argument.
+option's argument. (Note that it remains possible to specify a /required/
+argument as next element.)
 
 `options` must be a pointer to the first element of an array of *struct option*
 declared as such :
diff --git a/src/liblimb/parseopt.h/parseopt.c b/src/liblimb/parseopt.h/parseopt.c
index c922c54..958eb63 100644
--- a/src/liblimb/parseopt.h/parseopt.c
+++ b/src/liblimb/parseopt.h/parseopt.c
@@ -94,14 +94,12 @@ again:
 
     ctx->arg = NULL;
     if (options[ctx->idx].arg == ARG_REQ) {
-        if (arg_long) {
+        if (arg_long || !arg[1]) {
             ++ctx->cur;
             if (ctx->cur == argc)
                 return (errno = ENOMSG, -1);
             ctx->arg = argv[ctx->cur];
         } else {
-            if (!arg[1])
-                return (errno = ENOMSG, -1);
             ctx->arg = arg + 1;
         }
     } else if (options[ctx->idx].arg == ARG_OPT) {