5 #include <boost/program_options.hpp>
10 #if __has_include(<filesystem>)
14 inline void validate(boost::any& v,
const std::vector<std::string>& values, std::filesystem::path* ,
int ) {
15 namespace pov = boost::program_options::validators;
16 namespace fs = std::filesystem;
17 fs::path p(pov::get_single_string(values));
29 namespace po = boost::program_options;
36 void default_to(po::variables_map& values,
const std::string& name,
const T& value) {
37 auto it = values.find(name);
38 if (it == values.end()) {
39 values.emplace(name, po::variable_value(boost::any(value),
true));
40 }
else if (it->second.defaulted()) {
41 values.at(name) = po::variable_value(boost::any(value),
false);
50 void overwrite_to(po::variables_map& values,
const std::string& name,
const T& value) {
51 auto it = values.find(name);
52 if (it == values.end()) {
53 values.emplace(name, po::variable_value(boost::any(value),
true));
55 values.at(name) = po::variable_value(boost::any(value),
false);
109 std::cerr <<
"Ignoring unrecognized option " << s << std::endl;
113 std::cerr <<
"Could not load config file " << file << std::endl;
130 po::options_description&
add(
const std::string& title) {
131 return mOptions.emplace_back(po::options_description(title));
149 void parse_options(
int argc,
char* argv[],
bool allow_unregistered =
true);
void default_to(po::variables_map &values, const std::string &name, const T &value)
Inserts value into variables_map if it is not yet set.
void validate(boost::any &v, const std::vector< std::string > &values, carl::settings::duration *, int)
Custom validator for duration that wraps some std::chrono::duration.
void overwrite_to(po::variables_map &values, const std::string &name, const T &value)
Inserts or overwrites value into variables_map.
std::ostream & operator<<(std::ostream &os, const duration &d)
Streaming operator for duration. Auto-detects proper time suffix.
Helper class to nicely print the options that are available.
const SettingsParser & parser
Reference to parser.
Helper class to nicely print the settings that were parsed.
const SettingsParser & parser
Reference to parser.
Base class for a settings parser.
void parse_options(int argc, char *argv[], bool allow_unregistered=true)
Parse the options.
virtual ~SettingsParser()=default
Virtual destructor.
SettingsPrinter print_options() const
Print the parsed settings.
void finalize()
Finalizes the parser.
bool finalize_settings()
Calls the finalizer functions.
po::options_description & add(const std::string &title)
Adds a new options_description with a title and a reference to the settings object.
std::vector< po::options_description > mOptions
Stores the individual options until the parser is finalized.
void parse_config_file(bool allow_unregistered)
Parses the config file if one was configured.
void add_finalizer(F &&f)
Adds a finalizer function to be called after parsing.
virtual void warn_config_file(const std::string &file) const
Prints a warning if loading the config file failed. Can be overridden.
po::options_description mAllOptions
Accumulates all available options.
po::positional_options_description mPositional
Stores the positional arguments.
virtual void warn_for_unrecognized_option(const std::string &s) const
Prints a warning if an option was unrecognized. Can be overridden.
virtual std::string name_of_config_file() const
Gives the option name for the config file name. Can be overridden.
po::variables_map mValues
Stores the parsed values.
void warn_for_unrecognized(const po::parsed_options &parsed) const
Checks for unrecognized options that were found.
void parse_command_line(int argc, char *argv[], bool allow_unregistered)
Parses the command line.
OptionPrinter print_help() const
Print a help page.
std::vector< std::function< bool()> > mFinalizer
Stores hooks for setting object finalizer functions.
char * argv_zero
Stores the name of the current binary.