GNUnet 0.22.2
Getopt library

Command line parsing and –help formatting. More...

Collaboration diagram for Getopt library:

Data Structures

struct  GNUNET_GETOPT_CommandLineProcessorContext
 General context for command line processors. More...
 
struct  GNUNET_GETOPT_CommandLineOption
 Definition of a command line option. More...
 

Macros

#define GNUNET_GETOPT_option_base32_auto(shortName, name, argumentHelp, description, val)
 Allow user to specify a binary value using Crockford Base32 encoding where the size of the binary value is automatically determined from its type. More...
 
#define GNUNET_GETOPT_OPTION_END
 Marker for the end of the list of options. More...
 

Typedefs

typedef int(* GNUNET_GETOPT_CommandLineOptionProcessor) (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, void *scls, const char *option, const char *value)
 Process a command line option. More...
 

Functions

struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_help (const struct GNUNET_OS_ProjectData *pd, const char *about)
 Defining the option to print the command line help text (-h option). More...
 
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_timetravel (char shortName, const char *name)
 Allow user to specify a long long with an offset to add to the current system time to construct the time seen by the application. More...
 
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_version (const char *version)
 Define the option to print the version of the application (-v option) More...
 
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_logfile (char **logfn)
 Allow user to specify log file name (-l option) More...
 
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_string (char shortName, const char *name, const char *argumentHelp, const char *description, char **str)
 Allow user to specify a string. More...
 
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_filename (char shortName, const char *name, const char *argumentHelp, const char *description, char **str)
 Allow user to specify a filename (automatically path expanded). More...
 
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_base32_fixed_size (char shortName, const char *name, const char *argumentHelp, const char *description, void *val, size_t val_size)
 Allow user to specify a binary value using Crockford Base32 encoding. More...
 
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_flag (char shortName, const char *name, const char *description, int *val)
 Allow user to specify a flag (which internally means setting an integer to 1/GNUNET_YES/GNUNET_OK. More...
 
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_uint (char shortName, const char *name, const char *argumentHelp, const char *description, unsigned int *val)
 Allow user to specify an unsigned int. More...
 
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_uint16 (char shortName, const char *name, const char *argumentHelp, const char *description, uint16_t *val)
 Allow user to specify an uint16_t. More...
 
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_ulong (char shortName, const char *name, const char *argumentHelp, const char *description, unsigned long long *val)
 Allow user to specify an unsigned long long. More...
 
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_relative_time (char shortName, const char *name, const char *argumentHelp, const char *description, struct GNUNET_TIME_Relative *val)
 Allow user to specify a struct GNUNET_TIME_Relative (using human-readable "fancy" time). More...
 
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_absolute_time (char shortName, const char *name, const char *argumentHelp, const char *description, struct GNUNET_TIME_Absolute *val)
 Allow user to specify a struct GNUNET_TIME_Absolute (using human-readable "fancy" time). More...
 
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_timestamp (char shortName, const char *name, const char *argumentHelp, const char *description, struct GNUNET_TIME_Timestamp *val)
 Allow user to specify a struct GNUNET_TIME_Timestamp (using human-readable "fancy" time). More...
 
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_increment_uint (char shortName, const char *name, const char *description, unsigned int *val)
 Increment val each time the option flag is given by one. More...
 
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_loglevel (char **level)
 Define the '-L' log level option. More...
 
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_verbose (unsigned int *level)
 Define the '-V' verbosity option. More...
 
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_cfgfile (char **fn)
 Allow user to specify configuration file name (-c option) More...
 
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_mandatory (struct GNUNET_GETOPT_CommandLineOption opt)
 Make the given option mandatory. More...
 
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_exclusive (struct GNUNET_GETOPT_CommandLineOption opt)
 Make the given option mutually exclusive with other options. More...
 
int GNUNET_GETOPT_run (const char *binaryOptions, const struct GNUNET_GETOPT_CommandLineOption *allOptions, unsigned int argc, char *const *argv)
 Parse the command line. More...
 

Detailed Description

Command line parsing and –help formatting.

Macro Definition Documentation

◆ GNUNET_GETOPT_option_base32_auto

#define GNUNET_GETOPT_option_base32_auto (   shortName,
  name,
  argumentHelp,
  description,
  val 
)
Value:
name, \
argumentHelp, \
description, \
val, \
sizeof(*val))
static char * name
Name (label) of the records to list.
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_base32_fixed_size(char shortName, const char *name, const char *argumentHelp, const char *description, void *val, size_t val_size)
Allow user to specify a binary value using Crockford Base32 encoding.

