carl  24.04
Computer ARithmetic Library
carl::settings::SettingsParser Class Reference

Base class for a settings parser. More...

#include <SettingsParser.h>

Collaboration diagram for carl::settings::SettingsParser:

Public Member Functions

virtual ~SettingsParser ()=default
 Virtual destructor. More...
 
void finalize ()
 Finalizes the parser. More...
 
po::options_description & add (const std::string &title)
 Adds a new options_description with a title and a reference to the settings object. More...
 
template<typename F >
void add_finalizer (F &&f)
 Adds a finalizer function to be called after parsing. More...
 
void parse_options (int argc, char *argv[], bool allow_unregistered=true)
 Parse the options. More...
 
OptionPrinter print_help () const
 Print a help page. More...
 
SettingsPrinter print_options () const
 Print the parsed settings. More...
 

Protected Member Functions

void warn_for_unrecognized (const po::parsed_options &parsed) const
 Checks for unrecognized options that were found. More...
 
void parse_command_line (int argc, char *argv[], bool allow_unregistered)
 Parses the command line. More...
 
void parse_config_file (bool allow_unregistered)
 Parses the config file if one was configured. More...
 
bool finalize_settings ()
 Calls the finalizer functions. More...
 
virtual void warn_for_unrecognized_option (const std::string &s) const
 Prints a warning if an option was unrecognized. Can be overridden. More...
 
virtual void warn_config_file (const std::string &file) const
 Prints a warning if loading the config file failed. Can be overridden. More...
 
virtual std::string name_of_config_file () const
 Gives the option name for the config file name. Can be overridden. More...
 

Protected Attributes

char * argv_zero = nullptr
 Stores the name of the current binary. More...
 
po::positional_options_description mPositional
 Stores the positional arguments. More...
 
po::options_description mAllOptions
 Accumulates all available options. More...
 
po::variables_map mValues
 Stores the parsed values. More...
 
std::vector< po::options_description > mOptions
 Stores the individual options until the parser is finalized. More...
 
std::vector< std::function< bool()> > mFinalizer
 Stores hooks for setting object finalizer functions. More...
 

Friends

std::ostream & settings::operator<< (std::ostream &os, settings::OptionPrinter op)
 
std::ostream & settings::operator<< (std::ostream &os, settings::SettingsPrinter sp)
 

Detailed Description

Base class for a settings parser.

Definition at line 81 of file SettingsParser.h.

Constructor & Destructor Documentation

◆ ~SettingsParser()

virtual carl::settings::SettingsParser::~SettingsParser ( )
virtualdefault

Virtual destructor.

Member Function Documentation

◆ add()

po::options_description& carl::settings::SettingsParser::add ( const std::string &  title)
inline

Adds a new options_description with a title and a reference to the settings object.

The settings object is needed to pass it to the finalizer function.

Definition at line 130 of file SettingsParser.h.

◆ add_finalizer()

template<typename F >
void carl::settings::SettingsParser::add_finalizer ( F &&  f)
inline

Adds a finalizer function to be called after parsing.

boost::program_options::notify() is called before running the finalizer functions. The finalizer function should accept a boost::program_options::variables_map as its only argument and should return a bool indicating whether it changed the variables map. If any finalizer changed the variables map, boost::program_options::notify() is called again afterwards.

Definition at line 140 of file SettingsParser.h.

◆ finalize()

void carl::settings::SettingsParser::finalize ( )

Finalizes the parser.

Definition at line 95 of file SettingsParser.cpp.

◆ finalize_settings()

bool carl::settings::SettingsParser::finalize_settings ( )
protected

Calls the finalizer functions.

Definition at line 89 of file SettingsParser.cpp.

Here is the caller graph for this function:

◆ name_of_config_file()

virtual std::string carl::settings::SettingsParser::name_of_config_file ( ) const
inlineprotectedvirtual

Gives the option name for the config file name. Can be overridden.

Definition at line 116 of file SettingsParser.h.

Here is the caller graph for this function:

◆ parse_command_line()

void carl::settings::SettingsParser::parse_command_line ( int  argc,
char *  argv[],
bool  allow_unregistered 
)
protected

Parses the command line.

Definition at line 64 of file SettingsParser.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_config_file()

void carl::settings::SettingsParser::parse_config_file ( bool  allow_unregistered)
protected

Parses the config file if one was configured.

Definition at line 76 of file SettingsParser.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_options()

void carl::settings::SettingsParser::parse_options ( int  argc,
char *  argv[],
bool  allow_unregistered = true 
)

Parse the options.

If allow_unregistered is set to true, we allow them but call warn_for_unrecognized_option() for each one. Otherwise an exception is raised when an unrecognized option is encountered.

Definition at line 101 of file SettingsParser.cpp.

Here is the call graph for this function:

◆ print_help()

OptionPrinter carl::settings::SettingsParser::print_help ( ) const
inline

Print a help page.

Returns a helper object so that it can be used as follows: std::cout << parser.print_help() << std::endl;

Definition at line 156 of file SettingsParser.h.

◆ print_options()

SettingsPrinter carl::settings::SettingsParser::print_options ( ) const
inline

Print the parsed settings.

Returns a helper object so that it can be used as follows: std::cout << parser.print_options() << std::endl;

Definition at line 165 of file SettingsParser.h.

◆ warn_config_file()

virtual void carl::settings::SettingsParser::warn_config_file ( const std::string &  file) const
inlineprotectedvirtual

Prints a warning if loading the config file failed. Can be overridden.

Definition at line 112 of file SettingsParser.h.

Here is the caller graph for this function:

◆ warn_for_unrecognized()

void carl::settings::SettingsParser::warn_for_unrecognized ( const po::parsed_options &  parsed) const
protected

Checks for unrecognized options that were found.

Definition at line 57 of file SettingsParser.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ warn_for_unrecognized_option()

virtual void carl::settings::SettingsParser::warn_for_unrecognized_option ( const std::string &  s) const
inlineprotectedvirtual

Prints a warning if an option was unrecognized. Can be overridden.

Definition at line 108 of file SettingsParser.h.

Here is the caller graph for this function:

Friends And Related Function Documentation

◆ settings::operator<< [1/2]

std::ostream& settings::operator<< ( std::ostream &  os,
settings::OptionPrinter  op 
)
friend

◆ settings::operator<< [2/2]

std::ostream& settings::operator<< ( std::ostream &  os,
settings::SettingsPrinter  sp 
)
friend

Field Documentation

◆ argv_zero

char* carl::settings::SettingsParser::argv_zero = nullptr
protected

Stores the name of the current binary.

Definition at line 86 of file SettingsParser.h.

◆ mAllOptions

po::options_description carl::settings::SettingsParser::mAllOptions
protected

Accumulates all available options.

Definition at line 90 of file SettingsParser.h.

◆ mFinalizer

std::vector<std::function<bool()> > carl::settings::SettingsParser::mFinalizer
protected

Stores hooks for setting object finalizer functions.

Definition at line 96 of file SettingsParser.h.

◆ mOptions

std::vector<po::options_description> carl::settings::SettingsParser::mOptions
protected

Stores the individual options until the parser is finalized.

Definition at line 94 of file SettingsParser.h.

◆ mPositional

po::positional_options_description carl::settings::SettingsParser::mPositional
protected

Stores the positional arguments.

Definition at line 88 of file SettingsParser.h.

◆ mValues

po::variables_map carl::settings::SettingsParser::mValues
protected

Stores the parsed values.

Definition at line 92 of file SettingsParser.h.


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