2 This file is part of GNUnet.
3 (C) 2001, 2002, 2003, 2004, 2005, 2006, 2009 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;
65 struct GNUNET_CONFIGURATION_Handle *cfg;
68 * Original command line
73 * Total number of argv's.
80 unsigned int currentArgument;
85 * @brief Process a command line option
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
94 int (*GNUNET_GETOPT_CommandLineOptionProcessor) (struct
95 GNUNET_GETOPT_CommandLineProcessorContext
101 * @brief Definition of a command line option.
103 struct GNUNET_GETOPT_CommandLineOption
107 * Short name of the option (use '\0' for none).
109 const char shortName;
112 * Long name of the option (may not be NULL)
117 * Name of the argument for the user in help text
119 const char *argumentHelp;
122 * Help text for the option (description)
124 const char *description;
127 * Is an argument required? 0: GNUNET_NO (includes optional), 1: GNUNET_YES.
129 int require_argument;
132 * Handler for the option.
134 GNUNET_GETOPT_CommandLineOptionProcessor processor;
137 * Specific closure to pass to the processor.
144 * Macro defining the option to print the command line
147 * @param about string with brief description of the application
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 }
153 * Macro defining the option to print the version of
156 * @param version string with the version number
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 }
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 }
168 * Set the configuration option for logging.
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 }
174 * Get number of verbose flags
176 #define GNUNET_GETOPT_OPTION_VERBOSE(level) \
177 { 'V', "verbose", (const char *) NULL, gettext_noop("be verbose"), 0, &GNUNET_GETOPT_increment_value, (void *) level }
180 * Get configuration file name
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 }
186 * Marker to end the list of options.
188 #define GNUNET_GETOPT_OPTION_END \
189 { '\0', NULL, NULL, NULL, 0, NULL, NULL }
192 * Parse the command line.
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
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);
207 int GNUNET_GETOPT_set_ulong (struct GNUNET_GETOPT_CommandLineProcessorContext
208 *ctx, void *scls, const char *option,
211 int GNUNET_GETOPT_set_uint (struct GNUNET_GETOPT_CommandLineProcessorContext
212 *ctx, void *scls, const char *option,
215 int GNUNET_GETOPT_set_one (struct GNUNET_GETOPT_CommandLineProcessorContext
216 *ctx, void *scls, const char *option,
219 int GNUNET_GETOPT_set_string (struct GNUNET_GETOPT_CommandLineProcessorContext
220 *ctx, void *scls, const char *option,
224 GNUNET_GETOPT_increment_value (struct
225 GNUNET_GETOPT_CommandLineProcessorContext *ctx,
226 void *scls, const char *option,
229 /* *************** internal prototypes - use macros above! ************* */
231 int GNUNET_GETOPT_format_help_ (struct
232 GNUNET_GETOPT_CommandLineProcessorContext
233 *ctx, void *scls, const char *option,
236 int GNUNET_GETOPT_print_version_ (struct
237 GNUNET_GETOPT_CommandLineProcessorContext
238 *ctx, void *scls, const char *option,
241 #if 0 /* keep Emacsens' auto-indent happy */
249 /* ifndef GNUNET_GETOPT_LIB_H */
251 /* end of gnunet_getopt_lib.h */