Allow user to specify a binary value using Crockford Base32 encoding where the size of the binary value is automatically determined from its type.

Parameters
shortNameshort name of the option
namelong name of the option
argumentHelphelp text for the option argument
descriptionlong help text for the option
[out]valbinary value decoded from Crockford Base32-encoded argument; size is determined by type (sizeof (*val)).

Definition at line 275 of file gnunet_getopt_lib.h.

◆ GNUNET_GETOPT_OPTION_END

#define GNUNET_GETOPT_OPTION_END
Value:
{ \
'\0', NULL, NULL, NULL, 0, 0, 0, NULL, NULL, NULL \
}

Marker for the end of the list of options.

Definition at line 476 of file gnunet_getopt_lib.h.

Typedef Documentation

◆ GNUNET_GETOPT_CommandLineOptionProcessor

typedef int(* GNUNET_GETOPT_CommandLineOptionProcessor) (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, void *scls, const char *option, const char *value)

Process a command line option.

Parameters
ctxcontext for all options
sclsspecific closure (for this processor)
optionlong name of the option (e.g. "config" for –config)
valueargument, NULL if none was given
Returns
GNUNET_OK to continue processing other options, GNUNET_SYSERR to abort

Definition at line 99 of file gnunet_getopt_lib.h.

Function Documentation

◆ GNUNET_GETOPT_option_help()

struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_help ( const struct GNUNET_OS_ProjectData pd,
const char *  about 
)

Defining the option to print the command line help text (-h option).

Parameters
pdproject data to determine details about the application
aboutstring with brief description of the application

Definition at line 200 of file getopt_helpers.c.

202{
204 .shortName = 'h',
205 .name = "help",
206 .argumentHelp = about,
207 .description = gettext_noop (
208 "print this help"),
209 .option_exclusive = 1,
210 .processor = &format_help,
211 .scls = (void *) pd,
212 };
213
214 return clo;
215}
static enum GNUNET_GenericReturnValue format_help(struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, void *scls, const char *option, const char *value)
Print out details on command line options (implements –help).
#define gettext_noop(String)
Definition: gettext.h:74
Definition of a command line option.
const char shortName
Short name of the option.

References format_help(), gettext_noop, and GNUNET_GETOPT_CommandLineOption::shortName.

Referenced by GNUNET_PROGRAM_conf_and_options(), GNUNET_PROGRAM_run2(), GNUNET_SERVICE_run_(), and main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_GETOPT_option_timetravel()

struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_timetravel ( char  shortName,
const char *  name 
)

Allow user to specify a long long with an offset to add to the current system time to construct the time seen by the application.

Used for debugging / testing.

Parameters
shortNameshort name of the option
namelong name of the option
[out]valset to the time specified at the command line

Definition at line 600 of file getopt_helpers.c.

602{
605 .name = name,
606 .argumentHelp = _ ("[+/-]MICROSECONDS"),
607 .description = _ (
608 "modify system time by given offset (for debugging/testing only)"),
609 .require_argument = 1,
610 .processor = &set_timetravel_time
611 };
612
613 return clo;
614}
static enum GNUNET_GenericReturnValue set_timetravel_time(struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, void *scls, const char *option, const char *value)
Set an option of type 'struct GNUNET_TIME_Relative' from the command line.
#define _(String)
GNU gettext support macro.
Definition: platform.h:178

References _, name, set_timetravel_time(), and GNUNET_GETOPT_CommandLineOption::shortName.

Here is the call graph for this function:

◆ GNUNET_GETOPT_option_version()

struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_version ( const char *  version)

Define the option to print the version of the application (-v option)

Parameters
versionstring with the version number

Definition at line 60 of file getopt_helpers.c.

61{
63 .shortName = 'v',
64 .name = "version",
65 .description = gettext_noop (
66 "print the version number"),
67 .option_exclusive = 1,
68 .processor = &print_version,
69 .scls = (void *) version
70 };
71
72 return clo;
73}
static enum GNUNET_GenericReturnValue print_version(struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, void *scls, const char *option, const char *value)
Print out program version (implements –version).

References gettext_noop, print_version(), and GNUNET_GETOPT_CommandLineOption::shortName.

Referenced by GNUNET_PROGRAM_conf_and_options(), GNUNET_PROGRAM_run2(), GNUNET_SERVICE_run_(), and main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_GETOPT_option_logfile()

struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_logfile ( char **  logfn)

Allow user to specify log file name (-l option)

Parameters
[out]logfnset to the name of the logfile

Definition at line 449 of file getopt_helpers.c.

450{
452 .shortName = 'l',
453 .name = "logfile",
454 .argumentHelp = "FILENAME",
455 .description =
456 gettext_noop ("configure logging to write logs to FILENAME"),
457 .require_argument = 1,
458 .processor = &set_filename,
459 .scls = (void *) logfn
460 };
461
462 return clo;
463}
static enum GNUNET_GenericReturnValue set_filename(struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, void *scls, const char *option, const char *value)
Set an option of type 'char *' from the command line with filename expansion a la GNUNET_STRINGS_file...

References gettext_noop, set_filename(), and GNUNET_GETOPT_CommandLineOption::shortName.

Referenced by GNUNET_PROGRAM_conf_and_options(), GNUNET_PROGRAM_run2(), GNUNET_SERVICE_run_(), and main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_GETOPT_option_string()

struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_string ( char  shortName,
const char *  name,
const char *  argumentHelp,
const char *  description,
char **  str 
)

Allow user to specify a string.

Parameters
shortNameshort name of the option
namelong name of the option
argumentHelphelp text for the option argument
descriptionlong help text for the option
[out]strset to the string

Definition at line 362 of file getopt_helpers.c.

367{
370 .name = name,
371 .argumentHelp = argumentHelp,
372 .description = description,
373 .require_argument = 1,
374 .processor = &set_string,
375 .scls = (void *) str
376 };
377
378 return clo;
379}
static enum GNUNET_GenericReturnValue set_string(struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, void *scls, const char *option, const char *value)
Set an option of type 'char *' from the command line.
const char * description
Help text for the option (description)
const char * argumentHelp
Name of the argument for the user in help text.

References GNUNET_GETOPT_CommandLineOption::argumentHelp, GNUNET_GETOPT_CommandLineOption::description, name, set_string(), and GNUNET_GETOPT_CommandLineOption::shortName.

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_GETOPT_option_filename()

struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_filename ( char  shortName,
const char *  name,
const char *  argumentHelp,
const char *  description,
char **  str 
)

Allow user to specify a filename (automatically path expanded).

Parameters
shortNameshort name of the option
namelong name of the option
argumentHelphelp text for the option argument
descriptionlong help text for the option
[out]strset to the string

Definition at line 428 of file getopt_helpers.c.

433{
436 .name = name,
437 .argumentHelp = argumentHelp,
438 .description = description,
439 .require_argument = 1,
440 .processor = &set_filename,
441 .scls = (void *) str
442 };
443
444 return clo;
445}

References GNUNET_GETOPT_CommandLineOption::argumentHelp, GNUNET_GETOPT_CommandLineOption::description, name, set_filename(), and GNUNET_GETOPT_CommandLineOption::shortName.

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_GETOPT_option_base32_fixed_size()

struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_base32_fixed_size ( char  shortName,
const char *  name,
const char *  argumentHelp,
const char *  description,
void *  val,
size_t  val_size 
)

Allow user to specify a binary value using Crockford Base32 encoding.

