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;
85 const struct GNUNET_OS_ProjectData *pd;
89 printf ("%s\n%s\n", ctx->binaryOptions, gettext (about));
91 ("Arguments mandatory for long options are also mandatory for short options.\n"));
94 opt = ctx->allOptions;
95 while (opt[i].description != NULL)
97 if (opt[i].shortName == '\0')
100 printf (" -%c, ", opt[i].shortName);
101 printf ("--%s", opt[i].name);
102 slen = 8 + strlen (opt[i].name);
103 if (opt[i].argumentHelp != NULL)
105 printf ("=%s", opt[i].argumentHelp);
106 slen += 1 + strlen (opt[i].argumentHelp);
110 printf ("\n%*s", BORDER, "");
115 printf ("%*s", (int) (BORDER - slen), "");
118 if (0 < strlen (opt[i].description))
119 trans = gettext (opt[i].description);
125 while (ml - p > 78 - slen)
127 for (j = p + 78 - slen; j > p; j--)
129 if (isspace ((unsigned char) trans[j]))
131 scp = GNUNET_malloc (j - p + 1);
132 memcpy (scp, &trans[p], j - p);
134 printf ("%s\n%*s", scp, BORDER + 2, "");
141 /* could not find space to break line */
142 scp = GNUNET_malloc (78 - slen + 1);
143 memcpy (scp, &trans[p], 78 - slen);
144 scp[78 - slen] = '\0';
145 printf ("%s\n%*s", scp, BORDER + 2, "");
152 printf ("%s\n", &trans[p]);
153 if (strlen (trans) == 0)
157 pd = GNUNET_OS_project_data_get ();
158 printf ("Report bugs to %s.\n"
159 "GNUnet home page: %s\n"
160 "General help using GNU software: http://www.gnu.org/gethelp/\n",
168 * Set an option of type 'unsigned int' from the command line. Each
169 * time the option flag is given, the value is incremented by one.
170 * A pointer to this function should be passed as part of the
171 * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
172 * of this type. It should be followed by a pointer to a value of
175 * @param ctx command line processing context
176 * @param scls additional closure (will point to the 'int')
177 * @param option name of the option
178 * @param value not used (NULL)
182 GNUNET_GETOPT_increment_value (struct GNUNET_GETOPT_CommandLineProcessorContext
183 *ctx, void *scls, const char *option,
194 * Set an option of type 'int' from the command line to 1 if the
195 * given option is present.
196 * A pointer to this function should be passed as part of the
197 * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
198 * of this type. It should be followed by a pointer to a value of
201 * @param ctx command line processing context
202 * @param scls additional closure (will point to the 'int')
203 * @param option name of the option
204 * @param value not used (NULL)
208 GNUNET_GETOPT_set_one (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
209 void *scls, const char *option, const char *value)
219 * Set an option of type 'char *' from the command line.
220 * A pointer to this function should be passed as part of the
221 * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
222 * of this type. It should be followed by a pointer to a value of
223 * type 'char *', which will be allocated with the requested string.
225 * @param ctx command line processing context
226 * @param scls additional closure (will point to the 'char *',
227 * which will be allocated)
228 * @param option name of the option
229 * @param value actual value of the option (a string)
233 GNUNET_GETOPT_set_string (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
234 void *scls, const char *option, const char *value)
238 GNUNET_assert (value != NULL);
239 GNUNET_free_non_null (*val);
240 *val = GNUNET_strdup (value);
246 GNUNET_GETOPT_set_filename (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
247 void *scls, const char *option, const char *value)
251 GNUNET_assert (value != NULL);
252 GNUNET_free_non_null (*val);
253 *val = GNUNET_STRINGS_filename_expand (value);
258 * Set an option of type 'unsigned long long' from the command line.
259 * A pointer to this function should be passed as part of the
260 * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
261 * of this type. It should be followed by a pointer to a value of
262 * type 'unsigned long long'.
264 * @param ctx command line processing context
265 * @param scls additional closure (will point to the 'unsigned long long')
266 * @param option name of the option
267 * @param value actual value of the option as a string.
268 * @return #GNUNET_OK if parsing the value worked
271 GNUNET_GETOPT_set_ulong (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
272 void *scls, const char *option, const char *value)
274 unsigned long long *val = scls;
276 if (1 != SSCANF (value, "%llu", val))
278 FPRINTF (stderr, _("You must pass a number to the `%s' option.\n"), option);
279 return GNUNET_SYSERR;
286 * Set an option of type 'struct GNUNET_TIME_Relative' from the command line.
287 * A pointer to this function should be passed as part of the
288 * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
289 * of this type. It should be followed by a pointer to a value of
290 * type 'struct GNUNET_TIME_Relative'.
292 * @param ctx command line processing context
293 * @param scls additional closure (will point to the 'struct GNUNET_TIME_Relative')
294 * @param option name of the option
295 * @param value actual value of the option as a string.
296 * @return #GNUNET_OK if parsing the value worked
299 GNUNET_GETOPT_set_relative_time (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
300 void *scls, const char *option, const char *value)
302 struct GNUNET_TIME_Relative *val = scls;
305 GNUNET_STRINGS_fancy_time_to_relative (value,
308 FPRINTF (stderr, _("You must pass relative time to the `%s' option.\n"), option);
309 return GNUNET_SYSERR;
316 * Set an option of type 'unsigned int' from the command line.
317 * A pointer to this function should be passed as part of the
318 * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
319 * of this type. It should be followed by a pointer to a value of
320 * type 'unsigned int'.
322 * @param ctx command line processing context
323 * @param scls additional closure (will point to the 'unsigned int')
324 * @param option name of the option
325 * @param value actual value of the option as a string.
326 * @return #GNUNET_OK if parsing the value worked
329 GNUNET_GETOPT_set_uint (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
330 void *scls, const char *option, const char *value)
332 unsigned int *val = scls;
334 if (1 != SSCANF (value, "%u", val))
336 FPRINTF (stderr, _("You must pass a number to the `%s' option.\n"), option);
337 return GNUNET_SYSERR;
343 /* end of getopt_helpers.c */