Remove useless code
[ctxopt.git] / README.rst
blobb68a72d24d5054d35abcd49c99246a11f6323970
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 **ctxopt**  uses another (better) approach to manage these simple and
14 complex command lines: options are grouped in contexts, hence the ctx
15 in **ctxopt**.
17 Features:
18 ---------
20 **ctxopt** has many features, its main ones are:
22 - Options are organized in a hierarchy of contexts.
23 - Any number of parameters can be assigned to each option.
24 - Parameters are not limited to just one character.
25 - The parameters associated with an option can be abbreviated as long as
26   they do not conflict with another option in the same context.
27 - Parameters can be aggregated, even the long ones.
28 - Options evaluations trigger user-defined functions.
29 - Options can appear multiple times in one or more contexts.
30 - Options can be optional or mandatory.
31 - Option arguments can be optional or mandatory.
32 - Arguments can be multiple and their number controlled by simple rules.
33 - Arguments can have user-defined or built-in constraints.
34 - Options marked as mutually incompatibles are automatically detected.
35 - Error functions can be customized.
36 - Automatic detection of missing|incompatible|unknown|... options or
37   parameters.
38 - Command lines options are evaluated in order but some options can be
39   forced to be evaluated first though.
41 Context notion illustrated by a tiny example:
42 ---------------------------------------------
44 Imagine a situation where you want an option to be allowed only if
45 another option was previously given.
47 For example, you want the *group* option to be allowed only after
48 a *user* option.
50 With **ctxopt** its easy, you just have to define two contexts (at least one
51 is mandatory), tell the *user* option to switch to the second context
52 (named ``ctx1`` here) and define the *group* option in the second context.
54 +------------------+-----------------+--------------+-------------------+
55 | Defined contexts | Allowed options | Next context | Option parameters |
56 +==================+=================+==============+===================+
57 | ``main``         | user            | ``ctx1``     | ``-u`` ``-user``  |
58 +------------------+-----------------+--------------+-------------------+
59 | ``ctx1``         | group           |              | ``-g`` ``-group`` |
60 +------------------+-----------------+--------------+-------------------+
62 According to the situation summarized in this table, the following
63 command line (the context changes in brackets have been added only for
64 understanding and not part of the command line)
66 .. parsed-literal::
67   prog[main] -u[ctx1] u1 -g g1 g2 -user[ctx1] u2 -group g3
69 will be understood as:
71 .. parsed-literal::
72   Context main:  prog -u u1          -user u2
73   Context ctx1:             -g g1 g2          -group g3
75 In this example, you can see that the previous context (``main`` here) is
76 automatically re-entered after the analysis of the *group* option because
77 the *user* option is unknown in the ``ctx1`` context.
79 See the file **example1.c** in the **examples** directory for details.
81 Usage:
82 ------
84 To use **ctxopt**, the users must at least:
86 - include **ctxopt.h** and **ctxopt.c** in his code.
87 - define at least one context and describe its options.
88 - set at least one parameter's name for the options described in the contexts.
89 - write and attach an action callback function to each options.
90 - possibly register constraint and other things (optional).
91 - call ``ctxopt_init``.
92 - call ``ctxopt_analyze``.
93 - call ``ctxopt_evaluate``.
95 Optional steps include:
97 - register entering and/or exiting function for contexts.
98 - register arguments constraint checking functions.
99 - redefine non internal error functions.
101 For more, please read the provided man page.
103 Enough theory, here is a basic Hello World example:
104 ---------------------------------------------------
106 .. code:: c
107   :number-lines:
109   #include "ctxopt.h"
110   #include <stdio.h>
111   #include <stdlib.h>
112   #include <string.h>
114   /* Callback functions */
115   /* ****************** */
117   void name_action(char * ctx_name, char * opt_name, char * param,
118                    int nb_values, char ** values, int nb_opt_data,
119                    void ** opt_data, int nb_ctx_data, void** ctx_data)
120   {
121     int v;
123     printf("Hello %s", values[0]); /* First command line argument after name *
124                                     | (-n or -name).                         */
126     for (v = 1; v < nb_values; v++) /* Other command line arguments.         */
127       printf(", %s", values[v]);
129     printf(".\n");
130   }
132   /* Program entry */
133   /* ************* */
135   int main(int argc, char * argv[])
136   {
137     int     nb_rem_args = 0;    /* Nb of remaining unprocessed arguments. */
138     char ** rem_args    = NULL; /* Remaining arguments string array.      */
140     ctxopt_init(argv[0]);
141     ctxopt_new_ctx("main", "[name... #<string>...]");
142     ctxopt_add_opt_settings(parameters, "name", "-n -name");
143     ctxopt_add_opt_settings(actions, "name", name_action, NULL);
144     ctxopt_analyze(argc - 1, argv + 1, &nb_rem_args, &rem_args);
146     if (nb_rem_args > 0)
147     {
148       printf("Non-arguments are not allowed.\n");
149       exit(EXIT_FAILURE);
150     }
152     ctxopt_evaluate();
154     if (argc == 1)
155       printf("Hello world.\n");
157     exit(EXIT_SUCCESS);
158   }
160 Code explanations:
161 ..................
163 Line 1:
165   This ``#include`` gives access to the API necessary to use **ctxopt**.
167 Line 9:
169   This function is the callback function call each time a parameter
170   associated with the option **name** is seen in the command line.
172 Line 32:
174   The init function is mandatory and must be called first.
176 Line 33:
178   Here the first (and unique here) context called **main** here is
179   created with the description of an option called **name**.
181   The **name** option is defined as an optional possible multiple option
182   taking mandatory possibly multiple arguments.
183   It is the ``#`` which indicates the presence of an argument,
184   ``<string>`` is just a decaration to clarify the meaning of this
185   argument.
187 Line 34:
189   It's now time to introduce the two parameters of the option **name**.
190   These are the parameters looked for in the command line.
192 Line 35:
194   Here the callback function defined line 9 is associated with the option
195   **name**.
197 Line 36:
199   Here the command line is parsed and errors like unknown parameter, not
200   enough arguments... are detected. All errors detected during this phase
201   are fatal.
203 Line 38:
205   The remaining non-arguments, if any, are managed here.
207 Line 44:
209   All the internal representation of the command line built during the
210   analysis phase (line 36) is finally evaluated and the callback
211   registered functions (here **name_action**) called.
213 Line 46:
215   The special case where the command line only contains the program name
216   is treated here.
218 Examples of running session:
219 ............................
221 .. parsed-literal::
223   **$ ./hello -n Alice Bob -name Carol**
224   Hello Alice, Bob.
225   Hello Carol.
227   **$ ./hello -n**
228   -n requires argument(s).
230   Synopsis:
231   hello \
232     [-n|-name... #<string>...]
234   Syntactic explanations:
235   Only the parameters (prefixed by -) and the arguments, if any, must be entered.
236   The following is just there to explain the other symbols displayed.
238   #tag         : argument tag giving a clue to its meaning.
239   [...]        : the object between square brackets is optional.
240   ...          : the previous object can be repeated more than one time.
242   **$ ./hello**
243   Hello world.