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 - Command line options are normally evaluated in order but can be forced
40 to be evaluated at the start of a context. They can also be asked to
41 be reordered automatically to be evaluated before or after a set of
42 other options in a context.
43 - Error functions can be customized.
44 - Automatic detection of missing|incompatible|unknown|... options or
47 Context notion illustrated by a tiny example:
48 ---------------------------------------------
50 Imagine a situation where you want an option to be allowed only if
51 another option was previously given.
53 For example, you want the *group* option to be allowed only after
56 With **ctxopt** its easy, you just have to define two contexts (at least one
57 is mandatory), tell the *user* option to switch to the second context
58 (named ``ctx1`` here) and define the *group* option in the second context.
60 +------------------+-----------------+--------------+-------------------+
61 | Defined contexts | Allowed options | Next context | Option parameters |
62 +==================+=================+==============+===================+
63 | ``main`` | user | ``ctx1`` | ``-u`` ``-user`` |
64 +------------------+-----------------+--------------+-------------------+
65 | ``ctx1`` | group | | ``-g`` ``-group`` |
66 +------------------+-----------------+--------------+-------------------+
68 According to the situation summarized in this table, the following
69 command line (the context changes in brackets have been added only for
70 understanding and not part of the command line)
73 prog[main] -u[ctx1] u1 -g g1 g2 -user[ctx1] u2 -group g3
75 will be understood as:
78 Context main: prog -u u1 -user u2
79 Context ctx1: -g g1 g2 -group g3
81 In this example, you can see that the previous context (``main`` here) is
82 automatically re-entered after the analysis of the *group* option because
83 the *user* option is unknown in the ``ctx1`` context.
85 See the file **example1.c** in the **examples** directory for details.
90 To use **ctxopt**, the users must at least:
92 - include **ctxopt.h** and **ctxopt.c** in his code.
93 - define at least one context and describe its options.
94 - set at least one parameter's name for the options described in the contexts.
95 - write and attach an action callback function to each options.
96 - possibly register constraint and other things (optional).
97 - call ``ctxopt_init``.
98 - call ``ctxopt_analyze``.
99 - call ``ctxopt_evaluate``.
101 Optional steps include:
103 - register entering and/or exiting function for contexts.
104 - register arguments constraint checking functions.
105 - redefine non internal error functions.
107 For more, please read the provided man page.
109 Enough theory, here is a basic Hello World example:
110 ---------------------------------------------------
120 /* Callback functions */
121 /* ****************** */
123 void name_action(char * ctx_name, char * opt_name, char * param,
124 int nb_values, char ** values, int nb_opt_data,
125 void ** opt_data, int nb_ctx_data, void** ctx_data)
129 printf("Hello %s", values[0]); /* First command line argument after name *
132 for (v = 1; v < nb_values; v++) /* Other command line arguments. */
133 printf(", %s", values[v]);
141 int main(int argc, char * argv[])
143 int nb_rem_args = 0; /* Nb of remaining unprocessed arguments. */
144 char ** rem_args = NULL; /* Remaining arguments string array. */
146 ctxopt_init(argv[0], "stop_if_non_option=Yes allow_abbreviations=Yes");
147 ctxopt_new_ctx("main", "[name... #<string>...]");
148 ctxopt_add_opt_settings(parameters, "name", "-n -name");
149 ctxopt_add_opt_settings(actions, "name", name_action, NULL);
150 ctxopt_analyze(argc - 1, argv + 1, &nb_rem_args, &rem_args);
154 printf("Non-arguments are not allowed.\n");
161 printf("Hello world.\n");
171 This ``#include`` gives access to the API necessary to use **ctxopt**.
175 This function is the callback function call each time a parameter
176 associated with the option **name** is seen in the command line.
180 The init function is mandatory and must be called first.
184 Here the first (and unique here) context called **main** here is
185 created with the description of an option called **name**.
187 The **name** option is defined as an optional possible multiple option
188 taking mandatory possibly multiple arguments.
189 It is the ``#`` which indicates the presence of an argument,
190 ``<string>`` is just a decaration to clarify the meaning of this
195 It's now time to introduce the two parameters of the option **name**.
196 These are the parameters looked for in the command line.
200 Here the callback function defined line 9 is associated with the option
205 Here the command line is parsed and errors like unknown parameter, not
206 enough arguments... are detected. All errors detected during this phase
211 The remaining non-arguments, if any, are managed here.
215 All the internal representation of the command line built during the
216 analysis phase (line 36) is finally evaluated and the callback
217 registered functions (here **name_action**) called.
221 The special case where the command line only contains the program name
224 Examples of running session:
225 ............................
229 **$ ./hello -n Alice Bob -name Carol**
234 -n requires argument(s).
238 [-n|-name... #<string>...]
240 Syntactic explanations:
241 Only the parameters (prefixed by -) and the arguments, if any, must be entered.
242 The following is just there to explain the other symbols displayed.
244 #tag : argument tag giving a clue to its meaning.
245 [...] : the object between square brackets is optional.
246 ... : the previous object can be repeated more than one time.