Welcome to little lamb

Code » limb » commit ba39be4

parseopt.h: Add OPTION_ARG_{NONE,OPT,REQ} macros

author Olivier Brunel
2023-04-05 08:49:33 UTC
committer Olivier Brunel
2023-05-20 18:06:35 UTC
parent 3172ea3b5a3a413bb8899ec54b2acc3acd84e5bd

parseopt.h: Add OPTION_ARG_{NONE,OPT,REQ} macros

src/doc/parseopt.h.0.md +10 -3
src/doc/parseopt.h/parseopt.3.md +17 -7
src/liblimb/include/limb/parseopt.h +10 -2

diff --git a/src/doc/parseopt.h.0.md b/src/doc/parseopt.h.0.md
index d0eca19..32914c6 100644
--- a/src/doc/parseopt.h.0.md
+++ b/src/doc/parseopt.h.0.md
@@ -20,16 +20,23 @@ The following constants are defined :
 : *ARG_NONE*, *ARG_REQ*, *ARG_OPT*
 :: To define if an options has no argument, requires one, or may have one.
 
+: *OPTION_ARG_NONE*, *OPTION_ARG_OPT*, *OPTION_ARG_REQ*
+:: To define an option taking no argument, an optional argument, or a required
+:: argument respectively.
+
 : *OPTION_DONE*
 :: To be used as last element in the array of *struct option*, indicating the
 :: end of said array.
 
-: *PARSEOPT_IS_LONG*, *PARSEOPT_STRICT*
-:: Flags that can be passed to [parseopt](3).
+: *OPTID_FIRST*
+:: First ID to be used for options.
 
-: *ID_SHORTOPT*
+: *OPTID_SHORTOPT*
 :: Special ID for an option, meaning to return its `shortopt` instead.
 
+: *PARSEOPT_IS_LONG*, *PARSEOPT_STRICT*
+:: Flags that can be passed to [parseopt](3).
+
 ## Structures
 
 The following structure are defined :
diff --git a/src/doc/parseopt.h/parseopt.3.md b/src/doc/parseopt.h/parseopt.3.md
index 620ca56..8f2b916 100644
--- a/src/doc/parseopt.h/parseopt.3.md
+++ b/src/doc/parseopt.h/parseopt.3.md
@@ -111,11 +111,21 @@ The meanings of different members are :
 
 : `id`
 :: An integer value unique to this option, to recognize when it is parsed. It
-:: must be a value >= 128 (0x80), or 0 (*ID_SHORTOPT*) in which case `shortopt`
-:: will be returned instead.
+:: must be a value >= 128 (*OPTID_FIRST*), or 0 (*OPTID_SHORTOPT*) in which
+:: case `shortopt` will be returned instead.
 :: ! INFO:
-:: ! Other values (between 1 and 32) are reserved for internal use by
-:: ! [loadopt](3) and its handling of options.
+:: ! Other values (between 1 and 32) are reserved for internal use.
+
+To define options you can use the following convenience macros :
+
+: *OPTION_ARG_NONE*(`shortopt`, `longopt`, `flags`, `id`)
+:: To define an option taking no arguments
+
+: *OPTION_ARG_OPT*(`shortopt`, `longopt`, `flags`, `id`)
+:: To define an option accepting an optional argument
+
+: *OPTION_ARG_REQ*(`shortopt`, `longopt`, `flags`, `id`)
+:: To define an option requiring an argument
 
 The last element of the array must be set to *OPTION_DONE* (all members set to 0
 except `flags` set to 1) to indicate the end.
@@ -135,9 +145,9 @@ constructed as a bitwise-inclusive OR of flags from the following list :
 
 # RETURN VALUE
 
-If an option was successfully found, `parseopt`() returns the option's `id` if
-non-zero, else its `shortopt`. When all command-line options have been parsed it
-returns 0.
+If an option was successfully found, `parseopt`() returns the option's `id`
+unless it is *OPTID_SHORTOPT*, in which case its `shortopt` is returned instead.
+When all command-line options have been parsed it returns 0.
 
 If an error occurs, it returns -1 and sets `errno` to indicate the error.
 
diff --git a/src/liblimb/include/limb/parseopt.h b/src/liblimb/include/limb/parseopt.h
index 31cbce9..b3f31bf 100644
--- a/src/liblimb/include/limb/parseopt.h
+++ b/src/liblimb/include/limb/parseopt.h
@@ -12,11 +12,19 @@ enum {
     ARG_OPT         /* argument optional */
 };
 
+/* to define options with no arg, optinal arg, required arg */
+#define OPTION_ARG_NONE(s,l,f,i)    { s, l, ARG_NONE, f, i }
+#define OPTION_ARG_OPT(s,l,f,i)     { s, l, ARG_OPT,  f, i }
+#define OPTION_ARG_REQ(s,l,f,i)     { s, l, ARG_REQ,  f, i }
+
 /* last element in struct option[] to indicate the end */
-#define OPTION_DONE             { .flags = 1 }
+#define OPTION_DONE                 { .flags = 1 }
 
 /* special ID meaning to return shortopt instead */
-#define ID_SHORTOPT             0
+#define OPTID_SHORTOPT              0
+
+/* first value for user option.id */
+#define OPTID_FIRST                 127
 
 struct option {
     const char  shortopt;