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 252 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 445 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 91 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 196 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().

197 {
198  struct GNUNET_GETOPT_CommandLineOption clo = {.shortName = 'h',
199  .name = "help",
200  .description = gettext_noop (
201  "print this help"),
202  .option_exclusive = 1,
203  .processor = format_help,
204  .scls = (void *) about};
205 
206  return clo;
207 }
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  return clo;
73 }
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 484 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().

485 {
487  {.shortName = 'l',
488  .name = "logfile",
489  .argumentHelp = "FILENAME",
490  .description =
491  gettext_noop ("configure logging to write logs to FILENAME"),
492  .require_argument = 1,
493  .processor = &set_filename,
494  .scls = (void *) logfn};
495 
496  return clo;
497 }
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 382 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().

387 {
389  .name = name,
390  .argumentHelp = argumentHelp,
391  .description = description,
392  .require_argument = 1,
393  .processor = &set_string,
394  .scls = (void *) str};
395 
396  return clo;
397 }
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 460 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().

465 {
467  .name = name,
468  .argumentHelp = argumentHelp,
469  .description = description,
470  .require_argument = 1,
471  .processor = &set_filename,
472  .scls = (void *) str};
473 
474  return clo;
475 }
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 929 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.

935 {
936  struct Base32Context *bc = GNUNET_new (struct Base32Context);
938  .name = name,
939  .argumentHelp = argumentHelp,
940  .description = description,
941  .require_argument = 1,
942  .processor = &set_base32,
943  .cleaner = &free_bc,
944  .scls = (void *) bc};
945 
946  bc->val = val;
947  bc->val_size = val_size;
948  return clo;
949 }
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 326 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().

330 {
332  .name = name,
333  .description = description,
334  .processor = &set_one,
335  .scls = (void *) val};
336 
337  return clo;
338 }
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 759 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().

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

836 {
838  .name = name,
839  .argumentHelp = argumentHelp,
840  .description = description,
841  .require_argument = 1,
842  .processor = &set_uint16,
843  .scls = (void *) val};
844 
845  return clo;
846 }
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 565 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().

570 {
572  .name = name,
573  .argumentHelp = argumentHelp,
574  .description = description,
575  .require_argument = 1,
576  .processor = &set_ulong,
577  .scls = (void *) val};
578 
579  return clo;
580 }
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 627 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().

632 {
634  .name = name,
635  .argumentHelp = argumentHelp,
636  .description = description,
637  .require_argument = 1,
638  .processor = &set_relative_time,
639  .scls = (void *) val};
640 
641  return clo;
642 }
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 689 of file getopt_helpers.c.

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

694 {
696  .name = name,
697  .argumentHelp = argumentHelp,
698  .description = description,
699  .require_argument = 1,
700  .processor = &set_absolute_time,
701  .scls = (void *) val};
702 
703  return clo;
704 }
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 250 of file getopt_helpers.c.

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

Referenced by main().

254 {
256  .name = name,
257  .description = description,
258  .processor = &increment_value,
259  .scls = (void *) val};
260 
261  return clo;
262 }
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 407 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().

408 {
410  {.shortName = 'L',
411  .name = "log",
412  .argumentHelp = "LOGLEVEL",
413  .description = gettext_noop ("configure logging to use LOGLEVEL"),
414  .require_argument = 1,
415  .processor = &set_string,
416  .scls = (void *) level};
417 
418  return clo;
419 }
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 272 of file getopt_helpers.c.

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

Referenced by main().

273 {
274  struct GNUNET_GETOPT_CommandLineOption clo = {.shortName = 'V',
275  .name = "verbose",
276  .description =
277  gettext_noop ("be verbose"),
278  .processor = &increment_value,
279  .scls = (void *) level};
280 
281  return clo;
282 }
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 506 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().

507 {
509  {.shortName = 'c',
510  .name = "config",
511  .argumentHelp = "FILENAME",
512  .description = gettext_noop ("use configuration file FILENAME"),
513  .require_argument = 1,
514  .processor = &set_filename,
515  .scls = (void *) fn};
516 
517  return clo;
518 }
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 959 of file getopt_helpers.c.

References GNUNET_GETOPT_CommandLineOption::option_mandatory.

Referenced by main().

960 {
961  opt.option_mandatory = 1;
962  return opt;
963 }
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 973 of file getopt_helpers.c.

References GNUNET_GETOPT_CommandLineOption::option_exclusive.

Referenced by main().

974 {
975  opt.option_exclusive = 1;
976  return opt;
977 }
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 893 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, FPRINTF, 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().

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