From 85b043ef34186a24d960cb32417da2a610eae526 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Wed, 15 Mar 2017 10:26:37 +0100 Subject: add GNUNET_GETOPT_OPTION_SET_ABSOLUTE_TIME --- src/include/gnunet_getopt_lib.h | 18 ++++++++++++ src/util/getopt_helpers.c | 65 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) (limited to 'src') diff --git a/src/include/gnunet_getopt_lib.h b/src/include/gnunet_getopt_lib.h index 0acf15679..bc2e079da 100644 --- a/src/include/gnunet_getopt_lib.h +++ b/src/include/gnunet_getopt_lib.h @@ -316,6 +316,24 @@ GNUNET_GETOPT_OPTION_SET_RELATIVE_TIME (char shortName, struct GNUNET_TIME_Relative *val); +/** + * Allow user to specify a `struct GNUNET_TIME_Absolute` + * (using human-readable "fancy" time). + * + * @param shortName short name of the option + * @param name long name of the option + * @param argumentHelp help text for the option argument + * @param description long help text for the option + * @param[out] val set to the time specified at the command line + */ +struct GNUNET_GETOPT_CommandLineOption +GNUNET_GETOPT_OPTION_SET_ABSOLUTE_TIME (char shortName, + const char *name, + const char *argumentHelp, + const char *description, + struct GNUNET_TIME_Absolute *val); + + /** * Increment @a val each time the option flag is given by one. * diff --git a/src/util/getopt_helpers.c b/src/util/getopt_helpers.c index 9f6f4c764..31fe093a9 100644 --- a/src/util/getopt_helpers.c +++ b/src/util/getopt_helpers.c @@ -646,6 +646,71 @@ GNUNET_GETOPT_OPTION_SET_RELATIVE_TIME (char shortName, } +/** + * Set an option of type 'struct GNUNET_TIME_Absolute' from the command line. + * A pointer to this function should be passed as part of the + * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options + * of this type. It should be followed by a pointer to a value of + * type 'struct GNUNET_TIME_Absolute'. + * + * @param ctx command line processing context + * @param scls additional closure (will point to the 'struct GNUNET_TIME_Relative') + * @param option name of the option + * @param value actual value of the option as a string. + * @return #GNUNET_OK if parsing the value worked + */ +static int +set_absolute_time (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, + void *scls, + const char *option, + const char *value) +{ + struct GNUNET_TIME_Absolute *val = scls; + + if (GNUNET_OK != + GNUNET_STRINGS_fancy_time_to_absolute (value, + val)) + { + FPRINTF (stderr, + _("You must pass absolute time to the `%s' option.\n"), + option); + return GNUNET_SYSERR; + } + return GNUNET_OK; +} + + +/** + * Allow user to specify a `struct GNUNET_TIME_Absolute` + * (using human-readable "fancy" time). + * + * @param shortName short name of the option + * @param name long name of the option + * @param argumentHelp help text for the option argument + * @param description long help text for the option + * @param[out] val set to the time specified at the command line + */ +struct GNUNET_GETOPT_CommandLineOption +GNUNET_GETOPT_OPTION_SET_ABSOLUTE_TIME (char shortName, + const char *name, + const char *argumentHelp, + const char *description, + struct GNUNET_TIME_Absolute *val) +{ + struct GNUNET_GETOPT_CommandLineOption clo = { + .shortName = shortName, + .name = name, + .argumentHelp = argumentHelp, + .description = description, + .require_argument = 1, + .processor = &set_absolute_time, + .scls = (void *) val + }; + + return clo; +} + + /** * Set an option of type 'unsigned int' from the command line. * A pointer to this function should be passed as part of the -- cgit v1.2.3