12#ifndef SST_CORE_CONFIGBASE_H
13#define SST_CORE_CONFIGBASE_H
15#include "sst/core/from_string.h"
16#include "sst/core/serialization/serializer_fwd.h"
17#include "sst/core/sst_types.h"
18#include "sst/core/warnmacros.h"
34extern int main(
int argc,
char** argv);
56void option_serialize_data(SST::Core::Serialization::serializer& ser, T& val);
67struct OptionDefinition
70 OptionDefinition(std::function<std::string()> ext_help) :
75 const std::function<std::string()> ext_help;
76 bool set_cmdline =
false;
78 virtual ~OptionDefinition() =
default;
90 virtual int parse(std::string arg) = 0;
98 virtual void transfer(OptionDefinition* def) = 0;
116 virtual void to_string(std::vector<std::string>& out) = 0;
133 const std::function<int(std::string)>
operate;
142 OptionDefinition(nullptr),
149 void to_string(std::vector<std::string>& UNUSED(out))
override {}
170 const std::function<int(T&, std::string)> parser;
182 OptionDefinition(nullptr),
201 std::string name, T val, std::function<
int(T&, std::string)> parser, std::function<std::string()> ext_help) :
202 OptionDefinition(ext_help),
225 operator T()
const {
return value; }
233 int parse(std::string arg)
override {
return parser(value, arg); }
242 if constexpr ( std::is_same_v<T, std::string> ) {
243 s << name <<
"_ = " <<
"\"" << value <<
"\"";
246 s << name <<
"_ = " << value;
248 out.push_back(s.str());
261template <
typename T,
typename U>
276 const std::function<int(T&, U&, std::string)> parser;
292 std::string name1, T val1, std::string name2, U val2, std::function<
int(T&, U&, std::string)> parser) :
293 OptionDefinition(nullptr),
318 std::function<
int(T&, U&, std::string)> parser, std::function<std::string()> ext_help) :
319 OptionDefinition(ext_help),
332 int parse(std::string arg)
override {
return parser(value1, value2, arg); }
341 Impl::option_serialize_data(ser, value1);
342 Impl::option_serialize_data(ser, value2);
348 if constexpr ( std::is_same_v<T, std::string> ) {
349 s << name1 <<
"_ = " <<
"\"" << value1 <<
"\"";
352 s << name1 <<
"_ = " << value1;
354 out.push_back(s.str());
358 if constexpr ( std::is_same_v<U, std::string> ) {
359 s << name2 <<
"_ = " <<
"\"" << value2 <<
"\"";
362 s << name2 <<
"_ = " << value2;
364 out.push_back(s.str());
371#define SST_CONFIG_APPEND(first, second) first##second
408#define SST_CONFIG_DECLARE_OPTION(type, name, default_val, ...) \
413 return SST_CONFIG_APPEND(name, _).value; \
417 OptionDefinitionImpl<type> SST_CONFIG_APPEND(name, _) = { #name, default_val, ##__VA_ARGS__ };
464#define SST_CONFIG_DECLARE_OPTION_PAIR(type1, name1, default_val1, type2, name2, default_val2, ...) \
467 type1 name1() const \
469 return SST_CONFIG_APPEND(name1, _).value1; \
472 type2 name2() const \
474 return SST_CONFIG_APPEND(name1, _).value2; \
478 OptionDefinitionPair<type1, type2> SST_CONFIG_APPEND( \
479 name1, _) = { #name1, default_val1, #name2, default_val2, ##__VA_ARGS__ };
494#define SST_CONFIG_DECLARE_OPTION_NOVAR(name, ...) \
497 OptionDefinitionNoVar SST_CONFIG_APPEND(name, _) = { __VA_ARGS__ };
511 std::vector<bool> annotations;
514 LongOption(
struct option opt,
const char* argname,
const char* desc,
bool header, std::vector<bool> annotations,
520 annotations(annotations),
548#define DEF_FLAG_OPTVAL(longName, shortName, text, def, ...) \
549 addOption({ longName, optional_argument, 0, shortName }, "[BOOL]", text, { __VA_ARGS__ }, &def);
551#define DEF_FLAG(longName, shortName, text, def, ...) \
552 addOption({ longName, no_argument, 0, shortName }, "", text, { __VA_ARGS__ }, &def);
554#define DEF_ARG(longName, shortName, argName, text, def, ...) \
555 addOption({ longName, required_argument, 0, shortName }, argName, text, { __VA_ARGS__ }, &def);
557#define DEF_ARG_OPTVAL(longName, shortName, argName, text, def, ...) \
558 addOption({ longName, optional_argument, 0, shortName }, "[" argName "]", text, { __VA_ARGS__ }, &def);
561#define DEF_SECTION_HEADING(text) addHeading(text);
616 struct option opt,
const char* argname,
const char* desc, std::vector<bool> annotations,
OptionDefinition* def);
658 bool getAnnotation(
const std::string& entryName,
char annotation);
693 int parseCmdLine(
int argc,
char* argv[],
bool ignore_unknown =
false);
704 std::vector<LongOption> options;
706 void enable_printing() { suppress_print_ =
false; }
709 std::map<char, int> short_options;
710 std::string short_options_string;
711 size_t longest_option = 0;
712 size_t num_options = 0;
713 std::function<int(
const char* arg)> dashdash_callback;
714 std::function<int(
int num,
const char* arg)> positional_args;
717 std::map<std::string, std::function<std::string()>> extra_help_map;
720 std::vector<AnnotationInfo> annotations_;
722 std::string run_name_;
723 bool suppress_print_ =
true;
724 bool has_extended_help_ =
false;
728namespace StandardConfigParsers {
735from_string(T& var, std::string arg)
738 var = SST::Core::from_string<T>(arg);
740 catch (
const std::exception& e ) {
741 fprintf(stderr,
"ERROR: For option \"%s\", failed to parse argument: \"%s\"\n",
754from_string_default(T& var, std::string arg,
const T& default_value)
760 var = SST::Core::from_string<T>(arg);
762 catch (
const std::exception& e ) {
763 fprintf(stderr,
"ERROR: For option \"%s\", failed to parse argument: \"%s\"\n",
778check_parse_store_string(std::string& var, std::string arg)
781 int ret = from_string<T>(check, arg);
782 if ( ret != 0 )
return ret;
792int nonempty_string(std::string& var, std::string arg);
801int append_string(std::string pre, std::string post, std::string& var, std::string arg);
807int flag_set_true(
bool& var, std::string arg);
813int flag_set_false(
bool& var, std::string arg);
820int flag_default_true(
bool& var, std::string arg);
827int flag_default_false(
bool& var, std::string arg);
835int wall_time_to_seconds(uint32_t& var, std::string arg);
843int element_name(std::string& var, std::string arg);
Base class to parse command line options for SST Simulation Configuration variables.
Definition configBase.h:574
int printExtHelp(const std::string &option)
Called to print the extended help for an option.
Definition configBase.cc:389
int parseCmdLine(int argc, char *argv[], bool ignore_unknown=false)
Parse command-line arguments to update configuration values.
Definition configBase.cc:416
static uint32_t parseWallTimeToSeconds(const std::string &arg, bool &success, const std::string &option)
Function to parse a string to wall time using one of the following formats:
Definition configBase.cc:144
virtual int checkArgsAfterParsing()
Function that will be called at the end of parsing so that error checking can be done.
Definition configBase.cc:256
std::string getRunName()
Get the name of the executable being run.
Definition configBase.h:652
bool wasOptionSetOnCmdLine(const std::string &option)
Check to see if an option was set on the command line.
Definition configBase.cc:576
virtual std::string getUsagePrelude()
Called to get the prelude for the help/usage message.
Definition configBase.cc:250
void addOption(struct option opt, const char *argname, const char *desc, std::vector< bool > annotations, OptionDefinition *def)
Add options to the Config object.
Definition configBase.cc:193
void addAnnotation(const AnnotationInfo &info)
Add an annotation available to the options.
Definition configBase.cc:409
void addHeading(const char *desc)
Adds a heading to the usage output.
Definition configBase.cc:242
bool getAnnotation(const std::string &entryName, char annotation)
Get the value of an annotation for an option.
Definition configBase.cc:588
void enableDashDashSupport(std::function< int(const char *arg)> callback)
Enable support for everything after – to be passed to a callback.
Definition configBase.cc:262
bool setOptionExternal(const std::string &entryName, const std::string &value)
Set a configuration string to update configuration values.
Definition configBase.cc:559
void addPositionalCallback(std::function< int(int num, const char *arg)> callback)
Add support for positional args.
Definition configBase.cc:268
ConfigBase()
Default constructor used for serialization.
Definition configBase.h:591
int printUsage()
Called to print the help/usage message.
Definition configBase.cc:275
size_t getAnnotationIndex(char annotation)
Get the index in the annotation vector for the given annotation.
Definition configBase.cc:614
static std::string currently_parsing_option
Variable used to identify the currently parsing option.
Definition configBase.h:579
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition serializer.h:43
Definition configBase.h:526
Class use to represent OptionDefinitions that store a value.
Definition configBase.h:162
void transfer(OptionDefinition *def) override
Function called to transfer the value(s) of a separate OptionDefinition to the current OptionDefiniti...
Definition configBase.h:235
OptionDefinitionImpl(std::string name, T val, std::function< int(T &, std::string)> parser)
Constructor.
Definition configBase.h:181
void serialize(SST::Core::Serialization::serializer &ser) override
Function called to serialize the data in the OptionDefinition.
Definition configBase.h:237
OptionDefinitionImpl(std::string name, T val, std::function< int(T &, std::string)> parser, std::function< std::string()> ext_help)
Constructor.
Definition configBase.h:200
int parse(std::string arg) override
Function called to parse the option.
Definition configBase.h:233
void to_string(std::vector< std::string > &out) override
Adds a string representation to the input vector for each of the underlying variables.
Definition configBase.h:239
OptionDefinitionNoVar(std::function< int(std::string)> operate)
Constructor.
Definition configBase.h:141
int parse(std::string arg) override
Function called to parse the option.
Definition configBase.h:146
const std::function< int(std::string)> operate
std::function that holds a pointer to the function that will either print out the information,...
Definition configBase.h:133
Class use to represent OptionDefinitions that need to store two values.
Definition configBase.h:263
OptionDefinitionPair(std::string name1, T val1, std::string name2, U val2, std::function< int(T &, U &, std::string)> parser)
Constructor.
Definition configBase.h:291
void to_string(std::vector< std::string > &out) override
Adds a string representation to the input vector for each of the underlying variables.
Definition configBase.h:345
OptionDefinitionPair(std::string name1, T val1, std::string name2, U val2, std::function< int(T &, U &, std::string)> parser, std::function< std::string()> ext_help)
Constructor.
Definition configBase.h:317
int parse(std::string arg) override
Function called to parse the option.
Definition configBase.h:332
void serialize(SST::Core::Serialization::serializer &ser) override
Function called to serialize the data in the OptionDefinition.
Definition configBase.h:339
void transfer(OptionDefinition *def) override
Function called to transfer the value(s) of a separate OptionDefinition to the current OptionDefiniti...
Definition configBase.h:333
Base class for defining options available in the various config classes.
Definition configBase.h:68
virtual void serialize(SST::Core::Serialization::serializer &ser)=0
Function called to serialize the data in the OptionDefinition.
virtual int parse(std::string arg)=0
Function called to parse the option.
virtual void to_string(std::vector< std::string > &out)=0
Adds a string representation to the input vector for each of the underlying variables.
virtual void transfer(OptionDefinition *def)=0
Function called to transfer the value(s) of a separate OptionDefinition to the current OptionDefiniti...