author | Olivier Brunel
<jjk@jjacky.com> 2023-04-22 07:15:15 UTC |
committer | Olivier Brunel
<jjk@jjacky.com> 2023-05-20 18:06:37 UTC |
parent | 43089b94f373ffa4769ffdbcc026656f2e043c4c |
src/doc/parseopt.h.0.md | +17 | -1 |
src/doc/parseopt.h/parseopt.3.md | +14 | -17 |
src/liblimb/include/limb/parseopt.h | +10 | -5 |
diff --git a/src/doc/parseopt.h.0.md b/src/doc/parseopt.h.0.md index 4aafe68..977ccaf 100644 --- a/src/doc/parseopt.h.0.md +++ b/src/doc/parseopt.h.0.md @@ -45,7 +45,23 @@ The following structure are defined : :: To define an option when calling [parseopt](3). : *struct parseopt* -:: A semi-opaque structure to be passed to [parseopt](3) +:: An opaque structure to be passed to [parseopt](3) + +## Macros + +The following macros are defined : + +: *PARSEOPT_INIT* +:: Value to initialize a *struct parseopt*. + +: *PO_ARG(`ctx`)* +:: To get a pointer to the current option's argument. + +: *PO_CUR(`ctx`)* +:: To get the index of the first argument in argv. + +: *PO_IDX(`ctx`)* +:: To get the index of the current option. ## Functions diff --git a/src/doc/parseopt.h/parseopt.3.md b/src/doc/parseopt.h/parseopt.3.md index d74c579..82763de 100644 --- a/src/doc/parseopt.h/parseopt.3.md +++ b/src/doc/parseopt.h/parseopt.3.md @@ -12,6 +12,10 @@ parseopt - parse command-line options ```pre hl int parseopt(int <em>argc</em>, const char **<em>argv</em>, const struct option *<em>options</em>, unsigned int <em>flags</em>, struct parseopt *<em>ctx</em>) + +const char *PO_ARG(struct parseopt *<em>ctx</em>) +u16 PO_CUR(struct parseopt *<em>ctx</em>) +int PO_IDX(struct parseopt *<em>ctx</em>) ``` # DESCRIPTION @@ -25,28 +29,21 @@ all possible options. See [[Options]] below for more. The `flags` argument allows to enable certain options, see [[FLAGS]] below. -The last argument `ctx` is a semi-opaque structure, that should be initialized -to all zeroes, defined as such : - - struct parseopt { - u16 cur; - u16 off; - int idx; - const char *arg; - }; +The last argument `ctx` is pointer to an opaque structure, that should be +initialized to *PARSEOPT_INIT*. When `parseopt`() returns a positive value, i.e. an option was successfully -found, its member `arg` points to the option's argument if any, else it is -NULL. It is similar to the global `optarg` from [getopt](3). +found, the `PO_ARG`() macro will return a pointer to the option's argument if +any (NULL otherwise). It is similar to the global `optarg` from [getopt](3). -Additionally its member `idx` is set to the index of said option within -`options`. When an error occurs, it can either be set to -1 or, in the case of -abbreviated long options (and more than one option did match), the +Additionally, the `PO_IDX`() macro will return the index of said option within +the `options` array. When an error occurs, it can either be -1 or, in the case +of abbreviated long options (and more than one option did match), the first-matching option. -When `parseopt`() returns 0, its member `cur` is the index in `argv` of the -first non-option element. In that way, it is similar to the global `optind` from -[getopt](3). +When `parseopt`() returns 0, the `PO_CUR`() macro will return the index in +`argv` of the first non-option element. In that way, it is similar to the global +`optind` from [getopt](3). ## Parsing diff --git a/src/liblimb/include/limb/parseopt.h b/src/liblimb/include/limb/parseopt.h index 9dedd93..d346da7 100644 --- a/src/liblimb/include/limb/parseopt.h +++ b/src/liblimb/include/limb/parseopt.h @@ -42,15 +42,20 @@ enum { PARSEOPT_SILENT = 1 << 2, }; +/* opaque structure -- use macros below to access its public members */ struct parseopt { - /* private */ - u16 cur; /* public when done : index of first argument in argv */ - u16 off; - /* public (read-only) */ - int idx; const char *arg; + int idx; + u16 cur; + u16 off; }; +#define PARSEOPT_INIT { NULL, 0, 0, 0 } + +#define PO_ARG(ctx) (ctx)->arg +#define PO_IDX(ctx) (ctx)->idx +#define PO_CUR(ctx) (ctx)->cur + extern int parseopt(int argc, const char **argv, const struct option *options, unsigned int flags, struct parseopt *ctx);