Welcome to little lamb

Code » limb » commit 5c24e80

loadopt.h: Add ARGUMENT_{OPT,REQ} macros

author Olivier Brunel
2023-04-05 08:53:13 UTC
committer Olivier Brunel
2023-05-20 18:06:35 UTC
parent ba39be4a2dce10750ef22a1a8d8f6c227497c4c8

loadopt.h: Add ARGUMENT_{OPT,REQ} macros

src/doc/loadopt.h.0.md +5 -1
src/doc/loadopt.h/loadopt.3.md +17 -7
src/liblimb/include/limb/loadopt.h +10 -3

diff --git a/src/doc/loadopt.h.0.md b/src/doc/loadopt.h.0.md
index 3b7bcbb..45d6378 100644
--- a/src/doc/loadopt.h.0.md
+++ b/src/doc/loadopt.h.0.md
@@ -29,7 +29,11 @@ The following constants are defined :
 The following macros are defined :
 
 : *LOADOPT_ARGUMENTS*, *LOADOPT_DONE_OPEN*, *LOADOPT_DONE*
-:: To be used an element in a *struct option* array
+:: To be used as element in a *struct option* array
+
+: *ARGUMENT_REQ*, *ARGUMENT_OPT*
+:: To define required or optional argument in a *struct option* array to be
+:: checked by [loadopt](3)
 
 ## Structures
 
diff --git a/src/doc/loadopt.h/loadopt.3.md b/src/doc/loadopt.h/loadopt.3.md
index 25ba818..2e8ede5 100644
--- a/src/doc/loadopt.h/loadopt.3.md
+++ b/src/doc/loadopt.h/loadopt.3.md
@@ -53,8 +53,8 @@ Additionally, the following members are of interest :
 
 When called, `loadopt`() will first defer to [parseopt](3) to handle parsing of
 command-line arguments. Once done, if `file` was specified (i.e. is not NULL)
-then the corresponding file will be read and parsed as an INI-like configuration
-file.
+and the corresponding file exists, it will be read and parsed as an INI-like
+configuration file.
 
 That is, it expects to find on every line a long option name (without the "--"),
 optionally followed by an argument. No escaping of any kind is supported.
@@ -90,17 +90,26 @@ which includes making sure all options marked required were indeed found
 For this to happen, you need to specify a special element in the array `options`
 by the macro *LOADOPT_ARGUMENTS*. Every element /after/ it will be handled as
 referring to an argument instead of an option.
-Note that this can be the first element of the array, if there are no options.
+Note that *LOADOPT_ARGUMENTS* can be the first element of the array, if there
+are no options.
 
-For those elements, members `shortopt`, `id` and `flags` are ignored. The
-`longopt` member is only used in warnings to the user, to refer to said
-argument.
+For elements referring to arguments, members `shortopt`, `id` and `flags` are
+ignored. The `longopt` member is only used in warnings to the user, to refer to
+said argument.
 
 The important member is `arg` which is treated as defining whether the
 argument is required (*ARG_REQ*) or optional (*ARG_OPT*). If required and no
 argument was given on command-line, an error occurs. If optional and not
 specified, `loadopt`() successfully ends, returning 0.
 
+To define them, you can use the following convenience macros :
+
+: *ARGUMENT_OPT*(`name`)
+:: To define argument `arg` as optional
+
+: *ARGUMENT_REQ*(`name`)
+: To define argument `arg` as required
+
 Finally, after all such argument definitions in the array `options`, you must
 terminate it with either one of the macros *LOADOPT_DONE_OPEN* and
 *LOADOPT_DONE*
@@ -110,7 +119,8 @@ more arguments were specified on command-line or not. With the later however,
 any more arguments will result in an error for "too many arguments".
 
 Note that either of those can be specified even without any actual arguments,
-i.e. as last element right after all the options.
+i.e. as last element right after all the options, and must be specified as last
+element.
 
 In other words, *LOADOPT_DONE* is the same as *OPTION_DONE* as described from
 [parseopt](3).
diff --git a/src/liblimb/include/limb/loadopt.h b/src/liblimb/include/limb/loadopt.h
index 035cd87..d484333 100644
--- a/src/liblimb/include/limb/loadopt.h
+++ b/src/liblimb/include/limb/loadopt.h
@@ -17,9 +17,16 @@ enum {
     OPT_REQ     = 1 << 3,   /* must be specified */
 };
 
-#define LOADOPT_ARGUMENTS       { 0, 0, ARG_REQ,  OPT_DONE, 0 }
-#define LOADOPT_DONE_OPEN       { 0, 0, ARG_OPT,  OPT_DONE, 0 }
-#define LOADOPT_DONE            { 0, 0, ARG_NONE, OPT_DONE, 0 }
+/* end of options, on to arguments */
+#define LOADOPT_ARGUMENTS   { 0, 0, ARG_REQ,  OPT_DONE, 0 }
+
+/* to define required argument, optional argument */
+#define ARGUMENT_REQ(a)     { 0, a, ARG_REQ, 0, 0 }
+#define ARGUMENT_OPT(a)     { 0, a, ARG_OPT, 0, 0 }
+
+/* eof of arguments, either no more arguments or there can be more */
+#define LOADOPT_DONE        { 0, 0, ARG_NONE, OPT_DONE, 0 }
+#define LOADOPT_DONE_OPEN   { 0, 0, ARG_OPT,  OPT_DONE, 0 }
 
 struct loadopt {
     /* struct parseopt */