SST 15.0
Structural Simulation Toolkit
configBase.h
1// Copyright 2009-2025 NTESS. Under the terms
2// of Contract DE-NA0003525 with NTESS, the U.S.
3// Government retains certain rights in this software.
4//
5// Copyright (c) 2009-2025, NTESS
6// All rights reserved.
7//
8// This file is part of the SST software package. For license
9// information, see the LICENSE file in the top level directory of the
10// distribution.
11
12#ifndef SST_CORE_CONFIGBASE_H
13#define SST_CORE_CONFIGBASE_H
14
15#include "sst/core/sst_types.h"
16
17#include <functional>
18#include <getopt.h>
19#include <iostream>
20#include <map>
21#include <string>
22#include <vector>
23
24/* Forward declare for Friendship */
25extern int main(int argc, char** argv);
26
27namespace SST {
28
29/**
30 Struct that holds all the getopt_long options along with the
31 docuementation for the option
32*/
33struct LongOption
34{
35 struct option opt;
36 std::string argname;
37 std::string desc;
38 std::function<int(const char* arg)> callback;
39 bool header; // if true, desc is actually the header
40 std::vector<bool> annotations;
41 std::function<std::string()> ext_help;
42 mutable bool set_cmdline;
43
44 LongOption(struct option opt, const char* argname, const char* desc,
45 const std::function<int(const char* arg)>& callback, bool header, std::vector<bool> annotations,
46 std::function<std::string()> ext_help, bool set_cmdline) :
47 opt(opt),
48 argname(argname),
49 desc(desc),
50 callback(callback),
51 header(header),
52 annotations(annotations),
53 ext_help(ext_help),
54 set_cmdline(set_cmdline)
55 {}
56};
57
59{
60 char annotation;
61 std::string help;
62};
63
64// Macros to make defining options easier. These must be called
65// inside of a member function of a class inheriting from ConfigBase
66// Nomenaclature is:
67
68// FLAG - value is either true or false. FLAG defaults to no arguments allowed
69// ARG - value is a string. ARG defaults to required argument
70// OPTVAL - Takes an optional paramater
71
72// longName - multicharacter name referenced using --
73// shortName - single character name referenced using -
74// text - help text
75// func - function called if option is found
76#define DEF_FLAG_OPTVAL(longName, shortName, text, func, ...) \
77 addOption({ longName, optional_argument, 0, shortName }, "[BOOL]", text, func, { __VA_ARGS__ });
78
79#define DEF_FLAG(longName, shortName, text, func, ...) \
80 addOption({ longName, no_argument, 0, shortName }, "", text, func, { __VA_ARGS__ });
81
82#define DEF_ARG(longName, shortName, argName, text, func, ...) \
83 addOption({ longName, required_argument, 0, shortName }, argName, text, func, { __VA_ARGS__ });
84
85#define DEF_ARG_OPTVAL(longName, shortName, argName, text, func, ...) \
86 addOption({ longName, optional_argument, 0, shortName }, "[" argName "]", text, func, { __VA_ARGS__ });
87
88// Macros that include extended help
89#define DEF_FLAG_EH(longName, shortName, text, func, eh, ...) \
90 addOption({ longName, no_argument, 0, shortName }, "", text, func, { __VA_ARGS__ }, eh);
91
92#define DEF_ARG_EH(longName, shortName, argName, text, func, eh, ...) \
93 addOption({ longName, required_argument, 0, shortName }, argName, text, func, { __VA_ARGS__ }, eh);
94
95
96#define DEF_SECTION_HEADING(text) addHeading(text);
97
98
99/**
100 * Base class to parse command line options for SST Simulation
101 * Configuration variables.
102 *
103 * NOTE: This class contains only state for parsing the command line.
104 * All options will be stored in classes derived from this class.
105 * This means that we don't need to be able to serialize anything in
106 * this class.
107 */
109{
110protected:
111 /**
112 ConfigBase constructor. Meant to only be created by main
113 function
114 */
115 explicit ConfigBase(bool suppress_print) :
116 suppress_print_(suppress_print)
117 {}
118
119 /**
120 Default constructor used for serialization. After
121 serialization, the Config object is only used to get the values
122 of set options and it can no longer parse arguments. Given
123 that, it will no longer print anything, so set suppress_print_
124 to true. None of this class needs to be serialized because it
125 it's state is only for parsing the arguments.
126 */
128 suppress_print_(true)
129 {
130 options.reserve(100);
131 }
132
133
134 ConfigBase(bool suppress_print, std::vector<AnnotationInfo> annotations) :
135 annotations_(annotations),
136 suppress_print_(suppress_print)
137 {}
138
139 /**
140 Called to print the help/usage message
141 */
142 int printUsage();
143
144
145 /**
146 Called to print the extended help for an option
147 */
148 int printExtHelp(const std::string& option);
149
150 /**
151 Add options to the Config object. The options will be added in
152 the order they are in the input array, and across calls to the
153 function.
154 */
155 void addOption(struct option opt, const char* argname, const char* desc,
156 std::function<int(const char* arg)> callback, std::vector<bool> annotations,
157 std::function<std::string()> ext_help = std::function<std::string()>());
158
159 /**
160 Adds a heading to the usage output
161 */
162 void addHeading(const char* desc);
163
164 /**
165 Called to get the prelude for the help/usage message
166 */
167 virtual std::string getUsagePrelude();
168
169 // Function that will be called at the end of parsing so that
170 // error checking can be done
171 virtual int checkArgsAfterParsing();
172
173 // Enable support for everything after -- to be passed to a
174 // callback. Each arg will be passed independently to the callback
175 // function
176 void enableDashDashSupport(std::function<int(const char* arg)> callback);
177
178 // Add support for positional args. Must be added in the order the
179 // args show up on the command line
180 void addPositionalCallback(std::function<int(int num, const char* arg)> callback);
181
182
183 /**
184 Get the name of the executable being run. This is only
185 avaialable after parseCmdLine() is called.
186 */
187 std::string getRunName() { return run_name_; }
188
189 /** Set a configuration string to update configuration values */
190 bool setOptionExternal(const std::string& entryName, const std::string& value);
191
192 /** Get the value of an annotation for an option */
193 bool getAnnotation(const std::string& entryName, char annotation);
194
195public:
196 // Function to uniformly parse boolean values for command line
197 // arguments
198 static bool parseBoolean(const std::string& arg, bool& success, const std::string& option);
199
200 static uint32_t parseWallTimeToSeconds(const std::string& arg, bool& success, const std::string& option);
201
202 virtual ~ConfigBase() {}
203
204 /**
205 Parse command-line arguments to update configuration values.
206
207 @return Returns 0 if execution should continue. Returns -1 if
208 there was an error. Returns 1 if run command line only asked
209 for information to be print (e.g. --help or -V, for example).
210 */
211 int parseCmdLine(int argc, char* argv[], bool ignore_unknown = false);
212
213 /**
214 Check to see if an option was set on the command line
215
216 @return True if option was set on command line, false
217 otherwise. Will also return false if option is unknown.
218 */
219 bool wasOptionSetOnCmdLine(const std::string& option);
220
221private:
222 std::vector<LongOption> options;
223 std::map<char, int> short_options;
224 std::string short_options_string;
225 size_t longest_option = 0;
226 size_t num_options = 0;
227 std::function<int(const char* arg)> dashdash_callback;
228 std::function<int(int num, const char* arg)> positional_args;
229
230 // Map to hold extended help function calls
231 std::map<std::string, std::function<std::string()>> extra_help_map;
232
233 // Annotations
234 std::vector<AnnotationInfo> annotations_;
235
236 std::string run_name_;
237 bool suppress_print_;
238 bool has_extended_help_ = false;
239};
240
241} // namespace SST
242
243#endif // SST_CORE_CONFIGBASE_H
Base class to parse command line options for SST Simulation Configuration variables.
Definition configBase.h:109
int printExtHelp(const std::string &option)
Called to print the extended help for an option.
Definition configBase.cc:308
void addOption(struct option opt, const char *argname, const char *desc, std::function< int(const char *arg)> callback, std::vector< bool > annotations, std::function< std::string()> ext_help=std::function< std::string()>())
Add options to the Config object.
Definition configBase.cc:109
int parseCmdLine(int argc, char *argv[], bool ignore_unknown=false)
Parse command-line arguments to update configuration values.
Definition configBase.cc:329
std::string getRunName()
Get the name of the executable being run.
Definition configBase.h:187
bool wasOptionSetOnCmdLine(const std::string &option)
Check to see if an option was set on the command line.
Definition configBase.cc:483
virtual std::string getUsagePrelude()
Called to get the prelude for the help/usage message.
Definition configBase.cc:170
ConfigBase(bool suppress_print)
ConfigBase constructor.
Definition configBase.h:115
void addHeading(const char *desc)
Adds a heading to the usage output.
Definition configBase.cc:161
bool getAnnotation(const std::string &entryName, char annotation)
Get the value of an annotation for an option.
Definition configBase.cc:495
bool setOptionExternal(const std::string &entryName, const std::string &value)
Set a configuration string to update configuration values.
Definition configBase.cc:467
ConfigBase()
Default constructor used for serialization.
Definition configBase.h:127
int printUsage()
Called to print the help/usage message.
Definition configBase.cc:195
Definition configBase.h:59