goptions
Loading...
Searching...
No Matches
GOptions Class Reference

Parses, stores, and exposes command-line options and YAML configuration values. More...

#include <goptions.h>

Public Member Functions

 GOptions ()
 Default constructor.
 
 GOptions (std::string name)
 Constructor for creating verbosity/debug schema helpers.
 
 GOptions (int argc, char *argv[], const GOptions &user_defined_options=GOptions())
 Main constructor: registers options and parses inputs (YAML + command line).
 
 ~GOptions ()
 Destructor.
 
void defineSwitch (const std::string &name, const std::string &description)
 Defines and adds a command-line switch.
 
void defineOption (const GVariable &gvar, const std::string &help)
 Defines and adds a scalar option.
 
void defineOption (const std::string &name, const std::string &description, const std::vector< GVariable > &gvars, const std::string &help)
 Defines and adds a structured option (map or sequence of maps).
 
int getScalarInt (const std::string &tag) const
 Retrieves the value of a scalar integer option.
 
double getScalarDouble (const std::string &tag) const
 Retrieves the value of a scalar double option.
 
std::string getScalarString (const std::string &tag) const
 Retrieves the value of a scalar string option.
 
bool getSwitch (const std::string &tag) const
 Retrieves the status of a switch.
 
YAML::Node getOptionNode (const std::string &tag) const
 Retrieves the YAML node for the specified option.
 
YAML::Node getOptionMapInNode (const std::string &option_name, const std::string &map_key) const
 Retrieves a map entry value from a structured option stored as a sequence of maps.
 
int getVerbosityFor (const std::string &tag) const
 Retrieves the verbosity level for the specified tag.
 
int getDebugFor (const std::string &tag) const
 Retrieves the debug level for the specified tag.
 
const std::vector< GOption > & getOptions () const
 Returns the list of defined options.
 
const std::map< std::string, GSwitch > & getSwitches () const
 Returns the map of defined switches.
 
void addGOptions (const GOptions &src)
 Merges options and switches from another GOptions : into this one.
 
void addOptionTitle (const std::string &name)
 
template<typename T >
get_variable_in_option (const YAML::Node &node, const std::string &variable_name, const T &default_value)
 Retrieves a typed variable from a YAML node within an option.
 
std::vector< std::string > getYamlFiles () const
 Returns the list of YAML file paths detected on the command line.
 
bool doesOptionExist (const std::string &tag) const
 Checks if an option exists.
 

Data Fields

std::string option_verbosity_name {UNINITIALIZEDSTRINGQUANTITY}
 Name used when constructing the verbosity/debug schema helper.
 
std::vector< GVariableoption_verbosity_names
 Schema entries used to define the verbosity and debug structured options.
 

Detailed Description

GOptions : manages two categories of configuration inputs:

  • Switches (GSwitch :): boolean flags toggled by presence (e.g., -gui).
  • Options (GOption :): values that can be scalar or structured (map/sequence), set via YAML files and/or -name=value command line arguments.

Parsing precedence is:

  1. YAML file(s) (in the order they appear on the command line)
  2. Command-line options, which overwrite YAML values

The main constructor GOptions(argc, argv, user_defined_options) performs parsing immediately and may call exit() for help/version requests or invalid inputs.

Typical lifecycle:

  1. Build a definition-only GOptions : instance (often in a plugin/framework function).
  2. Construct a parsing GOptions : instance in main() using that definition set.
  3. Query resolved values using typed getters or direct YAML node access.

Definition at line 45 of file goptions.h.

Constructor & Destructor Documentation

◆ GOptions() [1/3]

GOptions::GOptions ( )
inline

Creates an empty GOptions : instance. This is primarily used to build up user-defined option sets (e.g., in plugin/framework defineOptions() functions) that will later be merged into a parsing instance.

Definition at line 56 of file goptions.h.

◆ GOptions() [2/3]

GOptions::GOptions ( std::string name)
inlineexplicit

This convenience constructor registers one schema entry (via addOptionTitle()) into option_verbosity_names. It is used to populate the standard verbosity and debug structured options.

Parameters
nameThe verbosity/debug key name to add to the schema.

Definition at line 69 of file goptions.h.

◆ GOptions() [3/3]

GOptions::GOptions ( int argc,
char * argv[],
const GOptions & user_defined_options = GOptions() )

The parsing pipeline performs:

  • registration of built-in switches/options,
  • merging of any user-defined switches/options,
  • parsing of YAML files present in argv,
  • parsing of command-line tokens (including dot-notation for structured sub-options),
  • writing of a resolved configuration YAML snapshot.
Parameters
argcNumber of command-line arguments.
argvArray of command-line argument strings.
user_defined_optionsA GOptions : containing additional options/switches to register.

Definition at line 38 of file goptions.cc.

◆ ~GOptions()

GOptions::~GOptions ( )
inline

Owns and deletes yamlConf if it was allocated by the parsing constructor. This ensures file handles are closed and memory is released deterministically.

Definition at line 96 of file goptions.h.

Member Function Documentation

◆ addGOptions()

void GOptions::addGOptions ( const GOptions & src)
inline

Merge rules:

  • Options are merged by option name; an option is added only if absent.
  • Switches are inserted by name; existing names remain unchanged.
  • Verbosity/debug schema names are merged by key name; duplicates are avoided.

This function enables a plugin/framework pattern:

goptions += pluginA::defineOptions();
goptions += pluginB::defineOptions();
Parameters
srcSource GOptions : to merge into this instance.

Definition at line 286 of file goptions.h.

◆ addOptionTitle()

void GOptions::addOptionTitle ( const std::string & name)
inline

Definition at line 331 of file goptions.h.

◆ defineOption() [1/2]

void GOptions::defineOption ( const GVariable & gvar,
const std::string & help )

Scalar options hold a single value (int/double/string/bool expressed as string in YAML) and are set using -name=value.

Parameters
gvarOption descriptor (name, default value, description).
helpAdditional detailed help text.

Definition at line 204 of file goptions.cc.

◆ defineOption() [2/2]

void GOptions::defineOption ( const std::string & name,
const std::string & description,
const std::vector< GVariable > & gvars,
const std::string & help )

A structured option is described by a vector of GVariable : schema entries. If any schema entry uses goptions::NODFLT : as its default, that key becomes mandatory and the option becomes cumulative.

Parameters
nameOption name (without leading '-').
descriptionBrief description shown in summary help.
gvarsSchema definitions (keys, defaults, descriptions).
helpDetailed help text and examples.

Definition at line 216 of file goptions.cc.

◆ defineSwitch()

void GOptions::defineSwitch ( const std::string & name,
const std::string & description )

Switches are presence-based boolean flags (default off) and are activated by specifying -<name> on the command line.

Registration rules:

  • A switch name must be unique.
  • Defining the same switch twice is a configuration error and results in an exit code.
Parameters
nameSwitch name (without leading '-').
descriptionText shown in help output.

Definition at line 192 of file goptions.cc.

◆ doesOptionExist()

bool GOptions::doesOptionExist ( const std::string & tag) const
Parameters
tagOption name.
Returns
True if the option exists; false otherwise.

Definition at line 299 of file goptions.cc.

◆ get_variable_in_option()

template<typename T >
template bool GOptions::get_variable_in_option< bool > ( const YAML::Node & node,
const std::string & variable_name,
const T & default_value )

Utility for structured options where individual keys may or may not be present. If the key is absent, the caller-provided default_value is returned.

Template Parameters
TThe type of the variable.
Parameters
nodeYAML node to query.
variable_nameKey name.
default_valueFallback value when key is absent.
Returns
Parsed value or fallback.

Definition at line 413 of file goptions.cc.

◆ getDebugFor()

int GOptions::getDebugFor ( const std::string & tag) const

Accepts values as either booleans ("true"/"false") or integers. This allows a command line such as:

  • -debug.general=true
  • -debug.general=1