Parameters
shortNameshort name of the option
namelong name of the option
argumentHelphelp text for the option argument
descriptionlong help text for the option
[out]valbinary value decoded from Crockford Base32-encoded argument
val_sizesize of val in bytes

Definition at line 987 of file getopt_helpers.c.

993{
994 struct Base32Context *bc = GNUNET_new (struct Base32Context);
997 .name = name,
998 .argumentHelp = argumentHelp,
999 .description = description,
1000 .require_argument = 1,
1001 .processor = &set_base32,
1002 .cleaner = &free_bc,
1003 .scls = (void *) bc
1004 };
1005
1006 bc->val = val;
1007 bc->val_size = val_size;
1008 return clo;
1009}
static enum GNUNET_GenericReturnValue set_base32(struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, void *scls, const char *option, const char *value)
Set an option of type 'unsigned int' from the command line.
static void free_bc(void *cls)
Helper function to clean up after GNUNET_GETOPT_option_base32_fixed_size.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
Closure for set_base32().
void * val
Value to initialize (already allocated)
size_t val_size
Number of bytes expected for val.

References GNUNET_GETOPT_CommandLineOption::argumentHelp, GNUNET_GETOPT_CommandLineOption::description, free_bc(), GNUNET_new, name, set_base32(), GNUNET_GETOPT_CommandLineOption::shortName, Base32Context::val, and Base32Context::val_size.

Here is the call graph for this function:

◆ GNUNET_GETOPT_option_flag()

struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_flag ( char  shortName,
const char *  name,
const char *  description,
int *  val 
)

Allow user to specify a flag (which internally means setting an integer to 1/GNUNET_YES/GNUNET_OK.

Parameters
shortNameshort name of the option
namelong name of the option
descriptionlong help text for the option
[out]valset to 1 if the option is present

Definition at line 313 of file getopt_helpers.c.

317{
320 .name = name,
321 .description = description,
322 .processor = &set_one,
323 .scls = (void *) val
324 };
325
326 return clo;
327}
static enum GNUNET_GenericReturnValue set_one(struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, void *scls, const char *option, const char *value)
Set an option of type 'int' from the command line to 1 if the given option is present.

References GNUNET_GETOPT_CommandLineOption::description, name, set_one(), and GNUNET_GETOPT_CommandLineOption::shortName.

Referenced by GNUNET_PROGRAM_conf_and_options(), GNUNET_SERVICE_run_(), and main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_GETOPT_option_uint()

struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_uint ( char  shortName,
const char *  name,
const char *  argumentHelp,
const char *  description,
unsigned int *  val 
)

Allow user to specify an unsigned int.

Parameters
shortNameshort name of the option
namelong name of the option
argumentHelphelp text for the option argument
descriptionlong help text for the option
[out]valset to the value specified at the command line

Definition at line 832 of file getopt_helpers.c.

837{
840 .name = name,
841 .argumentHelp = argumentHelp,
842 .description = description,
843 .require_argument = 1,
844 .processor = &set_uint,
845 .scls = (void *) val
846 };
847
848 return clo;
849}
static enum GNUNET_GenericReturnValue set_uint(struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, void *scls, const char *option, const char *value)
Set an option of type 'unsigned int' from the command line.

References GNUNET_GETOPT_CommandLineOption::argumentHelp, GNUNET_GETOPT_CommandLineOption::description, name, set_uint(), and GNUNET_GETOPT_CommandLineOption::shortName.

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_GETOPT_option_uint16()

struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_uint16 ( char  shortName,
const char *  name,
const char *  argumentHelp,
const char *  description,
uint16_t *  val 
)

Allow user to specify an uint16_t.

Parameters
shortNameshort name of the option
namelong name of the option
argumentHelphelp text for the option argument
descriptionlong help text for the option
[out]valset to the value specified at the command line

Definition at line 897 of file getopt_helpers.c.

902{
905 .name = name,
906 .argumentHelp = argumentHelp,
907 .description = description,
908 .require_argument = 1,
909 .processor = &set_uint16,
910 .scls = (void *) val
911 };
912
913 return clo;
914}
static enum GNUNET_GenericReturnValue set_uint16(struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, void *scls, const char *option, const char *value)
Set an option of type 'uint16_t' from the command line.

