docu
[oweals/gnunet.git] / src / include / gnunet_getopt_lib.h
1 /*
2      This file is part of GNUnet.
3      (C) 2001, 2002, 2003, 2004, 2005, 2006, 2009 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 2, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file include/gnunet_getopt_lib.h
23  * @brief command line parsing and --help formatting
24  *
25  * @author Christian Grothoff
26  */
27
28 #ifndef GNUNET_GETOPT_LIB_H
29 #define GNUNET_GETOPT_LIB_H
30
31 #ifdef __cplusplus
32 extern "C"
33 {
34 #if 0                           /* keep Emacsens' auto-indent happy */
35 }
36 #endif
37 #endif
38
39 #include "gnunet_configuration_lib.h"
40
41 /**
42  * @brief General context for command line processors.
43  */
44 struct GNUNET_GETOPT_CommandLineProcessorContext
45 {
46
47   /**
48    * Name of the application
49    */
50   const char *binaryName;
51
52   /**
53    * Name of application with option summary
54    */
55   const char *binaryOptions;
56
57   /**
58    * Array with all command line options.
59    */
60   const struct GNUNET_GETOPT_CommandLineOption *allOptions;
61
62   /**
63    * Original command line
64    */
65   char *const *argv;
66
67   /**
68    * Total number of argv's.
69    */
70   unsigned int argc;
71
72   /**
73    * Current argument.
74    */
75   unsigned int currentArgument;
76
77 };
78
79 /**
80  * @brief Process a command line option
81  *
82  * @param ctx context for all options
83  * @param scls specific closure (for this processor)
84  * @param option long name of the option (i.e. "config" for --config)
85  * @param value argument, NULL if none was given
86  * @return GNUNET_OK to continue processing other options, GNUNET_SYSERR to abort
87  */
88 typedef
89   int (*GNUNET_GETOPT_CommandLineOptionProcessor) (struct
90                                                    GNUNET_GETOPT_CommandLineProcessorContext
91                                                    * ctx, void *scls,
92                                                    const char *option,
93                                                    const char *value);
94
95 /**
96  * @brief Definition of a command line option.
97  */
98 struct GNUNET_GETOPT_CommandLineOption
99 {
100
101   /**
102    * Short name of the option (use '\\0' for none).
103    */
104   const char shortName;
105
106   /**
107    * Long name of the option (may not be NULL)
108    */
109   const char *name;
110
111   /**
112    * Name of the argument for the user in help text
113    */
114   const char *argumentHelp;
115
116   /**
117    * Help text for the option (description)
118    */
119   const char *description;
120
121   /**
122    * Is an argument required?  0: GNUNET_NO (includes optional), 1: GNUNET_YES.
123    */
124   int require_argument;
125
126   /**
127    * Handler for the option.
128    */
129   GNUNET_GETOPT_CommandLineOptionProcessor processor;
130
131   /**
132    * Specific closure to pass to the processor.
133    */
134   void *scls;
135
136 };
137
138 /**
139  * Macro defining the option to print the command line
140  * help text.
141  *
142  * @param about string with brief description of the application
143  */
144 #define GNUNET_GETOPT_OPTION_HELP(about) \
145   { 'h', "help", (const char *) NULL, gettext_noop("print this help"), 0, &GNUNET_GETOPT_format_help_, (void *) about }
146
147 /**
148  * Macro defining the option to print the version of
149  * the application
150  *
151  * @param version string with the version number
152  */
153 #define GNUNET_GETOPT_OPTION_VERSION(version) \
154   { 'v', "version", (const char *) NULL, gettext_noop("print the version number"), 0, &GNUNET_GETOPT_print_version_, (void *) version }
155
156 /**
157  * Get the log level
158  */
159 #define GNUNET_GETOPT_OPTION_LOGFILE(logfn)                             \
160   { 'l', "logfile", "LOGFILE", gettext_noop("configure logging to write logs to LOGFILE"), 1, &GNUNET_GETOPT_set_string, (void *) logfn }
161
162 /**
163  * Set the configuration option for logging.
164  */
165 #define GNUNET_GETOPT_OPTION_LOGLEVEL(loglev)                           \
166   { 'L', "log", "LOGLEVEL", gettext_noop("configure logging to use LOGLEVEL"), 1, &GNUNET_GETOPT_set_string, (void *) loglev }
167
168 /**
169  * Get number of verbose flags
170  */
171 #define GNUNET_GETOPT_OPTION_VERBOSE(level)                             \
172   { 'V', "verbose", (const char *) NULL, gettext_noop("be verbose"), 0, &GNUNET_GETOPT_increment_value, (void *) level }
173
174 /**
175  * Get configuration file name
176  */
177 #define GNUNET_GETOPT_OPTION_CFG_FILE(fn)                               \
178   { 'c', "config", "FILENAME", gettext_noop("use configuration file FILENAME"), 1, &GNUNET_GETOPT_set_string, (void *) fn }
179
180 /**
181  * Marker to end the list of options.
182  */
183 #define GNUNET_GETOPT_OPTION_END \
184   { '\0', NULL, NULL, NULL, 0, NULL, NULL }
185
186 /**
187  * Parse the command line.
188  *
189  * @param binaryOptions Name of application with option summary
190  * @param allOptions defined options and handlers
191  * @param argc number of arguments
192  * @param argv actual arguments
193  * @return index into argv with first non-option
194  *   argument, or GNUNET_SYSERR on error
195  */
196 int GNUNET_GETOPT_run (const char *binaryOptions,
197                        const struct GNUNET_GETOPT_CommandLineOption
198                        *allOptions, unsigned int argc, char *const *argv);
199
200
201 /**
202  * Set an option of type 'unsigned long long' from the command line.
203  * A pointer to this function should be passed as part of the
204  * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
205  * of this type.  It should be followed by a pointer to a value of
206  * type 'unsigned long long'.
207  *
208  * @param ctx command line processing context
209  * @param scls additional closure (will point to the 'unsigned long long')
210  * @param option name of the option
211  * @param value actual value of the option as a string.
212  * @return GNUNET_OK if parsing the value worked
213  */
214 int GNUNET_GETOPT_set_ulong (struct GNUNET_GETOPT_CommandLineProcessorContext
215                              *ctx, void *scls, const char *option,
216                              const char *value);
217
218
219 /**
220  * Set an option of type 'unsigned long long' from the command line.
221  * A pointer to this function should be passed as part of the
222  * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
223  * of this type.  It should be followed by a pointer to a value of
224  * type 'unsigned int'.
225  *
226  * @param ctx command line processing context
227  * @param scls additional closure (will point to the 'unsigned int')
228  * @param option name of the option
229  * @param value actual value of the option as a string.
230  * @return GNUNET_OK if parsing the value worked
231  */
232 int GNUNET_GETOPT_set_uint (struct GNUNET_GETOPT_CommandLineProcessorContext
233                             *ctx, void *scls, const char *option,
234                             const char *value);
235
236
237 /**
238  * Set an option of type 'int' from the command line to 1 if the 
239  * given option is present.
240  * A pointer to this function should be passed as part of the
241  * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
242  * of this type.  It should be followed by a pointer to a value of
243  * type 'int'.
244  *
245  * @param ctx command line processing context
246  * @param scls additional closure (will point to the 'int')
247  * @param option name of the option
248  * @param value not used (NULL)
249  * @return GNUNET_OK 
250  */
251 int GNUNET_GETOPT_set_one (struct GNUNET_GETOPT_CommandLineProcessorContext
252                            *ctx, void *scls, const char *option,
253                            const char *value);
254
255
256 /**
257  * Set an option of type 'char *' from the command line.
258  * A pointer to this function should be passed as part of the
259  * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
260  * of this type.  It should be followed by a pointer to a value of
261  * type 'char *'.
262  *
263  * @param ctx command line processing context
264  * @param scls additional closure (will point to the 'char *',
265  *             which will be allocated)
266  * @param option name of the option
267  * @param value actual value of the option (a string)
268  * @return GNUNET_OK 
269  */
270 int GNUNET_GETOPT_set_string (struct GNUNET_GETOPT_CommandLineProcessorContext
271                               *ctx, void *scls, const char *option,
272                               const char *value);
273
274 /**
275  * Set an option of type 'unsigned int' from the command line. Each
276  * time the option flag is given, the value is incremented by one.
277  * A pointer to this function should be passed as part of the
278  * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
279  * of this type.  It should be followed by a pointer to a value of
280  * type 'int'.
281  *
282  * @param ctx command line processing context
283  * @param scls additional closure (will point to the 'int')
284  * @param option name of the option
285  * @param value not used (NULL)
286  * @return GNUNET_OK 
287  */
288 int
289 GNUNET_GETOPT_increment_value (struct
290                                GNUNET_GETOPT_CommandLineProcessorContext *ctx,
291                                void *scls, const char *option,
292                                const char *value);
293
294
295 /* *************** internal prototypes - use macros above! ************* */
296
297 /**
298  * Print out details on command line options (implements --help).
299  *
300  * @param ctx command line processing context
301  * @param scls additional closure (points to about text)
302  * @param option name of the option
303  * @param value not used (NULL)
304  * @return GNUNET_SYSERR (do not continue)
305  */
306 int GNUNET_GETOPT_format_help_ (struct
307                                 GNUNET_GETOPT_CommandLineProcessorContext
308                                 *ctx, void *scls, const char *option,
309                                 const char *value);
310
311 /**
312  * Print out program version (implements --version).
313  *
314  * @param ctx command line processing context
315  * @param scls additional closure (points to version string)
316  * @param option name of the option
317  * @param value not used (NULL)
318  * @return GNUNET_SYSERR (do not continue)
319  */
320 int GNUNET_GETOPT_print_version_ (struct
321                                   GNUNET_GETOPT_CommandLineProcessorContext
322                                   *ctx, void *scls, const char *option,
323                                   const char *value);
324
325 #if 0                           /* keep Emacsens' auto-indent happy */
326 {
327 #endif
328 #ifdef __cplusplus
329 }
330 #endif
331
332
333 /* ifndef GNUNET_GETOPT_LIB_H */
334 #endif
335 /* end of gnunet_getopt_lib.h */