2 This file is part of GNUnet
3 Copyright (C) 2006, 2011 GNUnet e.V.
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 3, 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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file src/util/getopt_helpers.c
23 * @brief implements command line that sets option
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
29 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
33 * Print out program version (implements --version).
35 * @param ctx command line processing context
36 * @param scls additional closure (points to version string)
37 * @param option name of the option
38 * @param value not used (NULL)
39 * @return #GNUNET_NO (do not continue, not an error)
42 GNUNET_GETOPT_print_version_ (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
47 const char *version = scls;
57 * At what offset does the help text start?
62 * Print out details on command line options (implements --help).
64 * @param ctx command line processing context
65 * @param scls additional closure (points to about text)
66 * @param option name of the option
67 * @param value not used (NULL)
68 * @return #GNUNET_NO (do not continue, not an error)
71 GNUNET_GETOPT_format_help_ (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
76 const char *about = scls;
84 const struct GNUNET_GETOPT_CommandLineOption *opt;
88 printf ("%s\n%s\n", ctx->binaryOptions, gettext (about));
90 ("Arguments mandatory for long options are also mandatory for short options.\n"));
93 opt = ctx->allOptions;
94 while (opt[i].description != NULL)
96 if (opt[i].shortName == '\0')
99 printf (" -%c, ", opt[i].shortName);
100 printf ("--%s", opt[i].name);
101 slen = 8 + strlen (opt[i].name);
102 if (opt[i].argumentHelp != NULL)
104 printf ("=%s", opt[i].argumentHelp);
105 slen += 1 + strlen (opt[i].argumentHelp);
109 printf ("\n%*s", BORDER, "");
114 printf ("%*s", (int) (BORDER - slen), "");
117 if (0 < strlen (opt[i].description))
118 trans = gettext (opt[i].description);
124 while (ml - p > 78 - slen)
126 for (j = p + 78 - slen; j > p; j--)
128 if (isspace ((unsigned char) trans[j]))
130 scp = GNUNET_malloc (j - p + 1);
131 memcpy (scp, &trans[p], j - p);
133 printf ("%s\n%*s", scp, BORDER + 2, "");
140 /* could not find space to break line */
141 scp = GNUNET_malloc (78 - slen + 1);
142 memcpy (scp, &trans[p], 78 - slen);
143 scp[78 - slen] = '\0';
144 printf ("%s\n%*s", scp, BORDER + 2, "");
151 printf ("%s\n", &trans[p]);
152 if (strlen (trans) == 0)
156 printf ("Report bugs to gnunet-developers@gnu.org.\n"
157 "GNUnet home page: http://www.gnu.org/software/gnunet/\n"
158 "General help using GNU software: http://www.gnu.org/gethelp/\n");
164 * Set an option of type 'unsigned int' from the command line. Each
165 * time the option flag is given, the value is incremented by one.
166 * A pointer to this function should be passed as part of the
167 * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
168 * of this type. It should be followed by a pointer to a value of
171 * @param ctx command line processing context
172 * @param scls additional closure (will point to the 'int')
173 * @param option name of the option
174 * @param value not used (NULL)
178 GNUNET_GETOPT_increment_value (struct GNUNET_GETOPT_CommandLineProcessorContext
179 *ctx, void *scls, const char *option,
190 * Set an option of type 'int' from the command line to 1 if the
191 * given option is present.
192 * A pointer to this function should be passed as part of the
193 * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
194 * of this type. It should be followed by a pointer to a value of
197 * @param ctx command line processing context
198 * @param scls additional closure (will point to the 'int')
199 * @param option name of the option
200 * @param value not used (NULL)
204 GNUNET_GETOPT_set_one (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
205 void *scls, const char *option, const char *value)
215 * Set an option of type 'char *' from the command line.
216 * A pointer to this function should be passed as part of the
217 * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
218 * of this type. It should be followed by a pointer to a value of
219 * type 'char *', which will be allocated with the requested string.
221 * @param ctx command line processing context
222 * @param scls additional closure (will point to the 'char *',
223 * which will be allocated)
224 * @param option name of the option
225 * @param value actual value of the option (a string)
229 GNUNET_GETOPT_set_string (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
230 void *scls, const char *option, const char *value)
234 GNUNET_assert (value != NULL);
235 GNUNET_free_non_null (*val);
236 *val = GNUNET_strdup (value);
242 GNUNET_GETOPT_set_filename (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
243 void *scls, const char *option, const char *value)
247 GNUNET_assert (value != NULL);
248 GNUNET_free_non_null (*val);
249 *val = GNUNET_STRINGS_filename_expand (value);
254 * Set an option of type 'unsigned long long' from the command line.
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
258 * type 'unsigned long long'.
260 * @param ctx command line processing context
261 * @param scls additional closure (will point to the 'unsigned long long')
262 * @param option name of the option
263 * @param value actual value of the option as a string.
264 * @return #GNUNET_OK if parsing the value worked
267 GNUNET_GETOPT_set_ulong (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
268 void *scls, const char *option, const char *value)
270 unsigned long long *val = scls;
272 if (1 != SSCANF (value, "%llu", val))
274 FPRINTF (stderr, _("You must pass a number to the `%s' option.\n"), option);
275 return GNUNET_SYSERR;
282 * Set an option of type 'struct GNUNET_TIME_Relative' from the command line.
283 * A pointer to this function should be passed as part of the
284 * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
285 * of this type. It should be followed by a pointer to a value of
286 * type 'struct GNUNET_TIME_Relative'.
288 * @param ctx command line processing context
289 * @param scls additional closure (will point to the 'struct GNUNET_TIME_Relative')
290 * @param option name of the option
291 * @param value actual value of the option as a string.
292 * @return #GNUNET_OK if parsing the value worked
295 GNUNET_GETOPT_set_relative_time (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
296 void *scls, const char *option, const char *value)
298 struct GNUNET_TIME_Relative *val = scls;
301 GNUNET_STRINGS_fancy_time_to_relative (value,
304 FPRINTF (stderr, _("You must pass relative time to the `%s' option.\n"), option);
305 return GNUNET_SYSERR;
312 * Set an option of type 'unsigned int' from the command line.
313 * A pointer to this function should be passed as part of the
314 * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
315 * of this type. It should be followed by a pointer to a value of
316 * type 'unsigned int'.
318 * @param ctx command line processing context
319 * @param scls additional closure (will point to the 'unsigned int')
320 * @param option name of the option
321 * @param value actual value of the option as a string.
322 * @return #GNUNET_OK if parsing the value worked
325 GNUNET_GETOPT_set_uint (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
326 void *scls, const char *option, const char *value)
328 unsigned int *val = scls;
330 if (1 != SSCANF (value, "%u", val))
332 FPRINTF (stderr, _("You must pass a number to the `%s' option.\n"), option);
333 return GNUNET_SYSERR;
339 /* end of getopt_helpers.c */