References GNUNET_GETOPT_CommandLineOption::argumentHelp, GNUNET_GETOPT_CommandLineOption::description, name, set_uint16(), and GNUNET_GETOPT_CommandLineOption::shortName.

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_GETOPT_option_ulong()

struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_ulong ( char  shortName,
const char *  name,
const char *  argumentHelp,
const char *  description,
unsigned long long *  val 
)

Allow user to specify an unsigned long long.

Parameters
shortNameshort name of the option
namelong name of the option
argumentHelphelp text for the option argument
descriptionlong help text for the option
[out]valset to the value specified at the command line

Definition at line 518 of file getopt_helpers.c.

523{
526 .name = name,
527 .argumentHelp = argumentHelp,
528 .description = description,
529 .require_argument = 1,
530 .processor = &set_ulong,
531 .scls = (void *) val
532 };
533
534 return clo;
535}
static enum GNUNET_GenericReturnValue set_ulong(struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, void *scls, const char *option, const char *value)
Set an option of type 'unsigned long long' from the command line.

References GNUNET_GETOPT_CommandLineOption::argumentHelp, GNUNET_GETOPT_CommandLineOption::description, name, set_ulong(), and GNUNET_GETOPT_CommandLineOption::shortName.

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_GETOPT_option_relative_time()

struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_relative_time ( char  shortName,
const char *  name,
const char *  argumentHelp,
const char *  description,
struct GNUNET_TIME_Relative val 
)

Allow user to specify a struct GNUNET_TIME_Relative (using human-readable "fancy" time).

Parameters
shortNameshort name of the option
namelong name of the option
argumentHelphelp text for the option argument
descriptionlong help text for the option
[out]valset to the time specified at the command line

Definition at line 651 of file getopt_helpers.c.

656{
659 .name = name,
660 .argumentHelp = argumentHelp,
661 .description = description,
662 .require_argument = 1,
663 .processor = &set_relative_time,
664 .scls = (void *) val
665 };
666
667 return clo;
668}
static enum GNUNET_GenericReturnValue set_relative_time(struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, void *scls, const char *option, const char *value)
Set an option of type 'struct GNUNET_TIME_Relative' from the command line.

References GNUNET_GETOPT_CommandLineOption::argumentHelp, GNUNET_GETOPT_CommandLineOption::description, name, set_relative_time(), and GNUNET_GETOPT_CommandLineOption::shortName.

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_GETOPT_option_absolute_time()

struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_absolute_time ( char  shortName,
const char *  name,
const char *  argumentHelp,
const char *  description,
struct GNUNET_TIME_Absolute val 
)

Allow user to specify a struct GNUNET_TIME_Absolute (using human-readable "fancy" time).

Parameters
shortNameshort name of the option
namelong name of the option
argumentHelphelp text for the option argument
descriptionlong help text for the option
[out]valset to the time specified at the command line

Definition at line 705 of file getopt_helpers.c.

710{
713 .name = name,
714 .argumentHelp = argumentHelp,
715 .description = description,
716 .require_argument = 1,
717 .processor = &set_absolute_time,
718 .scls = (void *) val
719 };
720
721 return clo;
722}
static enum GNUNET_GenericReturnValue set_absolute_time(struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, void *scls, const char *option, const char *value)
Set an option of type 'struct GNUNET_TIME_Absolute' from the command line.

References GNUNET_GETOPT_CommandLineOption::argumentHelp, GNUNET_GETOPT_CommandLineOption::description, name, set_absolute_time(), and GNUNET_GETOPT_CommandLineOption::shortName.

Here is the call graph for this function:

◆ GNUNET_GETOPT_option_timestamp()

struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_timestamp ( char  shortName,
const char *  name,
const char *  argumentHelp,
const char *  description,
struct GNUNET_TIME_Timestamp val 
)

Allow user to specify a struct GNUNET_TIME_Timestamp (using human-readable "fancy" time).

