GNUnet  0.10.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_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_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 248 of file gnunet_getopt_lib.h.

Referenced by main().

◆ 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 441 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 (i.e. "config" for –config)
valueargument, NULL if none was given
Returns
GNUNET_OK to continue processing other options, GNUNET_SYSERR to abort

Definition at line 89 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 200 of file getopt_helpers.c.

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

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

201 {
202  struct GNUNET_GETOPT_CommandLineOption clo = { .shortName = 'h',
203  .name = "help",
204  .description = gettext_noop(
205  "print this help"),
206  .option_exclusive = 1,
207  .processor = format_help,
208  .scls = (void *)about };
209 
210  return clo;
211 }
static int format_help(struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, void *scls, const char *option, const char *value)
Print out details on command line options (implements –help).
Definition of a command line option.
const char shortName
Short name of the option.
#define gettext_noop(String)
Definition: gettext.h:69
Here is the call graph for this function:
Here is the caller 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 63 of file getopt_helpers.c.

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

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

64 {
65  struct GNUNET_GETOPT_CommandLineOption clo = { .shortName = 'v',
66  .name = "version",
67  .description = gettext_noop(
68  "print the version number"),
69  .option_exclusive = 1,
70  .processor = &print_version,
71  .scls = (void *)version };
72 
73  return clo;
74 }
static int print_version(struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, void *scls, const char *option, const char *value)
Print out program version (implements –version).
Definition of a command line option.
const char shortName
Short name of the option.
#define gettext_noop(String)
Definition: gettext.h:69
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 488 of file getopt_helpers.c.

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

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

489 {
491  { .shortName = 'l',
492  .name = "logfile",
493  .argumentHelp = "FILENAME",
494  .description =
495  gettext_noop("configure logging to write logs to FILENAME"),
496  .require_argument = 1,
497  .processor = &set_filename,
498  .scls = (void *)logfn };
499 
500  return clo;
501 }
Definition of a command line option.
const char shortName
Short name of the option.
static int 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...
#define gettext_noop(String)
Definition: gettext.h:69
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 386 of file getopt_helpers.c.

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

Referenced by main().

391 {
393  .name = name,
394  .argumentHelp = argumentHelp,
395  .description = description,
396  .require_argument = 1,
397  .processor = &set_string,
398  .scls = (void *)str };
399 
400  return clo;
401 }
const char * argumentHelp
Name of the argument for the user in help text.
const char * description
Help text for the option (description)
static int 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.
Definition of a command line option.
const char shortName
Short name of the option.
const char * name
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 464 of file getopt_helpers.c.

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

Referenced by main().

469 {
471  .name = name,
472  .argumentHelp = argumentHelp,
473  .description = description,
474  .require_argument = 1,
475  .processor = &set_filename,
476  .scls = (void *)str };
477 
478  return clo;
479 }
const char * argumentHelp
Name of the argument for the user in help text.
const char * description
Help text for the option (description)
Definition of a command line option.
const char shortName
Short name of the option.
static int 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...
const char * name
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 932 of file getopt_helpers.c.

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.

938 {
939  struct Base32Context *bc = GNUNET_new(struct Base32Context);
941  .name = name,
942  .argumentHelp = argumentHelp,
943  .description = description,
944  .require_argument = 1,
945  .processor = &set_base32,
946  .cleaner = &free_bc,
947  .scls = (void *)bc };
948 
949  bc->val = val;
950  bc->val_size = val_size;
951  return clo;
952 }
const char * argumentHelp
Name of the argument for the user in help text.
const char * description
Help text for the option (description)
#define GNUNET_new(type)
Allocate a struct or union of the given type.
Definition of a command line option.
Closure for set_base32().
const char shortName
Short name of the option.
static void free_bc(void *cls)
Helper function to clean up after GNUNET_GETOPT_option_base32_fixed_size.
size_t val_size
Number of bytes expected for val.
static int 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.
const char * name
void * val
Value to initialize (already allocated)
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
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 330 of file getopt_helpers.c.

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

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

334 {
336  .name = name,
337  .description = description,
338  .processor = &set_one,
339  .scls = (void *)val };
340 
341  return clo;
342 }
static int 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.
const char * description
Help text for the option (description)
Definition of a command line option.
const char shortName
Short name of the option.
const char * name
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

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

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

Referenced by main().

768 {
770  .name = name,
771  .argumentHelp = argumentHelp,
772  .description = description,
773  .require_argument = 1,
774  .processor = &set_uint,
775  .scls = (void *)val };
776 
777  return clo;
778 }
const char * argumentHelp
Name of the argument for the user in help text.
const char * description
Help text for the option (description)
Definition of a command line option.
const char shortName
Short name of the option.
const char * name
static int 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.
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 835 of file getopt_helpers.c.

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

Referenced by main().

