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 options and
11 excels when they appear in structured or independent blocs.
13 ctxopt uses another (better) approach to manage these simple and complex
14 command lines: options are grouped in contexts, hence the ctx in ctxopt.
19 Its main features are:
21 - Options are organized in a hierarchy of contexts.
22 - Any number of parameters can be assigned to each option.
23 - Parameters are not limited to just one character.
24 - The parameters associated with an option can be abbreviated as long as
25 they do not conflict with another option in the same context.
26 - Parameters can be aggregated, even the long ones.
27 - Options evaluations trigger user-defined functions.
28 - Options can appear multiple times in one or more contexts.
29 - Options can be optional or mandatory.
30 - Option arguments can be optional or mandatory.
31 - Arguments can be multiple and their number controlled by simple rules.
32 - Arguments can have user-defined or built-in constraints.
33 - Options marked as mutually incompatibles are automatically detected.
34 - Error functions can be customized.
35 - Automatic detection of missing|incompatible|unknown|... options or
37 - Command lines options are evaluated in order but some options can be
38 forced to be evaluated first though.
43 To use ctxopt, the users must at least:
45 - include **ctxopt.h** and **ctxopt.c** in his code.
46 - define at least one context and describe its options.
47 - set at least one parameter's name for the options described in the contexts.
48 - write and attach an action callback function to each options.
49 - possibly register constraint and other things (optional).
51 - call ctxopt_analyze.
52 - call ctxopt_evaluate.
54 Optional steps include:
56 - register entering and/or exiting function for contexts.
57 - register arguments constraint checking functions.
58 - redefine non internal error functions.
60 For more, please read the provided man page.
62 Enough theory, here is a basic Hello World example:
63 ---------------------------------------------------
73 /* Callback functions */
74 /* ****************** */
76 void name_action(char * ctx_name, char * opt_name, char * param,
77 int nb_values, char ** values, int nb_opt_data,
78 void ** opt_data, int nb_ctx_data, void** ctx_data)
82 printf("Hello %s", values[0]); /* First command line argument after name *
85 for (v = 1; v < nb_values; v++) /* Other command line arguments. */
86 printf(", %s", values[v]);
94 int main(int argc, char * argv[])
96 int nb_rem_args = 0; /* Nb of remaining unprocessed arguments. */
97 char ** rem_args = NULL; /* Remaining arguments string array. */
100 ctxopt_new_ctx("main", "[name... #<string>...]");
101 ctxopt_add_opt_settings(parameters, "name", "-n -name");
102 ctxopt_add_opt_settings(actions, "name", name_action, NULL);
103 ctxopt_analyze(argc - 1, argv + 1, &nb_rem_args, &rem_args);
107 printf("Non-arguments are not allowed.\n");
114 printf("Hello world.\n");
124 This ``#include`` gives access to the API necessary to use **ctxopt**.
128 This function is the callback function call each time a parameter
129 associated with the option **name** is seen in the command line.
133 The init function is mandatory and must be called first.
137 Here the first (and unique here) context called **main** here is
138 created with the description of an option called **name**.
140 The **name** option is defined as an optional possible multiple option
141 taking mandatory possibly multiple arguments.
142 It is the ``#`` which indicates the presence of an argument,
143 ``<string>`` is just a decaration to clarify the meaning of this
148 It's now time to introduce the two parameters of the option **name**.
149 These are the parameters looked for in the command line.
153 Here the callback function defined line 9 is associated with the option
158 Here the command line is parsed and errors like unknown parameter, not
159 enough arguments... are detected. All errors detected during this phase
164 The remaining non-arguments, if any, are managed here.
168 All the internal representation of the command line built during the
169 analysis phase (line 36) is finally evaluated and the callback
170 registered functions (here **name_action**) called.
174 The special case where the command line only contains the program name
177 Examples of running session:
178 ............................
182 **$ ./hello -n Alice Bob -name Carol**
187 -n requires argument(s).
191 [-n|-name... #<string>...]
193 Syntactic explanations:
194 Only the parameters (prefixed by -) and the arguments, if any, must be entered.
195 The following is just there to explain the other symbols displayed.
197 #tag : argument tag giving a clue to its meaning.
198 [...] : the object between square brackets is optional.
199 ... : the previous object can be repeated more than one time.