GNUnet  0.20.0
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 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))
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.
const char * name

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 273 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 483 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 char *  about)

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

Parameters
aboutstring with brief description of the application

Definition at line 1 of file getopt_helpers.c.

193 {
194  struct GNUNET_GETOPT_CommandLineOption clo = {
195  .shortName = 'h',
196  .name = "help",
197  .description = gettext_noop (
198  "print this help"),
199  .option_exclusive = 1,
200  .processor = format_help,
201  .scls = (void *) about
202  };
203 
204  return clo;
205 }
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:70
Definition of a command line option.
const char shortName
Short name of the option.

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

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 1 of file getopt_helpers.c.

592 {
593  struct GNUNET_GETOPT_CommandLineOption clo = {
594  .shortName = shortName,
595  .name = name,
596  .argumentHelp = _ ("[+/-]MICROSECONDS"),
597  .description = _ (
598  "modify system time by given offset (for debugging/testing only)"),
599  .require_argument = 1,
600  .processor = &set_timetravel_time
601  };
602 
603  return clo;
604 }
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

◆ 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 1 of file getopt_helpers.c.

59 {
60  struct GNUNET_GETOPT_CommandLineOption clo = {
61  .shortName = 'v',
62  .name = "version",
63  .description = gettext_noop (
64  "print the version number"),
65  .option_exclusive = 1,
66  .processor = &print_version,
67  .scls = (void *) version
68  };
69 
70  return clo;
71 }
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).

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

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 1 of file getopt_helpers.c.

440 {
441  struct GNUNET_GETOPT_CommandLineOption clo = {
442  .shortName = 'l',
443  .name = "logfile",
444  .argumentHelp = "FILENAME",
445  .description =
446  gettext_noop ("configure logging to write logs to FILENAME"),
447  .require_argument = 1,
448  .processor = &set_filename,
449  .scls = (void *) logfn
450  };
451 
452  return clo;
453 }
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...

Referenced by GNUNET_SERVICE_run_(), and LEGACY_SERVICE_run().

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 1 of file getopt_helpers.c.

357 {
358  struct GNUNET_GETOPT_CommandLineOption clo = {
359  .shortName = shortName,
360  .name = name,
361  .argumentHelp = argumentHelp,
362  .description = description,
363  .require_argument = 1,
364  .processor = &set_string,
365  .scls = (void *) str
366  };
367 
368  return clo;
369 }
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.

Referenced by main().

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 1 of file getopt_helpers.c.

423 {
424  struct GNUNET_GETOPT_CommandLineOption clo = {
425  .shortName = shortName,
426  .name = name,
427  .argumentHelp = argumentHelp,
428  .description = description,
429  .require_argument = 1,
430  .processor = &set_filename,
431  .scls = (void *) str
432  };
433 
434  return clo;
435 }

Referenced by main().

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 969 of file getopt_helpers.c.

982 {
983  struct Base32Context *bc = GNUNET_new (struct Base32Context);
984  struct GNUNET_GETOPT_CommandLineOption clo = {
985  .shortName = shortName,
986  .name = name,
987  .argumentHelp = argumentHelp,
988  .description = description,
989  .require_argument = 1,
990  .processor = &set_base32,
991  .cleaner = &free_bc,
992  .scls = (void *) bc
993  };
994 
995  bc->val = val;
996  bc->val_size = val_size;
997  return clo;
998 }
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.

◆ 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 1 of file getopt_helpers.c.

307 {
308  struct GNUNET_GETOPT_CommandLineOption clo = {
309  .shortName = shortName,
310  .name = name,
311  .description = description,
312  .processor = &set_one,
313  .scls = (void *) val
314  };
315 
316  return clo;
317 }
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.

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

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 1 of file getopt_helpers.c.

827 {
828  struct GNUNET_GETOPT_CommandLineOption clo = {
829  .shortName = shortName,
830  .name = name,
831  .argumentHelp = argumentHelp,
832  .description = description,
833  .require_argument = 1,
834  .processor = &set_uint,
835  .scls = (void *) val
836  };
837 
838  return clo;
839 }
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.

Referenced by main().

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 1 of file getopt_helpers.c.