840 {
842  .name = name,
843  .argumentHelp = argumentHelp,
844  .description = description,
845  .require_argument = 1,
846  .processor = &set_uint16,
847  .scls = (void *)val };
848 
849  return clo;
850 }
const char * argumentHelp
Name of the argument for the user in help text.
const char * description
Help text for the option (description)
Definition of a command line option.
const char shortName
Short name of the option.
static int 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.
const char * name
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

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

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

Referenced by main().

574 {
576  .name = name,
577  .argumentHelp = argumentHelp,
578  .description = description,
579  .require_argument = 1,
580  .processor = &set_ulong,
581  .scls = (void *)val };
582 
583  return clo;
584 }
const char * argumentHelp
Name of the argument for the user in help text.
const char * description
Help text for the option (description)
Definition of a command line option.
const char shortName
Short name of the option.
const char * name
static int 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.
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 631 of file getopt_helpers.c.

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

Referenced by main().

636 {
638  .name = name,
639  .argumentHelp = argumentHelp,
640  .description = description,
641  .require_argument = 1,
642  .processor = &set_relative_time,
643  .scls = (void *)val };
644 
645  return clo;
646 }
static int 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.
const char * argumentHelp
Name of the argument for the user in help text.
const char * description
Help text for the option (description)
Definition of a command line option.
const char shortName
Short name of the option.
const char * name
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 693 of file getopt_helpers.c.

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

698 {
700  .name = name,
701  .argumentHelp = argumentHelp,
702  .description = description,
703  .require_argument = 1,
704  .processor = &set_absolute_time,
705  .scls = (void *)val };
706 
707  return clo;
708 }
static int 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.
const char * argumentHelp
Name of the argument for the user in help text.
const char * description
Help text for the option (description)
Definition of a command line option.
const char shortName
Short name of the option.
const char * name
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
argumentHelphelp text for the option argument
descriptionlong help text for the option
[out]valset to 1 if the option is present
shortNameshort name of the option
namelong name of the option
argumentHelphelp text for the option argument
descriptionlong help text for the option
[out]valincrement by 1 each time the option is present

Definition at line 254 of file getopt_helpers.c.

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

Referenced by main().

258 {
260  .name = name,
261  .description = description,
262  .processor = &increment_value,
263  .scls = (void *)val };
264 
265  return clo;
266 }
static int 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.
const char * description
Help text for the option (description)
Definition of a command line option.
const char shortName
Short name of the option.
const char * name
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 411 of file getopt_helpers.c.

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

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

412 {
414  { .shortName = 'L',
415  .name = "log",
416  .argumentHelp = "LOGLEVEL",
417  .description = gettext_noop("configure logging to use LOGLEVEL"),
418  .require_argument = 1,
419  .processor = &set_string,
420  .scls = (void *)level };
421 
422  return clo;
423 }
static int 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.
Definition of a command line option.
const char shortName
Short name of the option.
#define gettext_noop(String)
Definition: gettext.h:69
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 276 of file getopt_helpers.c.

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

Referenced by main().

277 {
278  struct GNUNET_GETOPT_CommandLineOption clo = { .shortName = 'V',
279  .name = "verbose",
280  .description =
281  gettext_noop("be verbose"),
282  .processor = &increment_value,
283  .scls = (void *)level };
284 
285  return clo;
286 }
static int 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.
Definition of a command line option.
const char shortName
Short name of the option.
#define gettext_noop(String)
Definition: gettext.h:69
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 510 of file getopt_helpers.c.

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

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

511 {
513  { .shortName = 'c',
514  .name = "config",
515  .argumentHelp = "FILENAME",
516  .description = gettext_noop("use configuration file FILENAME"),
517  .require_argument = 1,
518  .processor = &set_filename,
519  .scls = (void *)fn };
520 
521  return clo;
522 }
Definition of a command line option.
const char shortName
Short name of the option.
static char * fn
Filename of the unique file.
static int 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...
#define gettext_noop(String)
Definition: gettext.h:69
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 962 of file getopt_helpers.c.

References GNUNET_GETOPT_CommandLineOption::option_mandatory.

Referenced by main().

963 {
964  opt.option_mandatory = 1;
965  return opt;
966 }
int option_mandatory
Is the presence of this option mandatory?
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 976 of file getopt_helpers.c.

References GNUNET_GETOPT_CommandLineOption::option_exclusive.

Referenced by main().

977 {
978  opt.option_exclusive = 1;
979  return opt;
980 }
int option_exclusive
Is the option exclusive?
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
Parameters
binaryOptionsName of application with option summary
allOptionsdefined options and handlers
argcnumber of arguments
argvactual arguments
Returns
index into argv with first non-option argument, or GNUNET_SYSERR on error

Definition at line 886 of file getopt.c.

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, GNoption::name, GNUNET_GETOPT_CommandLineOption::name, GNUNET_GETOPT_CommandLineOption::processor, GNUNET_GETOPT_CommandLineOption::require_argument, GNUNET_GETOPT_CommandLineOption::shortName, and GNoption::val.

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

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