5 -----------------------------------------
6 An advanced command line options manager.
7 -----------------------------------------
9 :Author: Pierre Gentile p.gen.progs@gmail.com
17 **ctxopt** provides an advanced way to parse and evaluate complex command
18 line parameters and arguments.
20 Only two files **ctxopt.h** and **ctxopt.c** are needed to use **ctxopt**
26 The command line can be considered as a tree of contexts.
27 This offers the possibility of having a hierarchy of option groups.
29 Each option has a set of parameters which must start with a single dash
30 and may appear in the command line.
32 In order not to change habits, the double dash notation popularized by
33 the GNU **getopt_long** function for long options is also accepted and
34 automatically and silently converted to notation with a single dash.
36 These parameters are not limited to a dash followed by single letter.
37 In what follows, I will consider for simplicity that these are the options
38 which are visible in the command line and not one of their parameters.
40 Unless explicitly stated, the options are processed in the same order
41 as they appear in the command line, from left to right.
43 Any option may have the ability to modify the current context.
44 If such an option is seen in the command line, then the next option will
45 be looked for in this new context.
47 Initially the default context is the first created one in the program.
49 Exiting a context can be explicit, see below, or implicit if the current
50 option is not supported in this context.
51 The previous context in the hierarchy then becomes the current context
52 if it is not already the highest one.
54 An option which is unknown in the current context will be automatically
55 reevaluated in the previous one in the hierarchy.
56 If there is no such context, then a fatal error occurs.
58 A context can be explicitly delimited by a pair of symbols ``{`` and
60 The symbol ``{`` car appear just before or after the option responsible
61 of the context change.
63 The symbol ``}`` forces the end of a context.
65 The presence of ``{`` and ``}`` is purely optional and rarely necessary
66 but can help readability.
71 The API consists in the following functions:
73 * **void ctxopt_init(char *** \
79 This function initializes the internal structures uses by **ctxopt**.
80 It is mandatory and must me called first.
82 Its first argument (*prog_name*) is typically the content of ``argv[0]``
83 which nearly always contains the name of the program.
85 *prog_name* is used by the error system.
87 Its second argument (*flags*) will be read as a set of parameters
88 using the syntax ``flag=value``, each flag being separated from
89 its neighbor by spaces.
91 Flags may be missing in this argument, in this case the missing flags
92 will be given their default value which is given below.
93 An empty string is of course allowed but must be given anyway.
95 For now, only two flags are understood: **stop_if_non_option** and
96 **allow_abbreviations**.
98 Their value can be **yes** or **no**, **1** and **0** and also accepted.
101 Instructs **ctxopt** to stop processing arguments as soon as it
102 encounters a non-option word in the command line, even if other
103 parameters remain to be processed. The default value of this flag
107 Tells **ctxopt** to try to guess a parameter name even if only its
108 beginning is given. The default value of this flag is **1** or
111 Example of content of the *flags* parameter:
115 "stop_if_non_option=1 allow_abbreviations=No"
119 * **void ctxopt_new_ctx(char *** \
125 The next thing to do after having called **ctxopt_new_ctx** is to
126 define the contexts and its associated options.
128 Using this function, you can name the new context and its authorized
129 options and determine their main characteristics with a syntax easy
131 If they already exist in another context, then they must have the same
132 signature (described below) as in their previous appearance.
134 *name* is the name of the new context and *opts_specs* must contain a
135 description of the options allowed in this context.
137 *opts_specs* is a string containing various components separated by
138 spaces according to the syntax below:
141 A mandatory option named opt without parameter usable only one type in
145 Two mandatory option named **opt1** and **opt2** without argument.
148 Same as above but can appear multiple time in the context.
151 Same as above but the option is optional
154 One mandatory and one optional option named **opt1** and **opt2**
155 without argument. **opt2** can appear multiple times in the context.
158 One mandatory option named **opt*** taking one mandatory argument.
161 One mandatory option named **opt** taking one mandatory argument.
162 **tag** is ignored but can be used to improve the readability.
165 One mandatory option named **opt** taking one optional argument.
168 One mandatory option named **opt** taking one or more mandatory
171 opt>ctx... [#<value>]
172 The mandatory multiple option named **opt** will switch to the
173 context named **cxt** which will become the new current context.
175 It takes an optional argument with a tag named **<value>**.
178 One optional option named **opt** taking multiple optional
181 The number of options/arguments allowed can be restricted by adding
182 an operator and an integer just after the dots like in the following
186 Here, the mandatory option **opt** is restricted to only appear
187 one or two times in the context.
188 The number of its mandatory arguments must be exactly three.
190 Valid operators are **<**, **=** and **>**.
192 The multiplicity or not of the options and argument, their mandatory or
193 optional characteristics constitutes their signatures.s
195 As said above, an option can appear in more than one context but must
196 have the same signature.
202 ctxopt_new_ctx("context1",
203 "[opt1>context2...] #arg1... [opt3]");
205 ctxopt_new_ctx("context2",
206 "[opt2 [#arg2]] [opt3]");
208 In the previous example, three options **opt1**, **opt2** and **opt3**
212 is mandatory and can appear more than one time and take multiple
216 is optional and take an optional argument.
219 is optional and take no argument.
220 Note that **opt3** is legal in both contexts.
224 **opt2**, if present in the command line, will be evaluated in the
225 context **context2**.
226 Note that, in this example, the **context2** can only be entered if
227 **opt1** has previously been seen in the command line.
228 Hence, **opt2** is only legal if **opt1** is present first.
230 **opt3** does not have this limitation.
231 In fact, as **opt3** is optional in **context2** and if its action
232 function is not interested in the name of the current context,
233 then it could have been omitted from the second setting thanks to
234 the backtracking: an option which is illegal in a context is retried
235 in the previous context in the hierarchy.
239 * **void ctxopt_ctx_disp_usage(char *** \
241 **, usage_behaviour** \
245 This function builds and prints an usage help text for the
246 specific context *ctx_name*.
247 The symbols used in this text are the same as those used when defining
248 options in **ctxopt_new_ctx**.
250 The parameter *action* can take the following values:
253 The program is not stopped when this function returns.
256 The program is stopped with a non zero return code (typically 1)
257 when this function returns.
259 The usage text is followed by a legend explaining the symbols meanings.
260 This function is useful when associated with a **help** or **usage**
265 * **void ctxopt_disp_usage(usage_behaviour** \
269 This function is similar to the preceding one but displays the usage
270 help text for all the defined contexts.
271 It is useful when associated with a general **help** or **usage**
274 The parameter *action* can take the following values:
277 The program is not stopped when this function returns.
280 The program is stopped with a non zero return code (typically 1)
281 when this function returns.
285 * **void ctxopt_add_global_settings(settings** \
291 This function allows to set general **ctxopt** settings.
292 As for now, the only possible setting for *s* is **error_functions**.
294 This setting tells **ctxopt_add_global_settings** to use the rest of
295 its arguments in order to replace the built-in error functions with
298 When the value of the first parameter is **error_functions**,
299 then the second one must be one of the following constants:
302 A mandatory parameter is missing.
305 A given parameter is unknown in the current context.
308 An option has been seen more than once but has not been declared as
309 multiple in the context.
312 An option is incompatible with an option already given in the context.
315 A mandatory argument is missing.
317 :CTXOPTCNTEOPT, CTXOPTCNTLOPT and CTXOPTCNTGOPT:
318 The number of occurrences is not equal, lower or greater than a
321 :CTXOPTCNTEARG, CTXOPTCNTLARG and CTXOPTCNTGARG:
322 The number of arguments of an option is not equal, lower or greater
325 and the third parameter is a function pointer with the following
330 void (*) (errors err, state_t * state);
332 *state* will point to the publicly available analysis state structure.
333 This structure contains a snapshot of variables related to the command
334 line analysis so far.
335 They and can be used to give the user clues about errors.
337 This structure available in **ctxopt.h** is:
343 char * prog_name; /* base name of the program name. */
344 char * ctx_name; /* current context name. */
345 char * ctx_par_name; /* parameter which led to this context. */
346 char * opt_name; /* current option name. */
347 char * opt_params; /* all parameters of the current option. */
348 int opts_count; /* limit of the number of occurrences of *
349 | the current option. */
350 int opt_args_count; /* limit of the number of parameters of *
351 | the current option. */
352 char * pre_opt_par_name; /* parameter just before the current one. */
353 char * cur_opt_par_name; /* current parameter. */
356 All these pointers can be equal to the **NULL** pointer.
362 ctxopt_add_global_settings(error_functions, CTXOPTMISPAR, error);
366 * **void ctxopt_add_ctx_settings(settings** \
372 This function manages some settings for a given context.
373 Its first parameter *s* determines the setting and the signification
374 of the remaining arguments.
376 Its possible values are:
379 This setting allows to declare a set of options incompatible with
382 In this case the second argument must be a context name and the
383 third argument must be a string containing option names separated
386 Example of **incompatibilities** setting:
390 void ctxopt_add_ctx_settings(incompatibilities,
394 The three options named **opt1**, **opt2** and **opt3** will be
395 marked as mutually incompatibles in each instance of the context
399 This setting allows to associate a function to the context.
401 The second argument (called *f* below) will be called as soon as the
402 context is entered or exited during the evaluation phase.
404 Note that *f* will NOT be called if the context is empty
405 (does not contain any option).
407 The next parameters must be pointers to arbitrary data which may
410 In this setting, the last parameter must be **NULL**.
412 *f* must have the following prototype:
416 int (*) (char * name1, /* Context name */
417 direction status, /* entering or exiting */
418 char * name2, /* previous or next context */
419 int nb_data, /* Number of data */
420 void ** data /* Data */);
422 This function *f* will be called when entering **AND** exiting
424 Its arguments will then be set to:
427 the name of the context.
430 will be **entering** when entering the context and **exiting**
431 when exiting the context.
434 according to the content of *status*, the name of the context we
435 are coming from or the name of the context we are returning to.
437 *name2* can be **NULL** if we are entering in the main context or
441 The number of data pointers passed to the **ctxopt_add_ctx_settings**
442 function after the *s* parameter.
445 The data pointers passed to the **ctxopt_add_ctx_settings** function
446 after the *s* parameter and arranged in an array of *nb_data*
448 Example of **actions** setting:
452 void ctxopt_add_ctx_settings(actions,
455 &data_1, &data_2, &data_3,
458 This function call registers the **action** function to the context
461 The action function will be called **after** entering to and
462 **before** exiting from each instance of the context
465 The optional *data_X* pointers will be passed to **action** through
466 its data pointer to allow it to manipulate them if needed.
467 The count of these pointers (3 here) will also be passed to action
468 through its *nb_data* parameter.
470 The ending **NULL** is mandatory.
474 * **void ctxopt_add_opt_settings(settings** \
482 This function manages some settings for an option whose name is given in
485 The first parameter *s* determines the exact setting and the
486 signification of the remaining arguments.
487 Its possible values are:
490 This setting allows to associate command line parameters with *opt*.
491 The set of parameters must be given in the third argument as a string
492 containing words separated by blanks.
494 Each appearance of one of these parameters in the command line will
495 trigger the action associated with the named option.
497 Each of these words must start with one and exactly one dash.
499 Example of **parameters** setting:
503 ctxopt_add_opt_settings(parameters,
507 In this example, **opt1** is the name of a previously defined option and
508 **-p**, **-parm** and **-p1** will be three valid command line
509 parameters for the option **opt1**.
512 This setting allows to associate a function to this options.
513 As said above, this function will be called each time the option will be
514 recognized when evaluating the command line.
516 The function pointer must be given as the third argument.
518 Following the function pointer, it is possible to add a bunch of
519 other parameters which must be pointers to some pre-allocated arbitrary
522 These pointers will be passed to the function when called.
523 The last parameter must be **NULL** to end the sequence.
525 The function needs to be given as the third argument and must
526 match the following prototype:
530 void (*) (char * ctx_name, /* Context name */
531 char * opt_name, /* Option name */
532 char * param, /* Parameter name */
533 int nb_values, /* Number of arguments */
534 char ** values, /* Arguments */
535 int nb_opt_data, /* Number of option data passed */
536 void ** opt_data, /* Array of option data passed */
537 int nb_ctx_data, /* Number of context data passed */
538 void ** ctx_data /* Array of context data passed */)
541 is the name of the current context.
544 is the name of the option.
547 is the name of the parameter that triggered the option *opt_name*.
550 is the number of arguments immediately following this option in
554 is an array of stings containing the arguments following this
555 option in the command line.
558 is the number of data pointers which were given after the third
559 arguments of **ctxopt_add_opt_settings**.
562 The data pointers passed after the third arguments of
563 **ctxopt_add_opt_settings** and reorganized as an array of
564 *nb_opt_data* elements.
566 The aim is to be able to consult/alter options specific data.
569 Same as *nb_opt_data* but referencing to the number of data
570 pointers given to **ctxopt_add_ctx_settings** for the current
571 context after its third argument.
574 are the data pointers given to **ctxopt_add_ctx_settings** for the
575 current context after its third argument.
577 The aim is to be able to consult/alter contexts specific data.
579 Example of **actions** setting:
583 void action(char * ctx_name,
586 int nb_values, char ** values,
587 int nb_opt_data, void ** opt_data,
588 int nb_ctx_data, void ** ctx_data)
595 void ctxopt_add_opt_settings(actions, "opt1", action,
596 &data_1, &data_2, &data_3,
599 This example associates the function *action* to the option **opt1**.
601 Here, the *data_** pointers will be accessible to the function
602 *action* when called through its argument *opt_data* and their number
603 (3 here) through its argument *nb_opt_data* as mentioned above.
605 *action* will also have access to the current context data in the
606 same way through its arguments *ctx_data* and *nb_ctx_data*.
608 The *action* argument *param* will receive the value of the specific
609 parameter which triggered it - one of the parameters registered with
610 **ctxopt_add_opt_settings**.
613 This setting registers a function whose responsibility is to validate
614 that the arguments of the option respect some constraints.
616 To do that the third argument must be a function pointer and the fourth
617 argument must be some arbitrary parameter to this function needed
618 to validate the constraint.
620 The constraint function must match the following prototype:
624 int (*) (int nb_args, char ** args, char * value, char * parameter);
629 is the number which will be set to the number of arguments fol-
630 lowing the command line parameter.
633 is an array of nb_args strings containing theses arguments.
636 is an arbitrary string containing the constraints which must be
640 is the parameter of which *value* is an argument.
642 Three constraint functions are built-in and are described below.
643 They give examples on how to build them.
645 Example of constraint function using the built-it regular expression
646 constraint checker function:
650 ctxopt_add_opt_settings(constraints,
652 ctxopt_re_constraint,
656 In this example all the arguments of the option **opt1** must match
657 the extended regular expression::
661 See below for details about the function **ctxopt_re_constraint**.
665 * **int ctxopt_format_constraint(int** \
675 This pre-defined constraint function checks whether the arguments
676 in *args* respect a C printf format given in value, `%2d` by e.g.
677 It returns 1 if the checking is successful and 0 if not.
681 * **int ctxopt_re_constraint(int** \
691 Another pre-defined constraint function which checks if the arguments
692 of an option respects the extended regular expression given in *value*.
694 It returns 1 if the arguments respects the constraint and 0 if this
699 * **int ctxopt_range_constraint(int** \
709 Yet another pre-defined constraint function. This one checks if the
710 arguments of an option are in in a specified ranges.
712 *value* must contain a string made of a maximum of 2 long integers
715 The first or the second of these numbers can be replaced with the
716 character '`.`'. In this case only the minimum or maximum is checked
717 and the '`.`' equals to plus or minus infinity depending of this
720 It returns 1 if the arguments respects the constraint and 0 if this
725 * **void ctxopt_analyze(int** \
735 This function processes the registered contexts instances tree, detects
736 errors and possibly reorganizes the options order according
739 The first two arguments are similar to the *argc* and *argv* arguments
740 of the main function but without counting `argv[0]`.
741 Therefore, in many cases, *nb_words* will have the value of `argc-1`
742 and *words* will have the value of `argv+1`.
744 The last two will receive the number of remaining (non analyzed)
745 command line words and the array of these remaining words.
746 Remaining words can be words appearing after ``--`` per example.
748 All errors are fatal and terminates the program with a return code
758 ctxopt_analyze(argc-1, argv+1, &res_argc, &res_argv);
762 * **void ctxopt_evaluate(void);**
764 This function walks through the tree of context instances previously
765 built by **ctxopt_analyze** and launches the action attached to
766 each options, if any, one after the other.
768 * **ctxopt_free_memory(void)**
770 This function frees the memory used internally by **ctxopt**.