1+ /* *
2+ * @file pfasst/config.hpp
3+ * @since v0.3.0
4+ */
15#ifndef _PFASST__CONFIG_HPP_
26#define _PFASST__CONFIG_HPP_
37
@@ -13,15 +17,25 @@ namespace po = boost::program_options;
1317
1418namespace pfasst
1519{
20+ /* *
21+ * @since v0.3.0
22+ */
1623 namespace config
1724 {
1825 /* *
26+ * runtime config options provider.
27+ *
28+ * This singleton provides easy access to command line parameters at runtime.
29+ *
1930 * @note This uses the Singleton Pattern, and hence pfasst::options::get_instance() is
2031 * thread-safe with C++11.
32+ * @since v0.3.0
33+ * @ingroup Internals
2134 */
2235 class options
2336 {
2437 public:
38+ // / line width of help and usage information
2539 static const size_t LINE_WIDTH = 100 ;
2640
2741 private:
@@ -31,34 +45,140 @@ namespace pfasst
3145 vector<string> unrecognized_args;
3246 bool initialized = false ;
3347
48+ // ! @{
3449 options ();
3550 options (const options&) = delete ;
3651 void operator =(const options&) = delete ;
52+ // ! @}
3753
3854 public:
55+ // ! @{
56+ /* *
57+ * accessor to the singleton instance.
58+ *
59+ * @returns singleton config::options instance
60+ */
3961 static options& get_instance ();
4062 po::variables_map& get_variables_map ();
4163 po::options_description& get_all_options ();
4264 vector<string>& get_unrecognized_args ();
65+ // ! @}
66+
67+ // ! @{
68+ /* *
69+ * adds a new boolean flag.
70+ *
71+ * @param[in] group string identifying the parameter group
72+ * @param[in] option Name of the command line parameter.
73+ * It is possible to specify a long and optional short option name by comma-separation.
74+ * Short option names are identified by being only a single character.
75+ * They are automatically parsed as '`-[SHORT]`' by `boost::program_options` in contrast
76+ * to '`--[LONG]`'.
77+ * @param[in] help help text to be displayed in the help and usage information
78+ */
4379 static void add_option (const string& group, const string& option, const string& help);
4480
81+ /* *
82+ * adds a new parameter with an expected value of type @p T.
83+ *
84+ * @tparam T type of the specified parameter
85+ * @param[in] group string identifying the parameter group
86+ * @param[in] option Name of the command line parameter.
87+ * It is possible to specify a long and optional short option name by comma-separation.
88+ * Short option names are identified by being only a single character.
89+ * They are automatically parsed as '`-[SHORT]`' by `boost::program_options` in contrast
90+ * to '`--[LONG]`'.
91+ * @param[in] help help text to be displayed in the help and usage information
92+ *
93+ * @overload
94+ */
4595 template <typename T>
4696 static void add_option (const string& group, const string& option, const string& help);
97+ // ! @}
4798
99+ /* *
100+ * initialize program options.
101+ *
102+ * This initializes `boost::program_options` with all previously added options and groups.
103+ */
48104 void init ();
49105 };
50106
107+ /* *
108+ * get value of specific type @p T
109+ *
110+ * @tparam T type of the retreived value
111+ * @param[in] name Name of the (long) option as defined with @p option in options::add_option()
112+ * @returns Value of type @p T of desired option.
113+ * @throws boost::bad_any_cast if option is not given.
114+ *
115+ * @see
116+ * [boost::any::bad_any_cast]
117+ * (http://www.boost.org/doc/libs/1_57_0/doc/html/boost/bad_any_cast.html)
118+ */
51119 template <typename T>
52- static T get_value (const string& name, const T& default_val);
120+ inline T get_value (const string& name)
121+ {
122+ return options::get_instance ().get_variables_map ()[name].as <T>();
123+ }
53124
125+ /* *
126+ * get value of specific type @p T with default value.
127+ *
128+ * @tparam T type of the retreived value
129+ *
130+ * @overload
131+ */
54132 template <typename T>
55- static T get_value (const string& name);
133+ inline T get_value (const string& name, const T& default_val)
134+ {
135+ return options::get_instance ().get_variables_map ().count (name)
136+ ? options::get_instance ().get_variables_map ()[name].as <T>() : default_val;
137+ }
56138
57139 /* *
58- * @returns empty string if params are set and `if_no_params` is `true`
140+ * compile basic help and usage information.
141+ *
142+ * Depending on @p if_no_params and presence of given command line parameters the help and
143+ * usage information is compiled.
144+ * In case @p if_no_params is `true` and there are no parameters given on the command line or
145+ * @p if_no_params is `false` no matter whether parameters are given, the help message is
146+ * generated.
147+ *
148+ * @param[in] if_no_params flag governing compilation of help and usage information
149+ * @returns string containing basic help and usage information; string may be empty
59150 */
60- static string print_help (bool if_no_params = false );
151+ static string print_help (bool if_no_params = false )
152+ {
153+ bool no_params_given = options::get_instance ().get_variables_map ().empty ();
154+
155+ if (!if_no_params || (if_no_params && no_params_given)) {
156+ stringstream s;
157+ s << options::get_instance ().get_all_options () << endl;
158+ s << " Logging options:" << endl
159+ << " -v [ --verbose ] activates maximum verbosity" << endl
160+ << " --v=arg activates verbosity upto verbose level `arg`" << endl
161+ << " (valid range: 0-9)" << endl
162+ << " -vmodule=arg actives verbose logging for specific module" << endl
163+ << " (see [1] for details)" << endl << endl
164+ << " [1]: https://github.com/easylogging/easyloggingpp#vmodule" << endl;
165+ return s.str ();
166+ } else {
167+ return string ();
168+ }
169+ }
61170
171+ /* *
172+ * read and parse command line parameters.
173+ *
174+ * @param[in] argc Number of command line arguments as provided by `%main(int, char**)`.
175+ * @param[in] argv List of command line arguments as provided by `%main(int, char**)`.
176+ * @param[in] exit_on_help Whether to exit the program after displaying help and usage
177+ * information.
178+ *
179+ * @note Will call `std::exit` in case the `help` parameter has been provided and
180+ * @p exit_on_help is `true`.
181+ */
62182 static inline void read_commandline (int argc, char * argv[], bool exit_on_help = true )
63183 {
64184 po::parsed_options parsed = po::command_line_parser (argc, argv)
@@ -76,7 +196,15 @@ namespace pfasst
76196 }
77197
78198 /* *
199+ * read config parameters from file.
200+ *
201+ * @param[in] file_name name of the INI-like file containing config parameters;
202+ * path/name may be relative
79203 * @throws invalid_argument if the given file could not be opened
204+ *
205+ * @see
206+ * [Boost Program Options Documentation on supported INI-like file format]
207+ * (http://www.boost.org/doc/libs/1_57_0/doc/html/program_options/overview.html#idp343292240)
80208 */
81209 static inline void read_config_file (const string& file_name)
82210 {
@@ -90,6 +218,20 @@ namespace pfasst
90218 }
91219 }
92220
221+ /* *
222+ * initialize options detection and parsing.
223+ *
224+ * Prepopulates following groups and parameters:
225+ *
226+ * Group | Parameter | Type
227+ * -----------|---------------|---------
228+ * Global | `h`, `help` | `bool`
229+ * Duration | `dt` | `double`
230+ * Duration | `tend` | `double`
231+ * Duration | `num_iters` | `size_t`
232+ * Tolerances | `abs_res_tol` | `double`
233+ * Tolerances | `rel_res_tol` | `double`
234+ */
93235 static inline void init ()
94236 {
95237 options::add_option (" Global" , " help,h" , " display this help message" );
0 commit comments