goptions
Loading...
Searching...
No Matches
define_options.cc File Reference

Example program showing how to define and use GOptions : . More...

#include "goptions.h"
#include <iostream>
Include dependency graph for define_options.cc:

Go to the source code of this file.

Functions

GOptions defineOptions ()
 Builds and returns a set of example options (definitions only).
 
int main (int argc, char *argv[])
 Example entry point: parse inputs and read back resolved values.
 

Detailed Description

This example demonstrates an end-to-end pattern commonly used in GEMC-style modules:

  1. Build a definition-only GOptions : instance (no parsing yet).
  2. Construct a parsing GOptions : instance in main() using: GOptions(argc, argv, user_defined_options) .
  3. Retrieve resolved values with typed getters such as getScalarInt() .

It also shows:

  • Defining a boolean switch with defineSwitch() .
  • Defining scalar options with defineOption() .
  • Defining a structured option schema (sequence of maps) and providing a usable command-line example.

Definition in file define_options.cc.

Function Documentation

◆ defineOptions()

GOptions defineOptions ( )

This function intentionally does not parse any inputs. Instead, it returns a GOptions : populated with registrations:

  • switches (presence-based booleans),
  • scalar options,
  • structured option schemas.

The returned object is then passed into the parsing constructor:

auto gopts = std::make_shared<GOptions>(argc, argv, defineOptions());
GOptions defineOptions()

This pattern allows multiple components (modules/plugins) to contribute their own option definitions and then combine them before parsing.

What this example defines:

  • Switch log: dummy switch that can be toggled by -log.
  • Scalar runno: an integer option set via -runno=<value>.
  • Scalar nthreads: an integer option controlling thread usage, with 0 meaning "use all cores".
  • Structured gparticle: a schema representing generator particles as a sequence of maps, including mandatory keys flagged using goptions::NODFLT.
Returns
A GOptions : object populated with example switches and options.

Definition at line 55 of file define_options.cc.

◆ main()

int main ( int argc,
char * argv[] )

This main() illustrates the common consumption flow:

  1. Construct a parsing GOptions : instance using argc/argv and the definitions provided by defineOptions().
  2. Query values from the resolved configuration.

Example invocations:

  • Scalar option from command line:
    define_options -nthreads=8
  • Dot-notation (if supported by the target option; shown here for illustration):
    define_options -verbosity.general=1
  • Structured option payload (quoted so the shell does not split YAML-like characters):
    define_options -gparticle="[{name: e-, p: 2300, theta: 23.0}]"
Parameters
argcNumber of command-line arguments.
argvArray of command-line argument strings.
Returns
Process exit code (EXIT_SUCCESS on success).

Definition at line 116 of file define_options.cc.