GNUnet  0.11.x
Data Structures | Macros | Typedefs | Functions
Getopt library

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

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 264 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 475 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 90 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.

192 {
193  struct GNUNET_GETOPT_CommandLineOption clo = {
194  .shortName = 'h',
195  .name = "help",
196  .description = gettext_noop (
197  "print this help"),
198  .option_exclusive = 1,
199  .processor = format_help,
200  .scls = (void *) about
201  };
202 
203  return clo;
204 }
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:69
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.

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

◆ 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.

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

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

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

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

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

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

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

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

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

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

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

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

763 {
764  struct GNUNET_GETOPT_CommandLineOption clo = {
765  .shortName = shortName,
766  .name = name,
767  .argumentHelp = argumentHelp,
768  .description = description,
769  .require_argument = 1,
770  .processor = &set_timestamp,
771  .scls = (void *) val
772  };
773 
774  return clo;
775 }
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
argumentHelphelp text for the option argument
descriptionlong help text for the option
[out]valset to 1 if the option is present

Definition at line 1 of file getopt_helpers.c.

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

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

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.

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

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.

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

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

1002 {
1003  opt.option_mandatory = 1;
1004  return opt;
1005 }
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 968 of file getopt_helpers.c.

1010 {
1011  opt.option_exclusive = 1;
1012  return opt;
1013 }
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 883 of file getopt.c.

887 {
888  struct GNoption *long_options;
890  int count;
891  char *shorts;
892  int spos;
893  enum GNUNET_GenericReturnValue cont;
894  uint8_t *seen;
895  unsigned int optmatch = 0;
896  const char *have_exclusive = NULL;
897 
898  GNUNET_assert (argc > 0);
899  GNoptind = 0;
900  clpc.binaryName = argv[0];
901  clpc.binaryOptions = binaryOptions;
902  clpc.allOptions = allOptions;
903  clpc.argv = argv;
904  clpc.argc = argc;
905  for (count = 0; NULL != allOptions[count].name; count++)
906  ;
907 
908  /* transform our option representation into the format
909  used by the GNU getopt copylib */
910  long_options = GNUNET_new_array (count + 1, struct GNoption);
911  seen = GNUNET_new_array (count, uint8_t);
912  shorts = GNUNET_malloc (count * 2 + 1);
913  spos = 0;
914  for (unsigned i = 0; i < count; i++)
915  {
916  long_options[i].name = allOptions[i].name;
917  long_options[i].has_arg = allOptions[i].require_argument;
918  long_options[i].flag = NULL;
919  long_options[i].val = allOptions[i].shortName;
920  shorts[spos++] = allOptions[i].shortName;
921  if (allOptions[i].require_argument != 0)
922  shorts[spos++] = ':';
923  }
924  long_options[count].name = NULL;
925  long_options[count].has_arg = 0;
926  long_options[count].flag = NULL;
927  long_options[count].val = '\0';
928  shorts[spos] = '\0';
929  cont = GNUNET_OK;
930 
931  /* main getopt loop */
932  while (1)
933  {
934  int option_index = 0;
935  unsigned int i;
936  int c;
937 
938  c = GNgetopt_long (argc,
939  argv,
940  shorts,
941  long_options,
942  &option_index);
943  if (c == GNUNET_SYSERR)
944  break; /* No more flags to process */
945 
946  /* Check which of our program's options was given by the user */
947  for (i = 0; i < count; i++)
948  {
949  clpc.currentArgument = GNoptind - 1;
950  if ((char) c == allOptions[i].shortName)
951  {
952  optmatch++;
953  if (allOptions[i].option_exclusive)
954  have_exclusive = allOptions[i].name;
955  if (GNUNET_OK == cont)
956  {
957  /* parse the option using the option-specific processor */
958  cont = allOptions[i].processor (&clpc,
959  allOptions[i].scls,
960  allOptions[i].name,
961  GNoptarg);
962  }
963  seen[i] = 1;
964  break;
965  }
966  }
967  if (i == count)
968  {
969  fprintf (stderr,
970  _ ("Use %s to get a list of options.\n"),
971  "--help");
972  cont = GNUNET_SYSERR;
973  }
974  }
975  GNUNET_free (shorts);
976  GNUNET_free (long_options);
977 
978  /* check that if any option that was marked as exclusive
979  is the only option that was provided */
980  if ((NULL != have_exclusive) && (optmatch > 1))
981  {
982  fprintf (stderr,
983  _ ("Option `%s' can't be used with other options.\n"),
984  have_exclusive);
985  cont = GNUNET_SYSERR;
986  }
987  if (GNUNET_YES == cont)
988  {
989  /* check that all mandatory options are present */
990  for (count = 0; NULL != allOptions[count].name; count++)
991  {
992  if ((0 == seen[count]) && (allOptions[count].option_mandatory))
993  {
994  fprintf (stderr,
995  _ ("Missing mandatory option `%s'.\n"),
996  allOptions[count].name);
997  cont = GNUNET_SYSERR;
998  }
999  }
1000  }
1001  GNUNET_free (seen);
1002 
1003  /* call cleaners, if available */
1004  for (unsigned int i = 0; NULL != allOptions[i].name; i++)
1005  if (NULL != allOptions[i].cleaner)
1006  allOptions[i].cleaner (allOptions[i].scls);
1007 
1008  if (GNUNET_OK != cont)
1009  return cont;
1010  return GNoptind;
1011 }
static int GNoptind
Definition: getopt.c:133
static int GNgetopt_long(int argc, char *const *argv, const char *options, const struct GNoption *long_options, int *opt_index)
Definition: getopt.c:869
static char * GNoptarg
Definition: getopt.c:118
#define _(msgid)
Definition: getopt.c:62
GNUNET_GenericReturnValue
Named constants for return values.
Definition: gnunet_common.h:92
@ GNUNET_OK
Definition: gnunet_common.h:95
@ GNUNET_YES
Definition: gnunet_common.h:97
@ GNUNET_SYSERR
Definition: gnunet_common.h:93
#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:92
int val
Definition: getopt.c:94
const char * name
Definition: getopt.c:89
int * flag
Definition: getopt.c:93

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: