SST  13.0.0
StructuralSimulationToolkit
configBase.h
1 // Copyright 2009-2023 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-2023, 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 */
25 extern int main(int argc, char** argv);
26 
27 namespace SST {
28 
29 /**
30  Struct that holds all the getopt_long options along with the
31  docuementation for the option
32 */
33 struct 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  bool sdl_avail;
41  mutable bool set_cmdline;
42 
43  LongOption(
44  struct option opt, const char* argname, const char* desc, const std::function<int(const char* arg)>& callback,
45  bool header, bool sdl_avail, bool set_cmdline) :
46  opt(opt),
47  argname(argname),
48  desc(desc),
49  callback(callback),
50  header(header),
51  sdl_avail(sdl_avail),
52  set_cmdline(set_cmdline)
53  {}
54 };
55 
56 
57 // Macros to make defining options easier. These must be called
58 // inside of a member function of a class inheriting from ConfigBase
59 // Nomenaclature is:
60 
61 // FLAG - value is either true or false. FLAG defaults to no arguments allowed
62 // ARG - value is a string. ARG defaults to required argument
63 // OPTVAL - Takes an optional paramater
64 
65 // longName - multicharacter name referenced using --
66 // shortName - single character name referenced using -
67 // text - help text
68 // func - function called if option is found
69 #define DEF_FLAG_OPTVAL(longName, shortName, text, func, sdl_avail) \
70  addOption({ longName, optional_argument, 0, shortName }, "[BOOL]", text, func, false, sdl_avail);
71 
72 #define DEF_FLAG(longName, shortName, text, func) \
73  addOption({ longName, no_argument, 0, shortName }, "", text, func, false, false);
74 
75 #define DEF_ARG(longName, shortName, argName, text, func, sdl_avail) \
76  addOption({ longName, required_argument, 0, shortName }, argName, text, func, false, sdl_avail);
77 
78 #define DEF_ARG_OPTVAL(longName, shortName, argName, text, func, sdl_avail) \
79  addOption({ longName, optional_argument, 0, shortName }, "[" argName "]", text, func, false, sdl_avail);
80 
81 
82 #define DEF_SECTION_HEADING(text) \
83  addOption({ "", optional_argument, 0, 0 }, "", text, std::function<int(const char* arg)>(), true, false);
84 
85 
86 /**
87  * Base class to parse command line options for SST Simulation
88  * Configuration variables.
89  *
90  * NOTE: This class contains only state for parsing the command line.
91  * All options will be stored in classes derived from this class.
92  * This means that we don't need to be able to serialize anything in
93  * this class.
94  */
96 {
97 protected:
98  /**
99  ConfigBase constructor. Meant to only be created by main
100  function
101  */
102  ConfigBase(bool suppress_print, bool suppress_sdl) : suppress_print_(suppress_print), suppress_sdl_(suppress_sdl) {}
103 
104  /**
105  Default constructor used for serialization. After
106  serialization, the Config object is only used to get the values
107  of set options and it can no longer parse arguments. Given
108  that, it will no longer print anything, so set suppress_print_
109  to true. None of this class needs to be serialized because it
110  it's state is only for parsing the arguments.
111  */
112  ConfigBase() : suppress_print_(true), suppress_sdl_(true) { options.reserve(100); }
113 
114 
115  /**
116  Called to print the help/usage message
117  */
118  int printUsage();
119 
120 
121  /**
122  Add options to the Config object. The options will be added in
123  the order they are in the input array, and across calls to the
124  function.
125  */
126  void addOption(
127  struct option opt, const char* argname, const char* desc, std::function<int(const char* arg)> callback,
128  bool header, bool sdl_avail);
129 
130  /**
131  Called to get the prelude for the help/usage message
132  */
133  virtual std::string getUsagePrelude();
134 
135  // Function that will be called at the end of parsing so that
136  // error checking can be done
137  virtual int checkArgsAfterParsing();
138 
139  // Enable support for everything after -- to be passed to a
140  // callback. Each arg will be passed independently to the callback
141  // function
142  void enableDashDashSupport(std::function<int(const char* arg)> callback);
143 
144  // Add support for positional args. Must be added in the order the
145  // args show up on the command line
146  void addPositionalCallback(std::function<int(int num, const char* arg)> callback);
147 
148 
149  /**
150  Get the name of the executable being run. This is only
151  avaialable after parseCmdLine() is called.
152  */
153  std::string getRunName() { return run_name_; }
154 
155  /** Set a configuration string to update configuration values */
156  bool setOptionExternal(const std::string& entryName, const std::string& value);
157 
158 public:
159  // Function to uniformly parse boolean values for command line
160  // arguments
161  static bool parseBoolean(const std::string& arg, bool& success, const std::string& option);
162 
163  virtual ~ConfigBase() {}
164 
165  /**
166  Parse command-line arguments to update configuration values.
167 
168  @return Returns 0 if execution should continue. Returns -1 if
169  there was an error. Returns 1 if run command line only asked
170  for information to be print (e.g. --help or -V, for example).
171  */
172  int parseCmdLine(int argc, char* argv[], bool ignore_unknown = false);
173 
174 
175 private:
176  std::vector<LongOption> options;
177  std::map<char, int> short_options;
178  std::string short_options_string;
179  size_t longest_option = 0;
180  size_t num_options = 0;
181  std::function<int(const char* arg)> dashdash_callback;
182  std::function<int(int num, const char* arg)> positional_args;
183 
184  std::string run_name_;
185  bool suppress_print_;
186  bool suppress_sdl_;
187 };
188 
189 } // namespace SST
190 
191 #endif // SST_CORE_CONFIGBASE_H
void addOption(struct option opt, const char *argname, const char *desc, std::function< int(const char *arg)> callback, bool header, bool sdl_avail)
Add options to the Config object.
Definition: configBase.cc:62
Struct that holds all the getopt_long options along with the docuementation for the option...
Definition: configBase.h:33
virtual std::string getUsagePrelude()
Called to get the prelude for the help/usage message.
Definition: configBase.cc:101
Definition: action.cc:18
ConfigBase(bool suppress_print, bool suppress_sdl)
ConfigBase constructor.
Definition: configBase.h:102
int printUsage()
Called to print the help/usage message.
Definition: configBase.cc:125
int parseCmdLine(int argc, char *argv[], bool ignore_unknown=false)
Parse command-line arguments to update configuration values.
Definition: configBase.cc:213
ConfigBase()
Default constructor used for serialization.
Definition: configBase.h:112
Base class to parse command line options for SST Simulation Configuration variables.
Definition: configBase.h:95
bool setOptionExternal(const std::string &entryName, const std::string &value)
Set a configuration string to update configuration values.
Definition: configBase.cc:346
std::string getRunName()
Get the name of the executable being run.
Definition: configBase.h:153