Parameters
shortNameshort name of the option
namelong name of the option
argumentHelphelp text for the option argument
descriptionlong help text for the option
[out]valset to the time specified at the command line

Definition at line 769 of file getopt_helpers.c.

774{
777 .name = name,
778 .argumentHelp = argumentHelp,
779 .description = description,
780 .require_argument = 1,
781 .processor = &set_timestamp,
782 .scls = (void *) val
783 };
784
785 return clo;
786}
static enum GNUNET_GenericReturnValue set_timestamp(struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, void *scls, const char *option, const char *value)
Set an option of type 'struct GNUNET_TIME_Timestamp' from the command line.

References GNUNET_GETOPT_CommandLineOption::argumentHelp, GNUNET_GETOPT_CommandLineOption::description, name, set_timestamp(), and GNUNET_GETOPT_CommandLineOption::shortName.

Here is the call graph for this function:

◆ GNUNET_GETOPT_option_increment_uint()

struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_increment_uint ( char  shortName,
const char *  name,
const char *  description,
unsigned int *  val 
)

Increment val each time the option flag is given by one.

Parameters
shortNameshort name of the option
namelong name of the option
descriptionlong help text for the option
[out]valset to 1 if the option is present

Definition at line 249 of file getopt_helpers.c.

253{
256 .name = name,
257 .description = description,
258 .processor = &increment_value,
259 .scls = (void *) val
260 };
261
262 return clo;
263}
static enum GNUNET_GenericReturnValue increment_value(struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, void *scls, const char *option, const char *value)
Set an option of type 'unsigned int' from the command line.

References GNUNET_GETOPT_CommandLineOption::description, increment_value(), name, and GNUNET_GETOPT_CommandLineOption::shortName.

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_GETOPT_option_loglevel()

struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_loglevel ( char **  level)

Define the '-L' log level option.

Note that we do not check that the log level is valid here.

Parameters
[out]levelset to the log level

Definition at line 383 of file getopt_helpers.c.

384{
386 .shortName = 'L',
387 .name = "log",
388 .argumentHelp = "LOGLEVEL",
389 .description = gettext_noop ("configure logging to use LOGLEVEL"),
390 .require_argument = 1,
391 .processor = &set_string,
392 .scls = (void *) level
393 };
394
395 return clo;
396}

References gettext_noop, set_string(), and GNUNET_GETOPT_CommandLineOption::shortName.

Referenced by GNUNET_PROGRAM_conf_and_options(), GNUNET_PROGRAM_run2(), GNUNET_SERVICE_run_(), and main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_GETOPT_option_verbose()

struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_verbose ( unsigned int *  level)

Define the '-V' verbosity option.

Using the option more than once increments level each time.

Parameters
[out]levelset to the verbosity level

Definition at line 267 of file getopt_helpers.c.

268{
270 .shortName = 'V',
271 .name = "verbose",
272 .description =
273 gettext_noop ("be verbose"),
274 .processor = &increment_value,
275 .scls = (void *) level
276 };
277
278 return clo;
279}

References gettext_noop, increment_value(), and GNUNET_GETOPT_CommandLineOption::shortName.

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_GETOPT_option_cfgfile()

struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_cfgfile ( char **  fn)

Allow user to specify configuration file name (-c option)

Parameters
[out]fnset to the name of the configuration file

Definition at line 467 of file getopt_helpers.c.

468{
470 .shortName = 'c',
471 .name = "config",
472 .argumentHelp = "FILENAME",
473 .description = gettext_noop ("use configuration file FILENAME"),
474 .require_argument = 1,
475 .processor = &set_filename,
476 .scls = (void *) fn
477 };
478
479 return clo;
480}

References gettext_noop, set_filename(), and GNUNET_GETOPT_CommandLineOption::shortName.

Referenced by GNUNET_PROGRAM_conf_and_options(), GNUNET_PROGRAM_run2(), GNUNET_SERVICE_run_(), and main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_GETOPT_option_mandatory()

struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_mandatory ( struct GNUNET_GETOPT_CommandLineOption  opt)

Make the given option mandatory.

Parameters
optoption to modify
Returns
opt with the mandatory flag set.

Definition at line 1013 of file getopt_helpers.c.

1014{
1015 opt.option_mandatory = 1;
1016 return opt;
1017}
int option_mandatory
Is the presence of this option mandatory?

References GNUNET_GETOPT_CommandLineOption::option_mandatory.

Referenced by main().

Here is the caller graph for this function:

◆ GNUNET_GETOPT_option_exclusive()

struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_exclusive ( struct GNUNET_GETOPT_CommandLineOption  opt)

Make the given option mutually exclusive with other options.

Parameters
optoption to modify
Returns
opt with the exclusive flag set.

Definition at line 1021 of file getopt_helpers.c.

1022{
1023 opt.option_exclusive = 1;
1024 return opt;
1025}
int option_exclusive
Is the option exclusive?

References GNUNET_GETOPT_CommandLineOption::option_exclusive.

Referenced by main().

Here is the caller graph for this function:

◆ GNUNET_GETOPT_run()

int GNUNET_GETOPT_run ( const char *  binaryOptions,
const struct GNUNET_GETOPT_CommandLineOption allOptions,
unsigned int  argc,
char *const *  argv 
)

Parse the command line.

Parameters
binaryOptionsName of application with option summary
allOptionsdefined options and handlers
argcnumber of arguments in argv
argvactual arguments
Returns
index into argv with first non-option argument, or GNUNET_SYSERR on error

Definition at line 884 of file getopt.c.

