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" 30 #include <type_traits> 34 extern int main(
int argc,
char** argv);
75 const std::function<std::string()> ext_help;
76 bool set_cmdline =
false;
90 virtual int parse(std::string arg) = 0;
116 virtual void to_string(std::vector<std::string>& out) = 0;
133 const std::function<int(std::string)>
operate;
149 void to_string(std::vector<std::string>& UNUSED(out))
override {}
160 template <
typename T>
170 const std::function<int(T&, std::string)> parser;
201 std::string name, T val, std::function<
int(T&, std::string)> parser, std::function<std::string()> 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());
261 template <
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) :
318 std::function<
int(T&, U&, std::string)> parser, std::function<std::string()> 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;
728 namespace StandardConfigParsers {
733 template <
typename T>
735 from_string(T& var, std::string arg)
738 var = SST::Core::from_string<T>(arg);
740 catch ( std::exception& e ) {
741 fprintf(stderr,
"ERROR: For option \"%s\", failed to parse argument: \"%s\"\n",
752 template <
typename T>
754 from_string_default(T& var, std::string arg,
const T& default_value)
760 var = SST::Core::from_string<T>(arg);
762 catch ( std::exception& e ) {
763 fprintf(stderr,
"ERROR: For option \"%s\", failed to parse argument: \"%s\"\n",
776 template <
typename T>
778 check_parse_store_string(std::string& var, std::string arg)
781 int ret = from_string<T>(check, arg);
782 if ( ret != 0 )
return ret;
792 int nonempty_string(std::string& var, std::string arg);
801 int append_string(std::string pre, std::string post, std::string& var, std::string arg);
807 int flag_set_true(
bool& var, std::string arg);
813 int flag_set_false(
bool& var, std::string arg);
820 int flag_default_true(
bool& var, std::string arg);
827 int flag_default_false(
bool& var, std::string arg);
835 int wall_time_to_seconds(uint32_t& var, std::string arg);
843 int element_name(std::string& var, std::string arg);
849 #endif // SST_CORE_CONFIGBASE_H This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition: serializer.h:42
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
void serialize(SST::Core::Serialization::serializer &ser) override
Function called to serialize the data in the OptionDefinition.
Definition: configBase.h:237
OptionDefinitionNoVar(std::function< int(std::string)> operate)
Constructor.
Definition: configBase.h:141
static std::string currently_parsing_option
Variable used to identify the currently parsing option.
Definition: configBase.h:579
void addPositionalCallback(std::function< int(int num, const char *arg)> callback)
Add support for positional args.
Definition: configBase.cc:268
int parse(std::string arg) override
Function called to parse the option.
Definition: configBase.h:146
bool wasOptionSetOnCmdLine(const std::string &option)
Check to see if an option was set on the command line.
Definition: configBase.cc:575
void serialize(SST::Core::Serialization::serializer &ser) override
Function called to serialize the data in the OptionDefinition.
Definition: configBase.h:339
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
void addHeading(const char *desc)
Adds a heading to the usage output.
Definition: configBase.cc:242
Struct that holds all the getopt_long options along with the documentation for the option...
Definition: configBase.h:505
bool getAnnotation(const std::string &entryName, char annotation)
Get the value of an annotation for an option.
Definition: configBase.cc:587
virtual std::string getUsagePrelude()
Called to get the prelude for the help/usage message.
Definition: configBase.cc:250
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 enableDashDashSupport(std::function< int(const char *arg)> callback)
Enable support for everything after – to be passed to a callback.
Definition: configBase.cc:262
virtual void serialize(SST::Core::Serialization::serializer &ser)=0
Function called to serialize the data in the OptionDefinition.
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
size_t getAnnotationIndex(char annotation)
Get the index in the annotation vector for the given annotation.
Definition: configBase.cc:613
void transfer(OptionDefinition *def) override
Function called to transfer the value(s) of a separate OptionDefinition to the current OptionDefiniti...
Definition: configBase.h:235
Definition: configBase.h:525
virtual void transfer(OptionDefinition *def)=0
Function called to transfer the value(s) of a separate OptionDefinition to the current OptionDefiniti...
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
OptionDefinitionImpl(std::string name, T val, std::function< int(T &, std::string)> parser, std::function< std::string()> ext_help)
Constructor.
Definition: configBase.h:200
OptionDefinitionImpl(std::string name, T val, std::function< int(T &, std::string)> parser)
Constructor.
Definition: configBase.h:181
int printUsage()
Called to print the help/usage message.
Definition: configBase.cc:275
int parseCmdLine(int argc, char *argv[], bool ignore_unknown=false)
Parse command-line arguments to update configuration values.
Definition: configBase.cc:415
OptionDefinition representing options that don't require a value.
Definition: configBase.h:126
Class use to represent OptionDefinitions that store a value.
Definition: configBase.h:161
int printExtHelp(const std::string &option)
Called to print the extended help for an option.
Definition: configBase.cc:388
void addAnnotation(const AnnotationInfo &info)
Add an annotation available to the options.
Definition: configBase.cc:408
void transfer(OptionDefinition *def) override
Function called to transfer the value(s) of a separate OptionDefinition to the current OptionDefiniti...
Definition: configBase.h:333
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
int parse(std::string arg) override
Function called to parse the option.
Definition: configBase.h:332
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
Base class for defining options available in the various config classes.
Definition: configBase.h:67
int parse(std::string arg) override
Function called to parse the option.
Definition: configBase.h:233
ConfigBase()
Default constructor used for serialization.
Definition: configBase.h:591
Base class to parse command line options for SST Simulation Configuration variables.
Definition: configBase.h:573
bool setOptionExternal(const std::string &entryName, const std::string &value)
Set a configuration string to update configuration values.
Definition: configBase.cc:558
std::string getRunName()
Get the name of the executable being run.
Definition: configBase.h:652
virtual int checkArgsAfterParsing()
Function that will be called at the end of parsing so that error checking can be done.
Definition: configBase.cc:256
Class use to represent OptionDefinitions that need to store two values.
Definition: configBase.h:262
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.