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-getopt", __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 print_version (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
47 const char *version = scls;
57 * Define the option to print the version of
58 * the application (-v option)
60 * @param version string with the version number
62 struct GNUNET_GETOPT_CommandLineOption
63 GNUNET_GETOPT_OPTION_VERSION (const char *version)
65 struct GNUNET_GETOPT_CommandLineOption clo = {
68 .description = gettext_noop("print the version number"),
69 .processor = &print_version,
70 .scls = (void *) version
77 * At what offset does the help text start?
82 * Print out details on command line options (implements --help).
84 * @param ctx command line processing context
85 * @param scls additional closure (points to about text)
86 * @param option name of the option
87 * @param value not used (NULL)
88 * @return #GNUNET_NO (do not continue, not an error)
91 format_help (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
96 const char *about = scls;
104 const struct GNUNET_GETOPT_CommandLineOption *opt;
105 const struct GNUNET_OS_ProjectData *pd;
109 printf ("%s\n%s\n", ctx->binaryOptions, gettext (about));
111 ("Arguments mandatory for long options are also mandatory for short options.\n"));
114 opt = ctx->allOptions;
115 while (opt[i].description != NULL)
117 if (opt[i].shortName == '\0')
120 printf (" -%c, ", opt[i].shortName);
121 printf ("--%s", opt[i].name);
122 slen = 8 + strlen (opt[i].name);
123 if (opt[i].argumentHelp != NULL)
125 printf ("=%s", opt[i].argumentHelp);
126 slen += 1 + strlen (opt[i].argumentHelp);
130 printf ("\n%*s", BORDER, "");
135 printf ("%*s", (int) (BORDER - slen), "");
138 if (0 < strlen (opt[i].description))
139 trans = gettext (opt[i].description);
145 while (ml - p > 78 - slen)
147 for (j = p + 78 - slen; j > p; j--)
149 if (isspace ((unsigned char) trans[j]))
151 scp = GNUNET_malloc (j - p + 1);
152 GNUNET_memcpy (scp, &trans[p], j - p);
154 printf ("%s\n%*s", scp, BORDER + 2, "");
161 /* could not find space to break line */
162 scp = GNUNET_malloc (78 - slen + 1);
163 GNUNET_memcpy (scp, &trans[p], 78 - slen);
164 scp[78 - slen] = '\0';
165 printf ("%s\n%*s", scp, BORDER + 2, "");
172 printf ("%s\n", &trans[p]);
173 if (strlen (trans) == 0)
177 pd = GNUNET_OS_project_data_get ();
178 printf ("Report bugs to %s.\n"
179 "GNUnet home page: %s\n"
180 "General help using GNU software: http://www.gnu.org/gethelp/\n",
188 * Defining the option to print the command line
189 * help text (-h option).
191 * @param about string with brief description of the application
193 struct GNUNET_GETOPT_CommandLineOption
194 GNUNET_GETOPT_OPTION_HELP (const char *about)
196 struct GNUNET_GETOPT_CommandLineOption clo = {
199 .description = gettext_noop("print this help"),
200 .processor = format_help,
201 .scls = (void *) about
209 * Set an option of type 'unsigned int' from the command line. Each
210 * time the option flag is given, the value is incremented by one.
211 * A pointer to this function should be passed as part of the
212 * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
213 * of this type. It should be followed by a pointer to a value of
216 * @param ctx command line processing context
217 * @param scls additional closure (will point to the 'unsigned int')
218 * @param option name of the option
219 * @param value not used (NULL)
223 increment_value (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
228 unsigned int *val = scls;
236 * Increment @a val each time the option flag is given by one.
238 * @param shortName short name of the option
239 * @param name long name of the option
240 * @param argumentHelp help text for the option argument
241 * @param description long help text for the option
242 * @param[out] val increment by 1 each time the option is present
244 struct GNUNET_GETOPT_CommandLineOption
245 GNUNET_GETOPT_OPTION_INCREMENT_VALUE (char shortName,
247 const char *description,
250 struct GNUNET_GETOPT_CommandLineOption clo = {
251 .shortName = shortName,
253 .description = description,
254 .processor = &increment_value,
263 * Define the '-V' verbosity option. Using the option more
264 * than once increments @a level each time.
266 * @param[out] level set to the verbosity level
268 struct GNUNET_GETOPT_CommandLineOption
269 GNUNET_GETOPT_OPTION_VERBOSE (unsigned int *level)
271 struct GNUNET_GETOPT_CommandLineOption clo = {
274 .description = gettext_noop("be verbose"),
275 .processor = &increment_value,
276 .scls = (void *) level
284 * Set an option of type 'int' from the command line to 1 if the
285 * given option is present.
286 * A pointer to this function should be passed as part of the
287 * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
288 * of this type. It should be followed by a pointer to a value of
291 * @param ctx command line processing context
292 * @param scls additional closure (will point to the 'int')
293 * @param option name of the option
294 * @param value not used (NULL)
298 set_one (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
311 * Allow user to specify a flag (which internally means setting
312 * an integer to 1/#GNUNET_YES/#GNUNET_OK.
314 * @param shortName short name of the option
315 * @param name long name of the option
316 * @param argumentHelp help text for the option argument
317 * @param description long help text for the option
318 * @param[out] val set to 1 if the option is present
320 struct GNUNET_GETOPT_CommandLineOption
321 GNUNET_GETOPT_OPTION_SET_ONE (char shortName,
323 const char *description,
326 struct GNUNET_GETOPT_CommandLineOption clo = {
327 .shortName = shortName,
329 .description = description,
330 .processor = &set_one,
339 * Set an option of type 'char *' from the command line.
340 * A pointer to this function should be passed as part of the
341 * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
342 * of this type. It should be followed by a pointer to a value of
343 * type 'char *', which will be allocated with the requested string.
345 * @param ctx command line processing context
346 * @param scls additional closure (will point to the 'char *',
347 * which will be allocated)
348 * @param option name of the option
349 * @param value actual value of the option (a string)
353 set_string (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
360 GNUNET_assert (NULL != value);
361 GNUNET_free_non_null (*val);
362 *val = GNUNET_strdup (value);
368 * Allow user to specify a string.
370 * @param shortName short name of the option
371 * @param name long name of the option
372 * @param argumentHelp help text for the option argument
373 * @param description long help text for the option
374 * @param[out] str set to the string
376 struct GNUNET_GETOPT_CommandLineOption
377 GNUNET_GETOPT_OPTION_STRING (char shortName,
379 const char *argumentHelp,
380 const char *description,
383 struct GNUNET_GETOPT_CommandLineOption clo = {
384 .shortName = shortName,
386 .argumentHelp = argumentHelp,
387 .description = description,
388 .require_argument = 1,
389 .processor = &set_string,
398 * Define the '-L' log level option. Note that we do not check
399 * that the log level is valid here.
401 * @param[out] level set to the log level
403 struct GNUNET_GETOPT_CommandLineOption
404 GNUNET_GETOPT_OPTION_LOGLEVEL (char **level)
406 struct GNUNET_GETOPT_CommandLineOption clo = {
409 .argumentHelp = "LOGLEVEL",
410 .description = gettext_noop("configure logging to use LOGLEVEL"),
411 .require_argument = 1,
412 .processor = &set_string,
413 .scls = (void *) level
421 * Set an option of type 'char *' from the command line with
422 * filename expansion a la #GNUNET_STRINGS_filename_expand().
424 * @param ctx command line processing context
425 * @param scls additional closure (will point to the `char *`,
426 * which will be allocated)
427 * @param option name of the option
428 * @param value actual value of the option (a string)
432 set_filename (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
439 GNUNET_assert (NULL != value);
440 GNUNET_free_non_null (*val);
441 *val = GNUNET_STRINGS_filename_expand (value);
447 * Allow user to specify a filename (automatically path expanded).
449 * @param shortName short name of the option
450 * @param name long name of the option
451 * @param argumentHelp help text for the option argument
452 * @param description long help text for the option
453 * @param[out] str set to the string
455 struct GNUNET_GETOPT_CommandLineOption
456 GNUNET_GETOPT_OPTION_FILENAME (char shortName,
458 const char *argumentHelp,
459 const char *description,
462 struct GNUNET_GETOPT_CommandLineOption clo = {
463 .shortName = shortName,
465 .argumentHelp = argumentHelp,
466 .description = description,
467 .require_argument = 1,
468 .processor = &set_filename,
477 * Allow user to specify log file name (-l option)
479 * @param[out] logfn set to the name of the logfile
481 struct GNUNET_GETOPT_CommandLineOption
482 GNUNET_GETOPT_OPTION_LOGFILE (char **logfn)
484 struct GNUNET_GETOPT_CommandLineOption clo = {
487 .argumentHelp = "FILENAME",
488 .description = gettext_noop ("configure logging to write logs to FILENAME"),
489 .require_argument = 1,
490 .processor = &set_filename,
491 .scls = (void *) logfn
499 * Allow user to specify configuration file name (-c option)
501 * @param[out] fn set to the name of the configuration file
503 struct GNUNET_GETOPT_CommandLineOption
504 GNUNET_GETOPT_OPTION_CFG_FILE (char **fn)
506 struct GNUNET_GETOPT_CommandLineOption clo = {
509 .argumentHelp = "FILENAME",
510 .description = gettext_noop("use configuration file FILENAME"),
511 .require_argument = 1,
512 .processor = &set_filename,
521 * Set an option of type 'unsigned long long' from the command line.
522 * A pointer to this function should be passed as part of the
523 * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
524 * of this type. It should be followed by a pointer to a value of
525 * type 'unsigned long long'.
527 * @param ctx command line processing context
528 * @param scls additional closure (will point to the 'unsigned long long')
529 * @param option name of the option
530 * @param value actual value of the option as a string.
531 * @return #GNUNET_OK if parsing the value worked
534 set_ulong (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
539 unsigned long long *val = scls;
541 if (1 != SSCANF (value,
546 _("You must pass a number to the `%s' option.\n"),
548 return GNUNET_SYSERR;
555 * Allow user to specify an `unsigned long long`
557 * @param shortName short name of the option
558 * @param name long name of the option
559 * @param argumentHelp help text for the option argument
560 * @param description long help text for the option
561 * @param[out] val set to the value specified at the command line
563 struct GNUNET_GETOPT_CommandLineOption
564 GNUNET_GETOPT_OPTION_SET_ULONG (char shortName,
566 const char *argumentHelp,
567 const char *description,
568 unsigned long long *val)
570 struct GNUNET_GETOPT_CommandLineOption clo = {
571 .shortName = shortName,
573 .argumentHelp = argumentHelp,
574 .description = description,
575 .require_argument = 1,
576 .processor = &set_ulong,
585 * Set an option of type 'struct GNUNET_TIME_Relative' from the command line.
586 * A pointer to this function should be passed as part of the
587 * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
588 * of this type. It should be followed by a pointer to a value of
589 * type 'struct GNUNET_TIME_Relative'.
591 * @param ctx command line processing context
592 * @param scls additional closure (will point to the 'struct GNUNET_TIME_Relative')
593 * @param option name of the option
594 * @param value actual value of the option as a string.
595 * @return #GNUNET_OK if parsing the value worked
598 set_relative_time (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
603 struct GNUNET_TIME_Relative *val = scls;
606 GNUNET_STRINGS_fancy_time_to_relative (value,
610 _("You must pass relative time to the `%s' option.\n"),
612 return GNUNET_SYSERR;
619 * Allow user to specify a `struct GNUNET_TIME_Relative`
620 * (using human-readable "fancy" time).
622 * @param shortName short name of the option
623 * @param name long name of the option
624 * @param argumentHelp help text for the option argument
625 * @param description long help text for the option
626 * @param[out] val set to the time specified at the command line
628 struct GNUNET_GETOPT_CommandLineOption
629 GNUNET_GETOPT_OPTION_SET_RELATIVE_TIME (char shortName,
631 const char *argumentHelp,
632 const char *description,
633 struct GNUNET_TIME_Relative *val)
635 struct GNUNET_GETOPT_CommandLineOption clo = {
636 .shortName = shortName,
638 .argumentHelp = argumentHelp,
639 .description = description,
640 .require_argument = 1,
641 .processor = &set_relative_time,
650 * Set an option of type 'struct GNUNET_TIME_Absolute' from the command line.
651 * A pointer to this function should be passed as part of the
652 * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
653 * of this type. It should be followed by a pointer to a value of
654 * type 'struct GNUNET_TIME_Absolute'.
656 * @param ctx command line processing context
657 * @param scls additional closure (will point to the `struct GNUNET_TIME_Absolute`)
658 * @param option name of the option
659 * @param value actual value of the option as a string.
660 * @return #GNUNET_OK if parsing the value worked
663 set_absolute_time (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
668 struct GNUNET_TIME_Absolute *val = scls;
671 GNUNET_STRINGS_fancy_time_to_absolute (value,
675 _("You must pass absolute time to the `%s' option.\n"),
677 return GNUNET_SYSERR;
684 * Allow user to specify a `struct GNUNET_TIME_Absolute`
685 * (using human-readable "fancy" time).
687 * @param shortName short name of the option
688 * @param name long name of the option
689 * @param argumentHelp help text for the option argument
690 * @param description long help text for the option
691 * @param[out] val set to the time specified at the command line
693 struct GNUNET_GETOPT_CommandLineOption
694 GNUNET_GETOPT_OPTION_SET_ABSOLUTE_TIME (char shortName,
696 const char *argumentHelp,
697 const char *description,
698 struct GNUNET_TIME_Absolute *val)
700 struct GNUNET_GETOPT_CommandLineOption clo = {
701 .shortName = shortName,
703 .argumentHelp = argumentHelp,
704 .description = description,
705 .require_argument = 1,
706 .processor = &set_absolute_time,
715 * Set an option of type 'unsigned int' from the command line.
716 * A pointer to this function should be passed as part of the
717 * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
718 * of this type. It should be followed by a pointer to a value of
719 * type 'unsigned int'.
721 * @param ctx command line processing context
722 * @param scls additional closure (will point to the 'unsigned int')
723 * @param option name of the option
724 * @param value actual value of the option as a string.
725 * @return #GNUNET_OK if parsing the value worked
728 set_uint (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
733 unsigned int *val = scls;
735 if (1 != SSCANF (value,
740 _("You must pass a number to the `%s' option.\n"),
742 return GNUNET_SYSERR;
749 * Allow user to specify an unsigned integer.
751 * @param shortName short name of the option
752 * @param name long name of the option
753 * @param argumentHelp help text for the option argument
754 * @param description long help text for the option
755 * @param[out] val set to the value specified at the command line
757 struct GNUNET_GETOPT_CommandLineOption
758 GNUNET_GETOPT_OPTION_SET_UINT (char shortName,
760 const char *argumentHelp,
761 const char *description,
764 struct GNUNET_GETOPT_CommandLineOption clo = {
765 .shortName = shortName,
767 .argumentHelp = argumentHelp,
768 .description = description,
769 .require_argument = 1,
770 .processor = &set_uint,
779 * Closure for #set_base32().
784 * Value to initialize (already allocated)
789 * Number of bytes expected for @e val.
796 * Set an option of type 'unsigned int' from the command line.
797 * A pointer to this function should be passed as part of the
798 * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
799 * of this type. It should be followed by a pointer to a value of
800 * type 'unsigned int'.
802 * @param ctx command line processing context
803 * @param scls additional closure (will point to the 'unsigned int')
804 * @param option name of the option
805 * @param value actual value of the option as a string.
806 * @return #GNUNET_OK if parsing the value worked
809 set_base32 (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
814 struct Base32Context *bc = scls;
817 GNUNET_STRINGS_string_to_data (value,
823 _("Argument `%s' malformed. Expected base32 (Crockford) encoded value.\n"),
825 return GNUNET_SYSERR;
832 * Helper function to clean up after
833 * #GNUNET_GETOPT_OPTION_SET_BASE32_FIXED_SIZE.
835 * @param cls value to GNUNET_free()
845 * Allow user to specify a binary value using Crockford
848 * @param shortName short name of the option
849 * @param name long name of the option
850 * @param argumentHelp help text for the option argument
851 * @param description long help text for the option
852 * @param[out] val binary value decoded from Crockford Base32-encoded argument
853 * @param val_size size of @a val in bytes
855 struct GNUNET_GETOPT_CommandLineOption
856 GNUNET_GETOPT_OPTION_SET_BASE32_FIXED_SIZE (char shortName,
858 const char *argumentHelp,
859 const char *description,
863 struct Base32Context *bc = GNUNET_new (struct Base32Context);
864 struct GNUNET_GETOPT_CommandLineOption clo = {
865 .shortName = shortName,
867 .argumentHelp = argumentHelp,
868 .description = description,
869 .require_argument = 1,
870 .processor = &set_base32,
876 bc->val_size = val_size;
882 * Make the given option mandatory.
884 * @param opt option to modify
885 * @return @a opt with the mandatory flag set.
887 struct GNUNET_GETOPT_CommandLineOption
888 GNUNET_GETOPT_OPTION_MANDATORY (struct GNUNET_GETOPT_CommandLineOption opt)
890 opt.option_mandatory = 1;
895 /* end of getopt_helpers.c */