1 **ctxopt**: yet another command line options manager.
2 #####################################################
6 For many uses, the traditional getopt function is enough to parse the
7 options in command lines.
9 However, cases exist where getopt shows its limits.
10 **ctxopt** is able to manage complex configurations of command line
11 options and excels when they appear in structured or independent blocs.
13 In **ctxopt**, each option has the possibility of starting a new context
14 in which the following command line options will be taken into account
16 With this concept, it becomes easy, for example, to have repetitions
17 of identical options with each their independent sub-options.
22 **ctxopt** has many features, its main ones are:
24 - Options are organized in a hierarchy of contexts.
25 - Options are easily declared using a syntax similar to BNF notation in
27 - Any number of parameters can be assigned to each option.
28 - Parameters are not limited to just one character.
29 - The parameters associated with an option can be abbreviated as long as
30 they do not conflict with another option in the same context.
31 - Parameters can be aggregated, even the long ones.
32 - Options evaluations trigger user-defined functions.
33 - Options can appear multiple times in one or more contexts.
34 - Options can be optional or mandatory.
35 - Option arguments can be optional or mandatory.
36 - Arguments can be multiple and their number controlled by simple rules.
37 - Arguments can have user-defined or built-in constraints.
38 - Options marked as mutually incompatibles are automatically detected.
39 - Error functions can be customized.
40 - Automatic detection of missing|incompatible|unknown|... options or
42 - Command lines options are evaluated in order but some options can be
43 forced to be evaluated first though.
45 Context notion illustrated by a tiny example:
46 ---------------------------------------------
48 Imagine a situation where you want an option to be allowed only if
49 another option was previously given.
51 For example, you want the *group* option to be allowed only after
54 With **ctxopt** its easy, you just have to define two contexts (at least one
55 is mandatory), tell the *user* option to switch to the second context
56 (named ``ctx1`` here) and define the *group* option in the second context.
58 +------------------+-----------------+--------------+-------------------+
59 | Defined contexts | Allowed options | Next context | Option parameters |
60 +==================+=================+==============+===================+
61 | ``main`` | user | ``ctx1`` | ``-u`` ``-user`` |
62 +------------------+-----------------+--------------+-------------------+
63 | ``ctx1`` | group | | ``-g`` ``-group`` |
64 +------------------+-----------------+--------------+-------------------+
66 According to the situation summarized in this table, the following
67 command line (the context changes in brackets have been added only for
68 understanding and not part of the command line)
71 prog[main] -u[ctx1] u1 -g g1 g2 -user[ctx1] u2 -group g3
73 will be understood as:
76 Context main: prog -u u1 -user u2
77 Context ctx1: -g g1 g2 -group g3
79 In this example, you can see that the previous context (``main`` here) is
80 automatically re-entered after the analysis of the *group* option because
81 the *user* option is unknown in the ``ctx1`` context.
83 See the file **example1.c** in the **examples** directory for details.
88 To use **ctxopt**, the users must at least:
90 - include **ctxopt.h** and **ctxopt.c** in his code.
91 - define at least one context and describe its options.
92 - set at least one parameter's name for the options described in the contexts.
93 - write and attach an action callback function to each options.
94 - possibly register constraint and other things (optional).
95 - call ``ctxopt_init``.
96 - call ``ctxopt_analyze``.
97 - call ``ctxopt_evaluate``.
99 Optional steps include:
101 - register entering and/or exiting function for contexts.
102 - register arguments constraint checking functions.
103 - redefine non internal error functions.
105 For more, please read the provided man page.
107 Enough theory, here is a basic Hello World example:
108 ---------------------------------------------------
118 /* Callback functions */
119 /* ****************** */
121 void name_action(char * ctx_name, char * opt_name, char * param,
122 int nb_values, char ** values, int nb_opt_data,
123 void ** opt_data, int nb_ctx_data, void** ctx_data)
127 printf("Hello %s", values[0]); /* First command line argument after name *
130 for (v = 1; v < nb_values; v++) /* Other command line arguments. */
131 printf(", %s", values[v]);
139 int main(int argc, char * argv[])
141 int nb_rem_args = 0; /* Nb of remaining unprocessed arguments. */
142 char ** rem_args = NULL; /* Remaining arguments string array. */
144 ctxopt_init(argv[0], "stop_if_non_option=Yes allow_abbreviations=Yes");
145 ctxopt_new_ctx("main", "[name... #<string>...]");
146 ctxopt_add_opt_settings(parameters, "name", "-n -name");
147 ctxopt_add_opt_settings(actions, "name", name_action, NULL);
148 ctxopt_analyze(argc - 1, argv + 1, &nb_rem_args, &rem_args);
152 printf("Non-arguments are not allowed.\n");
159 printf("Hello world.\n");
169 This ``#include`` gives access to the API necessary to use **ctxopt**.
173 This function is the callback function call each time a parameter
174 associated with the option **name** is seen in the command line.
178 The init function is mandatory and must be called first.
182 Here the first (and unique here) context called **main** here is
183 created with the description of an option called **name**.
185 The **name** option is defined as an optional possible multiple option
186 taking mandatory possibly multiple arguments.
187 It is the ``#`` which indicates the presence of an argument,
188 ``<string>`` is just a decaration to clarify the meaning of this
193 It's now time to introduce the two parameters of the option **name**.
194 These are the parameters looked for in the command line.
198 Here the callback function defined line 9 is associated with the option
203 Here the command line is parsed and errors like unknown parameter, not
204 enough arguments... are detected. All errors detected during this phase
209 The remaining non-arguments, if any, are managed here.
213 All the internal representation of the command line built during the
214 analysis phase (line 36) is finally evaluated and the callback
215 registered functions (here **name_action**) called.
219 The special case where the command line only contains the program name
222 Examples of running session:
223 ............................
227 **$ ./hello -n Alice Bob -name Carol**
232 -n requires argument(s).
236 [-n|-name... #<string>...]
238 Syntactic explanations:
239 Only the parameters (prefixed by -) and the arguments, if any, must be entered.
240 The following is just there to explain the other symbols displayed.
242 #tag : argument tag giving a clue to its meaning.
243 [...] : the object between square brackets is optional.
244 ... : the previous object can be repeated more than one time.