892 {
893  struct GNUNET_GETOPT_CommandLineOption clo = {
894  .shortName = shortName,
895  .name = name,
896  .argumentHelp = argumentHelp,
897  .description = description,
898  .require_argument = 1,
899  .processor = &set_uint16,
900  .scls = (void *) val
901  };
902 
903  return clo;
904 }
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.

Referenced by main().

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 1 of file getopt_helpers.c.

513 {
514  struct GNUNET_GETOPT_CommandLineOption clo = {
515  .shortName = shortName,
516  .name = name,
517  .argumentHelp = argumentHelp,
518  .description = description,
519  .require_argument = 1,
520  .processor = &set_ulong,
521  .scls = (void *) val
522  };
523 
524  return clo;
525 }
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.

Referenced by main().

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 1 of file getopt_helpers.c.

646 {
647  struct GNUNET_GETOPT_CommandLineOption clo = {
648  .shortName = shortName,
649  .name = name,
650  .argumentHelp = argumentHelp,
651  .description = description,
652  .require_argument = 1,
653  .processor = &set_relative_time,
654  .scls = (void *) val
655  };
656 
657  return clo;
658 }
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.

Referenced by main().

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 1 of file getopt_helpers.c.

700 {
701  struct GNUNET_GETOPT_CommandLineOption clo = {
702  .shortName = shortName,
703  .name = name,
704  .argumentHelp = argumentHelp,
705  .description = description,
706  .require_argument = 1,
707  .processor = &set_absolute_time,
708  .scls = (void *) val
709  };
710 
711  return clo;
712 }
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.

◆ 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 1 of file getopt_helpers.c.

764 {
765  struct GNUNET_GETOPT_CommandLineOption clo = {
766  .shortName = shortName,
767  .name = name,
768  .argumentHelp = argumentHelp,
769  .description = description,
770  .require_argument = 1,
771  .processor = &set_timestamp,
772  .scls = (void *) val
773  };
774 
775  return clo;
776 }
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.

◆ 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 1 of file getopt_helpers.c.

243 {
244  struct GNUNET_GETOPT_CommandLineOption clo = {
245  .shortName = shortName,
246  .name = name,
247  .description = description,
248  .processor = &increment_value,
249  .scls = (void *) val
250  };
251 
252  return clo;
253 }
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.

Referenced by main().

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 1 of file getopt_helpers.c.

374 {
375  struct GNUNET_GETOPT_CommandLineOption clo = {
376  .shortName = 'L',
377  .name = "log",
378  .argumentHelp = "LOGLEVEL",
379  .description = gettext_noop ("configure logging to use LOGLEVEL"),
380  .require_argument = 1,
381  .processor = &set_string,
382  .scls = (void *) level
383  };
384 
385  return clo;
386 }

Referenced by GNUNET_SERVICE_run_(), and LEGACY_SERVICE_run().

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 1 of file getopt_helpers.c.

258 {
259  struct GNUNET_GETOPT_CommandLineOption clo = {
260  .shortName = 'V',
261  .name = "verbose",
262  .description =
263  gettext_noop ("be verbose"),
264  .processor = &increment_value,
265  .scls = (void *) level
266  };
267 
268  return clo;
269 }

Referenced by main().

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 1 of file getopt_helpers.c.

458 {
459  struct GNUNET_GETOPT_CommandLineOption clo = {
460  .shortName = 'c',
461  .name = "config",
462  .argumentHelp = "FILENAME",
463  .description = gettext_noop ("use configuration file FILENAME"),
464  .require_argument = 1,
465  .processor = &set_filename,
466  .scls = (void *) fn
467  };
468 
469  return clo;
470 }

Referenced by GNUNET_SERVICE_run_(), and LEGACY_SERVICE_run().

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 969 of file getopt_helpers.c.

1003 {
1004  opt.option_mandatory = 1;
1005  return opt;
1006 }
int option_mandatory
Is the presence of this 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 969 of file getopt_helpers.c.

1011 {
1012  opt.option_exclusive = 1;
1013  return opt;
1014 }
int option_exclusive
Is the 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;
894  enum GNUNET_GenericReturnValue cont;
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_SERVICE_run_(), LEGACY_SERVICE_run(), and main().

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