888{
889 struct GNoption *long_options;
891 int count;
892 char *shorts;
893 int spos;
895 uint8_t *seen;
896 unsigned int optmatch = 0;
897 const char *have_exclusive = NULL;
898
899 GNUNET_assert (argc > 0);
900 GNoptind = 0;
901 clpc.binaryName = argv[0];
902 clpc.binaryOptions = binaryOptions;
903 clpc.allOptions = allOptions;
904 clpc.argv = argv;
905 clpc.argc = argc;
906 for (count = 0; NULL != allOptions[count].name; count++)
907 ;
908
909 /* transform our option representation into the format
910 used by the GNU getopt copylib */
911 long_options = GNUNET_new_array (count + 1, struct GNoption);
912 seen = GNUNET_new_array (count, uint8_t);
913 shorts = GNUNET_malloc (count * 2 + 1);
914 spos = 0;
915 for (unsigned i = 0; i < count; i++)
916 {
917 long_options[i].name = allOptions[i].name;
918 long_options[i].has_arg = allOptions[i].require_argument;
919 long_options[i].flag = NULL;
920 long_options[i].val = allOptions[i].shortName;
921 shorts[spos++] = allOptions[i].shortName;
922 if (allOptions[i].require_argument != 0)
923 shorts[spos++] = ':';
924 }
925 long_options[count].name = NULL;
926 long_options[count].has_arg = 0;
927 long_options[count].flag = NULL;
928 long_options[count].val = '\0';
929 shorts[spos] = '\0';
930 cont = GNUNET_OK;
931
932 /* main getopt loop */
933 while (1)
934 {
935 int option_index = 0;
936 unsigned int i;
937 int c;
938
939 c = GNgetopt_long (argc,
940 argv,
941 shorts,
942 long_options,
943 &option_index);
944 if (c == GNUNET_SYSERR)
945 break; /* No more flags to process */
946
947 /* Check which of our program's options was given by the user */
948 for (i = 0; i < count; i++)
949 {
950 clpc.currentArgument = GNoptind - 1;
951 if ((char) c == allOptions[i].shortName)
952 {
953 optmatch++;
954 if (allOptions[i].option_exclusive)
955 have_exclusive = allOptions[i].name;
956 if (GNUNET_OK == cont)
957 {
958 /* parse the option using the option-specific processor */
959 cont = allOptions[i].processor (&clpc,
960 allOptions[i].scls,
961 allOptions[i].name,
962 GNoptarg);
963 }
964 seen[i] = 1;
965 break;
966 }
967 }
968 if (i == count)
969 {
970 fprintf (stderr,
971 _ ("Use %s to get a list of options.\n"),
972 "--help");
973 cont = GNUNET_SYSERR;
974 }
975 }
976 GNUNET_free (shorts);
977 GNUNET_free (long_options);
978
979 /* check that if any option that was marked as exclusive
980 is the only option that was provided */
981 if ((NULL != have_exclusive) && (optmatch > 1))
982 {
983 fprintf (stderr,
984 _ ("Option `%s' can't be used with other options.\n"),
985 have_exclusive);
986 cont = GNUNET_SYSERR;
987 }
988 if (GNUNET_YES == cont)
989 {
990 /* check that all mandatory options are present */
991 for (count = 0; NULL != allOptions[count].name; count++)
992 {
993 if ((0 == seen[count]) && (allOptions[count].option_mandatory))
994 {
995 fprintf (stderr,
996 _ ("Missing mandatory option `%s'.\n"),
997 allOptions[count].name);
998 cont = GNUNET_SYSERR;
999 }
1000 }
1001 }
1002 GNUNET_free (seen);
1003
1004 /* call cleaners, if available */
1005 for (unsigned int i = 0; NULL != allOptions[i].name; i++)
1006 if (NULL != allOptions[i].cleaner)
1007 allOptions[i].cleaner (allOptions[i].scls);
1008
1009 if (GNUNET_OK != cont)
1010 return cont;
1011 return GNoptind;
1012}
static int GNoptind
Definition: getopt.c:134
static int GNgetopt_long(int argc, char *const *argv, const char *options, const struct GNoption *long_options, int *opt_index)
Definition: getopt.c:870
static char * GNoptarg
Definition: getopt.c:119
#define _(msgid)
Definition: getopt.c:63
GNUNET_GenericReturnValue
Named constants for return values.
@ GNUNET_OK
@ GNUNET_YES
@ GNUNET_SYSERR
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_new_array(n, type)
Allocate a size n array with structs or unions of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
int require_argument
Is an argument required? GNUNET_NO (includes optional) or GNUNET_YES (required)
const char * name
Long name of the option (may not be NULL)
GNUNET_GETOPT_CommandLineOptionProcessor processor
Handler for the option.
void(* cleaner)(void *cls)
Function to call on scls to clean up after processing all the arguments.
General context for command line processors.
const char * binaryOptions
Name of application with option summary.
const struct GNUNET_GETOPT_CommandLineOption * allOptions
Array with all command line options.
char *const * argv
Original command line.
unsigned int argc
Total number of argv's.
int has_arg
Definition: getopt.c:93
int val
Definition: getopt.c:95
const char * name
Definition: getopt.c:90
int * flag
Definition: getopt.c:94

References _, GNUNET_GETOPT_CommandLineProcessorContext::allOptions, GNUNET_GETOPT_CommandLineProcessorContext::argc, GNUNET_GETOPT_CommandLineProcessorContext::argv, GNUNET_GETOPT_CommandLineProcessorContext::binaryName, GNUNET_GETOPT_CommandLineProcessorContext::binaryOptions, GNUNET_GETOPT_CommandLineOption::cleaner, GNUNET_GETOPT_CommandLineProcessorContext::currentArgument, GNoption::flag, GNgetopt_long(), GNoptarg, GNoptind, GNUNET_assert, GNUNET_free, GNUNET_malloc, GNUNET_new_array, GNUNET_OK, GNUNET_SYSERR, GNUNET_YES, GNoption::has_arg, name, GNUNET_GETOPT_CommandLineOption::name, GNoption::name, GNUNET_GETOPT_CommandLineOption::processor, GNUNET_GETOPT_CommandLineOption::require_argument, GNUNET_GETOPT_CommandLineOption::shortName, and GNoption::val.

Referenced by GNUNET_PROGRAM_conf_and_options(), GNUNET_PROGRAM_run2(), GNUNET_SERVICE_run_(), and main().

Here is the call graph for this function:
Here is the caller graph for this function: