2 This file is part of GNUnet
3 (C) 2006, 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 src/util/getopt_helpers.c
23 * @brief implements command line that sets option
24 * @author Christian Grothoff
28 #include "gnunet_common.h"
29 #include "gnunet_util_lib.h"
31 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
35 * Print out program version (implements --version).
37 * @param ctx command line processing context
38 * @param scls additional closure (points to version string)
39 * @param option name of the option
40 * @param value not used (NULL)
41 * @return GNUNET_NO (do not continue, not an error)
44 GNUNET_GETOPT_print_version_ (struct GNUNET_GETOPT_CommandLineProcessorContext
45 *ctx, void *scls, const char *option,
48 const char *version = scls;
50 printf ("%s v%s\n", ctx->binaryName, version);
59 * Print out details on command line options (implements --help).
61 * @param ctx command line processing context
62 * @param scls additional closure (points to about text)
63 * @param option name of the option
64 * @param value not used (NULL)
65 * @return GNUNET_NO (do not continue, not an error)
68 GNUNET_GETOPT_format_help_ (struct GNUNET_GETOPT_CommandLineProcessorContext
69 *ctx, void *scls, const char *option,
72 const char *about = scls;
80 const struct GNUNET_GETOPT_CommandLineOption *opt;
84 printf ("%s\n%s\n", ctx->binaryOptions, gettext (about));
86 ("Arguments mandatory for long options are also mandatory for short options.\n"));
89 opt = ctx->allOptions;
90 while (opt[i].description != NULL)
92 if (opt[i].shortName == '\0')
95 printf (" -%c, ", opt[i].shortName);
96 printf ("--%s", opt[i].name);
97 slen = 8 + strlen (opt[i].name);
98 if (opt[i].argumentHelp != NULL)
100 printf ("=%s", opt[i].argumentHelp);
101 slen += 1 + strlen (opt[i].argumentHelp);
105 printf ("\n%*s", BORDER, "");
110 printf ("%*s", (int) (BORDER - slen), "");
113 if (0 < strlen (opt[i].description))
114 trans = gettext (opt[i].description);
120 while (ml - p > 78 - slen)
122 for (j = p + 78 - slen; j > p; j--)
124 if (isspace ((unsigned char) trans[j]))
126 scp = GNUNET_malloc (j - p + 1);
127 memcpy (scp, &trans[p], j - p);
129 printf ("%s\n%*s", scp, BORDER + 2, "");
136 /* could not find space to break line */
137 scp = GNUNET_malloc (78 - slen + 1);
138 memcpy (scp, &trans[p], 78 - slen);
139 scp[78 - slen] = '\0';
140 printf ("%s\n%*s", scp, BORDER + 2, "");
147 printf ("%s\n", &trans[p]);
148 if (strlen (trans) == 0)
152 printf ("Report bugs to gnunet-developers@gnu.org.\n"
153 "GNUnet home page: http://www.gnu.org/software/gnunet/\n"
154 "General help using GNU software: http://www.gnu.org/gethelp/\n");
160 * Set an option of type 'unsigned int' from the command line. Each
161 * time the option flag is given, the value is incremented by one.
162 * A pointer to this function should be passed as part of the
163 * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
164 * of this type. It should be followed by a pointer to a value of
167 * @param ctx command line processing context
168 * @param scls additional closure (will point to the 'int')
169 * @param option name of the option
170 * @param value not used (NULL)
174 GNUNET_GETOPT_increment_value (struct GNUNET_GETOPT_CommandLineProcessorContext
175 *ctx, void *scls, const char *option,
186 * Set an option of type 'int' from the command line to 1 if the
187 * given option is present.
188 * A pointer to this function should be passed as part of the
189 * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
190 * of this type. It should be followed by a pointer to a value of
193 * @param ctx command line processing context
194 * @param scls additional closure (will point to the 'int')
195 * @param option name of the option
196 * @param value not used (NULL)
200 GNUNET_GETOPT_set_one (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
201 void *scls, const char *option, const char *value)
211 * Set an option of type 'char *' from the command line.
212 * A pointer to this function should be passed as part of the
213 * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
214 * of this type. It should be followed by a pointer to a value of
215 * type 'char *', which will be allocated with the requested string.
217 * @param ctx command line processing context
218 * @param scls additional closure (will point to the 'char *',
219 * which will be allocated)
220 * @param option name of the option
221 * @param value actual value of the option (a string)
225 GNUNET_GETOPT_set_string (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
226 void *scls, const char *option, const char *value)
230 GNUNET_assert (value != NULL);
231 GNUNET_free_non_null (*val);
232 *val = GNUNET_strdup (value);
238 GNUNET_GETOPT_set_filename (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
239 void *scls, const char *option, const char *value)
243 GNUNET_assert (value != NULL);
244 GNUNET_free_non_null (*val);
245 *val = GNUNET_STRINGS_filename_expand (value);
250 * Set an option of type 'unsigned long long' from the command line.
251 * A pointer to this function should be passed as part of the
252 * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
253 * of this type. It should be followed by a pointer to a value of
254 * type 'unsigned long long'.
256 * @param ctx command line processing context
257 * @param scls additional closure (will point to the 'unsigned long long')
258 * @param option name of the option
259 * @param value actual value of the option as a string.
260 * @return GNUNET_OK if parsing the value worked
263 GNUNET_GETOPT_set_ulong (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
264 void *scls, const char *option, const char *value)
266 unsigned long long *val = scls;
268 if (1 != SSCANF (value, "%llu", val))
270 FPRINTF (stderr, _("You must pass a number to the `%s' option.\n"), option);
271 return GNUNET_SYSERR;
278 * Set an option of type 'struct GNUNET_TIME_Relative' from the command line.
279 * A pointer to this function should be passed as part of the
280 * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
281 * of this type. It should be followed by a pointer to a value of
282 * type 'struct GNUNET_TIME_Relative'.
284 * @param ctx command line processing context
285 * @param scls additional closure (will point to the 'struct GNUNET_TIME_Relative')
286 * @param option name of the option
287 * @param value actual value of the option as a string.
288 * @return GNUNET_OK if parsing the value worked
291 GNUNET_GETOPT_set_relative_time (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
292 void *scls, const char *option, const char *value)
294 struct GNUNET_TIME_Relative *val = scls;
297 GNUNET_STRINGS_fancy_time_to_relative (value,
300 FPRINTF (stderr, _("You must pass relative time to the `%s' option.\n"), option);
301 return GNUNET_SYSERR;
308 * Set an option of type 'unsigned int' from the command line.
309 * A pointer to this function should be passed as part of the
310 * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
311 * of this type. It should be followed by a pointer to a value of
312 * type 'unsigned int'.
314 * @param ctx command line processing context
315 * @param scls additional closure (will point to the 'unsigned int')
316 * @param option name of the option
317 * @param value actual value of the option as a string.
318 * @return GNUNET_OK if parsing the value worked
321 GNUNET_GETOPT_set_uint (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
322 void *scls, const char *option, const char *value)
324 unsigned int *val = scls;
326 if (1 != SSCANF (value, "%u", val))
328 FPRINTF (stderr, _("You must pass a number to the `%s' option.\n"), option);
329 return GNUNET_SYSERR;
335 /* end of getopt_helpers.c */