2 This file is part of GNUnet.
3 (C) 2001, 2002, 2003, 2004, 2005, 2006, 2009, 2011 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file include/gnunet_getopt_lib.h
23 * @brief command line parsing and --help formatting
25 * @author Christian Grothoff
28 #ifndef GNUNET_GETOPT_LIB_H
29 #define GNUNET_GETOPT_LIB_H
34 #if 0 /* keep Emacsens' auto-indent happy */
39 #include "gnunet_configuration_lib.h"
42 * @brief General context for command line processors.
44 struct GNUNET_GETOPT_CommandLineProcessorContext
48 * Name of the application
50 const char *binaryName;
53 * Name of application with option summary
55 const char *binaryOptions;
58 * Array with all command line options.
60 const struct GNUNET_GETOPT_CommandLineOption *allOptions;
63 * Original command line
68 * Total number of argv's.
75 unsigned int currentArgument;
80 * @brief Process a command line option
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
89 int (*GNUNET_GETOPT_CommandLineOptionProcessor) (struct
90 GNUNET_GETOPT_CommandLineProcessorContext
96 * @brief Definition of a command line option.
98 struct GNUNET_GETOPT_CommandLineOption
102 * Short name of the option (use '\\0' for none).
104 const char shortName;
107 * Long name of the option (may not be NULL)
112 * Name of the argument for the user in help text
114 const char *argumentHelp;
117 * Help text for the option (description)
119 const char *description;
122 * Is an argument required? 0: GNUNET_NO (includes optional), 1: GNUNET_YES.
124 int require_argument;
127 * Handler for the option.
129 GNUNET_GETOPT_CommandLineOptionProcessor processor;
132 * Specific closure to pass to the processor.
139 * Macro defining the option to print the command line
140 * help text (-h option).
142 * @param about string with brief description of the application
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 }
149 * Macro defining the option to print the version of
150 * the application (-v option)
152 * @param version string with the version number
154 #define GNUNET_GETOPT_OPTION_VERSION(version) \
155 { 'v', "version", (const char *) NULL, gettext_noop("print the version number"), 0, &GNUNET_GETOPT_print_version_, (void *) version }
159 * Allow user to specify log file name (-l option)
161 * @param logfn set to the name of the logfile
163 #define GNUNET_GETOPT_OPTION_LOGFILE(logfn) \
164 { 'l', "logfile", "LOGFILE", gettext_noop("configure logging to write logs to LOGFILE"), 1, &GNUNET_GETOPT_set_string, (void *) logfn }
168 * Allow user to specify log level (-L option)
170 * @param loglev set to the log level
172 #define GNUNET_GETOPT_OPTION_LOGLEVEL(loglev) \
173 { 'L', "log", "LOGLEVEL", gettext_noop("configure logging to use LOGLEVEL"), 1, &GNUNET_GETOPT_set_string, (void *) loglev }
177 * Get number of verbose (-V) flags
179 * @param level where to store the verbosity level (should be an 'int')
181 #define GNUNET_GETOPT_OPTION_VERBOSE(level) \
182 { 'V', "verbose", (const char *) NULL, gettext_noop("be verbose"), 0, &GNUNET_GETOPT_increment_value, (void *) level }
186 * Get configuration file name (-c option)
188 * @param fn set to the configuration file name
190 #define GNUNET_GETOPT_OPTION_CFG_FILE(fn) \
191 { 'c', "config", "FILENAME", gettext_noop("use configuration file FILENAME"), 1, &GNUNET_GETOPT_set_string, (void *) fn }
195 * Marker for the end of the list of options.
197 #define GNUNET_GETOPT_OPTION_END \
198 { '\0', NULL, NULL, NULL, 0, NULL, NULL }
202 * Parse the command line.
204 * @param binaryOptions Name of application with option summary
205 * @param allOptions defined options and handlers
206 * @param argc number of arguments
207 * @param argv actual arguments
208 * @return index into argv with first non-option
209 * argument, or GNUNET_SYSERR on error
211 int GNUNET_GETOPT_run (const char *binaryOptions,
212 const struct GNUNET_GETOPT_CommandLineOption
213 *allOptions, unsigned int argc, char *const *argv);
217 * Set an option of type 'unsigned long long' from the command line.
218 * A pointer to this function should be passed as part of the
219 * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
220 * of this type. It should be followed by a pointer to a value of
221 * type 'unsigned long long'.
223 * @param ctx command line processing context
224 * @param scls additional closure (will point to the 'unsigned long long')
225 * @param option name of the option
226 * @param value actual value of the option as a string.
227 * @return GNUNET_OK if parsing the value worked
229 int GNUNET_GETOPT_set_ulong (struct GNUNET_GETOPT_CommandLineProcessorContext
230 *ctx, void *scls, const char *option,
235 * Set an option of type 'unsigned long long' from the command line.
236 * A pointer to this function should be passed as part of the
237 * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
238 * of this type. It should be followed by a pointer to a value of
239 * type 'unsigned int'.
241 * @param ctx command line processing context
242 * @param scls additional closure (will point to the 'unsigned int')
243 * @param option name of the option
244 * @param value actual value of the option as a string.
245 * @return GNUNET_OK if parsing the value worked
247 int GNUNET_GETOPT_set_uint (struct GNUNET_GETOPT_CommandLineProcessorContext
248 *ctx, void *scls, const char *option,
253 * Set an option of type 'int' from the command line to 1 if the
254 * given option is present.
255 * A pointer to this function should be passed as part of the
256 * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
257 * of this type. It should be followed by a pointer to a value of
260 * @param ctx command line processing context
261 * @param scls additional closure (will point to the 'int')
262 * @param option name of the option
263 * @param value not used (NULL)
266 int GNUNET_GETOPT_set_one (struct GNUNET_GETOPT_CommandLineProcessorContext
267 *ctx, void *scls, const char *option,
272 * Set an option of type 'char *' from the command line.
273 * A pointer to this function should be passed as part of the
274 * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
275 * of this type. It should be followed by a pointer to a value of
278 * @param ctx command line processing context
279 * @param scls additional closure (will point to the 'char *',
280 * which will be allocated)
281 * @param option name of the option
282 * @param value actual value of the option (a string)
285 int GNUNET_GETOPT_set_string (struct GNUNET_GETOPT_CommandLineProcessorContext
286 *ctx, void *scls, const char *option,
290 * Set an option of type 'unsigned int' from the command line. Each
291 * time the option flag is given, the value is incremented by one.
292 * A pointer to this function should be passed as part of the
293 * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
294 * of this type. It should be followed by a pointer to a value of
297 * @param ctx command line processing context
298 * @param scls additional closure (will point to the 'int')
299 * @param option name of the option
300 * @param value not used (NULL)
304 GNUNET_GETOPT_increment_value (struct
305 GNUNET_GETOPT_CommandLineProcessorContext *ctx,
306 void *scls, const char *option,
310 /* *************** internal prototypes - use macros above! ************* */
313 * Print out details on command line options (implements --help).
315 * @param ctx command line processing context
316 * @param scls additional closure (points to about text)
317 * @param option name of the option
318 * @param value not used (NULL)
319 * @return GNUNET_SYSERR (do not continue)
321 int GNUNET_GETOPT_format_help_ (struct
322 GNUNET_GETOPT_CommandLineProcessorContext
323 *ctx, void *scls, const char *option,
327 * Print out program version (implements --version).
329 * @param ctx command line processing context
330 * @param scls additional closure (points to version string)
331 * @param option name of the option
332 * @param value not used (NULL)
333 * @return GNUNET_SYSERR (do not continue)
335 int GNUNET_GETOPT_print_version_ (struct
336 GNUNET_GETOPT_CommandLineProcessorContext
337 *ctx, void *scls, const char *option,
340 #if 0 /* keep Emacsens' auto-indent happy */
348 /* ifndef GNUNET_GETOPT_LIB_H */
350 /* end of gnunet_getopt_lib.h */