|
goptions
|
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, bool default_status=false) |
| 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. | |
| void | setOptionValueFromString (const std::string &optionName, const std::string &possibleYamlNode) |
| Updates an option value from a YAML-formatted string. | |
| 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> | |
| 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< GVariable > | option_verbosity_names |
| Schema entries used to define the verbosity and debug structured options. | |
GOptions : manages two categories of configuration inputs:
Parsing precedence is:
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:
Definition at line 45 of file goptions.h.
|
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.
|
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.
| name | The verbosity/debug key name to add to the schema. |
Definition at line 69 of file goptions.h.
The parsing pipeline performs:
| argc | Number of command-line arguments. |
| argv | Array of command-line argument strings. |
| user_defined_options | A GOptions : containing additional options/switches to register. |
Definition at line 55 of file goptions.cc.
|
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.
|
inline |
Merge rules:
This function enables a plugin/framework pattern:
| src | Source GOptions : to merge into this instance. |
Definition at line 302 of file goptions.h.
|
inline |
Definition at line 347 of file goptions.h.
| 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.
| gvar | Option descriptor (name, default value, description). |
| help | Additional detailed help text. |
Definition at line 254 of file goptions.cc.
| 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.
| name | Option name (without leading '-'). |
| description | Brief description shown in summary help. |
| gvars | Schema definitions (keys, defaults, descriptions). |
| help | Detailed help text and examples. |
Definition at line 266 of file goptions.cc.
| void GOptions::defineSwitch | ( | const std::string & | name, |
| const std::string & | description, | ||
| bool | default_status = false ) |
Switches are presence-based boolean flags and are activated by specifying -<name> on the command line. They default to off, but a switch may be defined as on by default via default_status; such a switch can be turned off explicitly (e.g. -<name>=false).
Registration rules:
| name | Switch name (without leading '-'). |
| description | Text shown in help output. |
| default_status | Initial on/off status of the switch (default: off). |
Definition at line 242 of file goptions.cc.
|
nodiscard |
| tag | Option name. |
Definition at line 388 of file goptions.cc.
| 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.
| T | The type of the variable. |
| node | YAML node to query. |
| variable_name | Key name. |
| default_value | Fallback value when key is absent. |
Definition at line 520 of file goptions.cc.
|
nodiscard |
Accepts values as either booleans ("true"/"false") or integers. This allows a command line such as:
| tag | Debug key (e.g., "gemc"). |
Definition at line 552 of file goptions.cc.
|
nodiscard |
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).
| option_name | Structured option name. |
| map_key | Key to retrieve. |
map_key . Definition at line 502 of file goptions.cc.
|
inlinenodiscard |
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:
| tag | Option name. |
EC__NOOPTIONFOUND . Definition at line 208 of file goptions.h.
|
inlinenodiscard |
Definition at line 276 of file goptions.h.
|
nodiscard |
| tag | Option name. |
Definition at line 291 of file goptions.cc.
|
nodiscard |
See getScalarString() for YAML null sentinel behavior.
| tag | Option name. |
Definition at line 280 of file goptions.cc.
|
nodiscard |
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.
| tag | Option name. |
Definition at line 302 of file goptions.cc.
|
nodiscard |
| tag | Switch name. |
Definition at line 489 of file goptions.cc.
|
inlinenodiscard |
Definition at line 283 of file goptions.h.
|
nodiscard |
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.
| tag | Verbosity key (e.g., "ghits"). |
Definition at line 538 of file goptions.cc.
|
inlinenodiscard |
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.
Definition at line 378 of file goptions.h.
| void GOptions::setOptionValueFromString | ( | const std::string & | optionName, |
| const std::string & | possibleYamlNode ) |
This uses the same parser as command-line -name=value options, allowing GUI modules to keep runtime options synchronized with user selections.
| optionName | Option name to update. |
| possibleYamlNode | Scalar, map, or sequence encoded as YAML text. |
Definition at line 471 of file goptions.cc.
| std::string GOptions::option_verbosity_name {UNINITIALIZEDSTRINGQUANTITY} |
Defaults to UNINITIALIZEDSTRINGQUANTITY and is typically set by the GOptions(name) helper constructor.
Definition at line 336 of file goptions.h.
| std::vector<GVariable> GOptions::option_verbosity_names |
Each GVariable : in this vector is used as a schema key (e.g., "gemc", "gparticle", etc.) with an integer default value and a short description.
Definition at line 345 of file goptions.h.