Parameters
tagDebug key (e.g., "general").
Returns
Debug level as integer.

Definition at line 445 of file goptions.cc.

◆ getOptionMapInNode()

YAML::Node GOptions::getOptionMapInNode ( const std::string & option_name,
const std::string & map_key ) const

This is commonly used for options like verbosity and debug, where each sequence element is a {key: value} map.

The function searches the sequence for a map whose key matches map_key and returns the corresponding YAML node (which may itself be a scalar, map, or sequence).

Parameters
option_nameStructured option name.
map_keyKey to retrieve.
Returns
YAML node associated with map_key .

Definition at line 395 of file goptions.cc.

◆ getOptionNode()

YAML::Node GOptions::getOptionNode ( const std::string & tag) const
inline

This provides direct access to the YAML node underlying the option, enabling clients to inspect structured content without additional copying.

This is typically used when:

  • the option stores a sequence of maps and the caller wants custom logic,
  • the caller wants to forward the node to another component without re-parsing.
Parameters
tagOption name.
Returns
YAML node representing the option value.
Warning
If the option does not exist, this function exits with EC__NOOPTIONFOUND .

Definition at line 206 of file goptions.h.

◆ getOptions()

const std::vector< GOption > & GOptions::getOptions ( ) const
inline
Returns
Const reference to internal option vector.

Definition at line 260 of file goptions.h.

◆ getScalarDouble()

double GOptions::getScalarDouble ( const std::string & tag) const
Parameters
tagOption name.
Returns
Value converted to double.

Definition at line 241 of file goptions.cc.

◆ getScalarInt()

int GOptions::getScalarInt ( const std::string & tag) const

See getScalarString() for YAML null sentinel behavior.

Parameters
tagOption name.
Returns
Value converted to int.

Definition at line 230 of file goptions.cc.

◆ getScalarString()

std::string GOptions::getScalarString ( const std::string & tag) const

If the underlying YAML node is null, returns the literal sentinel "NULL". This sentinel is intentionally explicit so downstream code can distinguish "unset" from an empty string.

Parameters
tagOption name.
Returns
Value as string (or "NULL").

Definition at line 252 of file goptions.cc.

◆ getSwitch()

bool GOptions::getSwitch ( const std::string & tag) const
Parameters
tagSwitch name.
Returns
True if enabled; false otherwise.

Definition at line 382 of file goptions.cc.

◆ getSwitches()

const std::map< std::string, GSwitch > & GOptions::getSwitches ( ) const
inline
Returns
Const reference to internal switch map.

Definition at line 267 of file goptions.h.

◆ getVerbosityFor()

int GOptions::getVerbosityFor ( const std::string & tag) const

The verbosity option is stored as a sequence of single-entry maps. This helper finds the entry whose key matches tag and returns the associated integer.

Parameters
tagVerbosity key (e.g., "ghits").
Returns
Verbosity level as integer.

Definition at line 431 of file goptions.cc.

◆ getYamlFiles()

std::vector< std::string > GOptions::getYamlFiles ( ) const
inline

YAML files are detected by extension (".yaml" or ".yml") and are parsed in argv order. This order matters because later YAML files overwrite earlier YAML values, and command-line tokens overwrite all YAML values.

Returns
Vector of YAML file path strings.

Definition at line 362 of file goptions.h.

Field Documentation

◆ option_verbosity_name

std::string GOptions::option_verbosity_name {UNINITIALIZEDSTRINGQUANTITY}

Defaults to UNINITIALIZEDSTRINGQUANTITY and is typically set by the GOptions(name) helper constructor.

Definition at line 320 of file goptions.h.

◆ option_verbosity_names

std::vector<GVariable> GOptions::option_verbosity_names

Each GVariable : in this vector is used as a schema key (e.g., "general", "ghits", etc.) with an integer default value and a short description.

Definition at line 329 of file goptions.h.


The documentation for this class was generated from the following files: