Add an example using two contexts
[ctxopt.git] / README.rst
blob8bf9b98a0c253865ae1d15633e53939daeeb6eb6
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.
16 Features:
17 ---------
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
36   parameters.
37 - Command lines options are evaluated in order but some options can be
38   forced to be evaluated first though.
40 Usage:
41 ------
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).
50 - call ctxopt_init.
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 ---------------------------------------------------
65 .. code:: c
66   :number-lines:
68   #include "ctxopt.h"
69   #include <stdio.h>
70   #include <stdlib.h>
71   #include <string.h>
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)
79   {
80     int v;
82     printf("Hello %s", values[0]); /* First command line argument after name *
83                                     | (-n or -name).                         */
85     for (v = 1; v < nb_values; v++) /* Other command line arguments.         */
86       printf(", %s", values[v]);
88     printf(".\n");
89   }
91   /* Program entry */
92   /* ************* */
94   int main(int argc, char * argv[])
95   {
96     int     nb_rem_args = 0;    /* Nb of remaining unprocessed arguments. */
97     char ** rem_args    = NULL; /* Remaining arguments string array.      */
99     ctxopt_init(argv[0]);
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);
105     if (nb_rem_args > 0)
106     {
107       printf("Non-arguments are not allowed.\n");
108       exit(EXIT_FAILURE);
109     }
111     ctxopt_evaluate();
113     if (argc == 1)
114       printf("Hello world.\n");
116     exit(EXIT_SUCCESS);
117   }
119 Code explanations:
120 ..................
122 Line 1:
124   This ``#include`` gives access to the API necessary to use **ctxopt**.
126 Line 9:
128   This function is the callback function call each time a parameter
129   associated with the option **name** is seen in the command line.
131 Line 32:
133   The init function is mandatory and must be called first.
135 Line 33:
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
144   argument.
146 Line 34:
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.
151 Line 35:
153   Here the callback function defined line 9 is associated with the option
154   **name**.
156 Line 36:
158   Here the command line is parsed and errors like unknown parameter, not
159   enough arguments... are detected. All errors detected during this phase
160   are fatal.
162 Line 38:
164   The remaining non-arguments, if any, are managed here.
166 Line 44:
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.
172 Line 46:
174   The special case where the command line only contains the program name
175   is treated here.
177 Examples of running session:
178 ............................
180 .. parsed-literal::
182   **$ ./hello -n Alice Bob -name Carol**
183   Hello Alice, Bob.
184   Hello Carol.
186   **$ ./hello -n**
187   -n requires argument(s).
189   Synopsis:
190   hello \
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.
201   **$ ./hello**
202   Hello world.