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_getopt_lib.h"
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_SYSERR (do not continue)
42 GNUNET_GETOPT_print_version_ (struct GNUNET_GETOPT_CommandLineProcessorContext
43 *ctx, void *scls, const char *option,
46 const char *version = scls;
48 printf ("%s v%s\n", ctx->binaryName, version);
57 * Print out details on command line options (implements --help).
59 * @param ctx command line processing context
60 * @param scls additional closure (points to about text)
61 * @param option name of the option
62 * @param value not used (NULL)
63 * @return GNUNET_SYSERR (do not continue)
66 GNUNET_GETOPT_format_help_ (struct GNUNET_GETOPT_CommandLineProcessorContext
67 *ctx, void *scls, const char *option,
70 const char *about = scls;
78 const struct GNUNET_GETOPT_CommandLineOption *opt;
80 printf ("%s\n%s\n", ctx->binaryOptions, gettext (about));
82 ("Arguments mandatory for long options are also mandatory for short options.\n"));
84 opt = ctx->allOptions;
85 while (opt[i].description != NULL)
87 if (opt[i].shortName == '\0')
90 printf (" -%c, ", opt[i].shortName);
91 printf ("--%s", opt[i].name);
92 slen = 8 + strlen (opt[i].name);
93 if (opt[i].argumentHelp != NULL)
95 printf ("=%s", opt[i].argumentHelp);
96 slen += 1 + strlen (opt[i].argumentHelp);
100 printf ("\n%*s", BORDER, "");
105 printf ("%*s", (int) (BORDER - slen), "");
108 if (0 < strlen (opt[i].description))
109 trans = gettext (opt[i].description);
115 while (ml - p > 78 - slen)
117 for (j = p + 78 - slen; j > p; j--)
119 if (isspace ((unsigned char) trans[j]))
121 scp = GNUNET_malloc (j - p + 1);
122 memcpy (scp, &trans[p], j - p);
124 printf ("%s\n%*s", scp, BORDER + 2, "");
131 /* could not find space to break line */
132 scp = GNUNET_malloc (78 - slen + 1);
133 memcpy (scp, &trans[p], 78 - slen);
134 scp[78 - slen] = '\0';
135 printf ("%s\n%*s", scp, BORDER + 2, "");
142 printf ("%s\n", &trans[p]);
143 if (strlen (trans) == 0)
147 printf ("Report bugs to gnunet-developers@gnu.org.\n"
148 "GNUnet home page: http://www.gnu.org/software/gnunet/\n"
149 "General help using GNU software: http://www.gnu.org/gethelp/\n");
150 return GNUNET_SYSERR;
155 * Set an option of type 'unsigned int' from the command line. Each
156 * time the option flag is given, the value is incremented by one.
157 * A pointer to this function should be passed as part of the
158 * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
159 * of this type. It should be followed by a pointer to a value of
162 * @param ctx command line processing context
163 * @param scls additional closure (will point to the 'int')
164 * @param option name of the option
165 * @param value not used (NULL)
169 GNUNET_GETOPT_increment_value (struct GNUNET_GETOPT_CommandLineProcessorContext
170 *ctx, void *scls, const char *option,
181 * Set an option of type 'int' from the command line to 1 if the
182 * given option is present.
183 * A pointer to this function should be passed as part of the
184 * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
185 * of this type. It should be followed by a pointer to a value of
188 * @param ctx command line processing context
189 * @param scls additional closure (will point to the 'int')
190 * @param option name of the option
191 * @param value not used (NULL)
195 GNUNET_GETOPT_set_one (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
196 void *scls, const char *option, const char *value)
206 * Set an option of type 'char *' from the command line.
207 * A pointer to this function should be passed as part of the
208 * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
209 * of this type. It should be followed by a pointer to a value of
212 * @param ctx command line processing context
213 * @param scls additional closure (will point to the 'char *',
214 * which will be allocated)
215 * @param option name of the option
216 * @param value actual value of the option (a string)
220 GNUNET_GETOPT_set_string (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
221 void *scls, const char *option, const char *value)
225 GNUNET_assert (value != NULL);
226 GNUNET_free_non_null (*val);
227 *val = GNUNET_strdup (value);
233 * Set an option of type 'unsigned long long' from the command line.
234 * A pointer to this function should be passed as part of the
235 * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
236 * of this type. It should be followed by a pointer to a value of
237 * type 'unsigned long long'.
239 * @param ctx command line processing context
240 * @param scls additional closure (will point to the 'unsigned long long')
241 * @param option name of the option
242 * @param value actual value of the option as a string.
243 * @return GNUNET_OK if parsing the value worked
246 GNUNET_GETOPT_set_ulong (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
247 void *scls, const char *option, const char *value)
249 unsigned long long *val = scls;
251 if (1 != SSCANF (value, "%llu", val))
253 fprintf (stderr, _("You must pass a number to the `%s' option.\n"), option);
254 return GNUNET_SYSERR;
261 * Set an option of type 'unsigned long long' from the command line.
262 * A pointer to this function should be passed as part of the
263 * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
264 * of this type. It should be followed by a pointer to a value of
265 * type 'unsigned int'.
267 * @param ctx command line processing context
268 * @param scls additional closure (will point to the 'unsigned int')
269 * @param option name of the option
270 * @param value actual value of the option as a string.
271 * @return GNUNET_OK if parsing the value worked
274 GNUNET_GETOPT_set_uint (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
275 void *scls, const char *option, const char *value)
277 unsigned int *val = scls;
279 if (1 != SSCANF (value, "%u", val))
281 fprintf (stderr, _("You must pass a number to the `%s' option.\n"), option);
282 return GNUNET_SYSERR;
288 /* end of getopt_helpers.c */