work on datastore API implementation
[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    * For configuration
64    */
65   struct GNUNET_CONFIGURATION_Handle *cfg;
66
67   /**
68    * Original command line
69    */
70   char *const *argv;
71
72   /**
73    * Total number of argv's.
74    */
75   unsigned int argc;
76
77   /**
78    * Current argument.
79    */
80   unsigned int currentArgument;
81
82 };
83
84 /**
85  * @brief Process a command line option
86  *
87  * @param ctx context for all options
88  * @param scls specific closure (for this processor)
89  * @param option long name of the option (i.e. "config" for --config)
90  * @param value argument, NULL if none was given
91  * @return GNUNET_OK to continue processing other options, GNUNET_SYSERR to abort
92  */
93 typedef
94   int (*GNUNET_GETOPT_CommandLineOptionProcessor) (struct
95                                                    GNUNET_GETOPT_CommandLineProcessorContext
96                                                    * ctx, void *scls,
97                                                    const char *option,
98                                                    const char *value);
99
100 /**
101  * @brief Definition of a command line option.
102  */
103 struct GNUNET_GETOPT_CommandLineOption
104 {
105
106   /**
107    * Short name of the option (use '\0' for none).
108    */
109   const char shortName;
110
111   /**
112    * Long name of the option (may not be NULL)
113    */
114   const char *name;
115
116   /**
117    * Name of the argument for the user in help text
118    */
119   const char *argumentHelp;
120
121   /**
122    * Help text for the option (description)
123    */
124   const char *description;
125
126   /**
127    * Is an argument required?  0: GNUNET_NO (includes optional), 1: GNUNET_YES.
128    */
129   int require_argument;
130
131   /**
132    * Handler for the option.
133    */
134   GNUNET_GETOPT_CommandLineOptionProcessor processor;
135
136   /**
137    * Specific closure to pass to the processor.
138    */
139   void *scls;
140
141 };
142
143 /**
144  * Macro defining the option to print the command line
145  * help text.
146  *
147  * @param about string with brief description of the application
148  */
149 #define GNUNET_GETOPT_OPTION_HELP(about) \
150   { 'h', "help", (const char *) NULL, gettext_noop("print this help"), 0, &GNUNET_GETOPT_format_help_, (void *) about }
151
152 /**
153  * Macro defining the option to print the version of
154  * the application
155  *
156  * @param version string with the version number
157  */
158 #define GNUNET_GETOPT_OPTION_VERSION(version) \
159   { 'v', "version", (const char *) NULL, gettext_noop("print the version number"), 0, &GNUNET_GETOPT_print_version_, (void *) version }
160
161 /**
162  * Get the log level
163  */
164 #define GNUNET_GETOPT_OPTION_LOGFILE(logfn)                             \
165   { 'l', "logfile", "LOGFILE", gettext_noop("configure logging to write logs to LOGFILE"), 1, &GNUNET_GETOPT_set_string, (void *) logfn }
166
167 /**
168  * Set the configuration option for logging.
169  */
170 #define GNUNET_GETOPT_OPTION_LOGLEVEL(loglev)                           \
171   { 'L', "log", "LOGLEVEL", gettext_noop("configure logging to use LOGLEVEL"), 1, &GNUNET_GETOPT_set_string, (void *) loglev }
172
173 /**
174  * Get number of verbose flags
175  */
176 #define GNUNET_GETOPT_OPTION_VERBOSE(level)                             \
177   { 'V', "verbose", (const char *) NULL, gettext_noop("be verbose"), 0, &GNUNET_GETOPT_increment_value, (void *) level }
178
179 /**
180  * Get configuration file name
181  */
182 #define GNUNET_GETOPT_OPTION_CFG_FILE(fn)                               \
183   { 'c', "config", "FILENAME", gettext_noop("use configuration file FILENAME"), 1, &GNUNET_GETOPT_set_string, (void *) fn }
184
185 /**
186  * Marker to end the list of options.
187  */
188 #define GNUNET_GETOPT_OPTION_END \
189   { '\0', NULL, NULL, NULL, 0, NULL, NULL }
190
191 /**
192  * Parse the command line.
193  *
194  * @param binaryName name of the binary / application with options
195  * @param cfg for storing/accessing configuration data
196  * @param allOptions defined options and handlers
197  * @param argc number of arguments
198  * @param argv actual arguments
199  * @return index into argv with first non-option
200  *   argument, or GNUNET_SYSERR on error
201  */
202 int GNUNET_GETOPT_run (const char *binaryName,
203                        struct GNUNET_CONFIGURATION_Handle *cfg,
204                        const struct GNUNET_GETOPT_CommandLineOption
205                        *allOptions, unsigned int argc, char *const *argv);
206
207 int GNUNET_GETOPT_set_ulong (struct GNUNET_GETOPT_CommandLineProcessorContext
208                              *ctx, void *scls, const char *option,
209                              const char *value);
210
211 int GNUNET_GETOPT_set_uint (struct GNUNET_GETOPT_CommandLineProcessorContext
212                             *ctx, void *scls, const char *option,
213                             const char *value);
214
215 int GNUNET_GETOPT_set_one (struct GNUNET_GETOPT_CommandLineProcessorContext
216                            *ctx, void *scls, const char *option,
217                            const char *value);
218
219 int GNUNET_GETOPT_set_string (struct GNUNET_GETOPT_CommandLineProcessorContext
220                               *ctx, void *scls, const char *option,
221                               const char *value);
222
223 int
224 GNUNET_GETOPT_increment_value (struct
225                                GNUNET_GETOPT_CommandLineProcessorContext *ctx,
226                                void *scls, const char *option,
227                                const char *value);
228
229 /* *************** internal prototypes - use macros above! ************* */
230
231 int GNUNET_GETOPT_format_help_ (struct
232                                 GNUNET_GETOPT_CommandLineProcessorContext
233                                 *ctx, void *scls, const char *option,
234                                 const char *value);
235
236 int GNUNET_GETOPT_print_version_ (struct
237                                   GNUNET_GETOPT_CommandLineProcessorContext
238                                   *ctx, void *scls, const char *option,
239                                   const char *value);
240
241 #if 0                           /* keep Emacsens' auto-indent happy */
242 {
243 #endif
244 #ifdef __cplusplus
245 }
246 #endif
247
248
249 /* ifndef GNUNET_GETOPT_LIB_H */
250 #endif
251 /* end of gnunet_getopt_lib.h */