|
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) |
| 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 > | |
| 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:
-gui).-name=value command line arguments.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:
main() using that definition set.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 38 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.
Merge rules:
This function enables a plugin/framework pattern:
| src | Source GOptions : to merge into this instance. |
Definition at line 286 of file goptions.h.
Definition at line 331 of file goptions.h.
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 204 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 216 of file goptions.cc.
Switches are presence-based boolean flags (default off) and are activated by specifying -<name> on the command line.
Registration rules:
| name | Switch name (without leading '-'). |
| description | Text shown in help output. |
Definition at line 192 of file goptions.cc.
| tag | Option name. |
Definition at line 299 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 413 of file goptions.cc.
Accepts values as either booleans ("true"/"false") or integers. This allows a command line such as:
-debug.general=true-debug.general=1| tag | Debug key (e.g., "general"). |
Definition at line 445 of file goptions.cc.
| 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).
| option_name | Structured option name. |
| map_key | Key to retrieve. |
map_key . Definition at line 395 of file goptions.cc.
|
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:
| tag | Option name. |
EC__NOOPTIONFOUND . Definition at line 206 of file goptions.h.
Definition at line 260 of file goptions.h.
| tag | Option name. |
Definition at line 241 of file goptions.cc.
See getScalarString() for YAML null sentinel behavior.
| tag | Option name. |
Definition at line 230 of file goptions.cc.
| 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.
| tag | Option name. |
"NULL"). Definition at line 252 of file goptions.cc.
| tag | Switch name. |
Definition at line 382 of file goptions.cc.
Definition at line 267 of file goptions.h.
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 431 of file goptions.cc.
|
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.
Definition at line 362 of file goptions.h.
| 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.
| 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.