OPAL (Object Oriented Parallel Accelerator Library) 2024.2
OPAL
CmdArguments.h
Go to the documentation of this file.
1//
2// Class CmdArguments
3// Parsing command line arguments
4//
5// In order to have a flexible framework, each component implementation gets
6// access to all command line arguments.
7// All command line options have the form:
8// --name=value
9// Spaces before and after the "=" will be trimmed.
10//
11// Copyright (c) 2010 - 2013, Yves Ineichen, ETH Zürich
12// All rights reserved
13//
14// Implemented as part of the PhD thesis
15// "Toward massively parallel multi-objective optimization with application to
16// particle accelerators" (https://doi.org/10.3929/ethz-a-009792359)
17//
18// This file is part of OPAL.
19//
20// OPAL is free software: you can redistribute it and/or modify
21// it under the terms of the GNU General Public License as published by
22// the Free Software Foundation, either version 3 of the License, or
23// (at your option) any later version.
24//
25// You should have received a copy of the GNU General Public License
26// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
27//
28#ifndef __CMD_ARGUMENTS__
29#define __CMD_ARGUMENTS__
30
31#include <map>
32#include <memory>
33#include <string>
34#include <iostream>
35#include <sstream>
36#include <set>
37
38#include "Util/AnsiColors.h"
40
42
43public:
44 CmdArguments(int argc, char **argv) {
45 addArguments(argc, argv);
46 }
47
49 {}
50
53
57 template<class T>
58 T getArg(const std::string name, bool isFatal = false) {
59 T value{};
60 return getArg<T>(name, value, isFatal);
61 }
62
73 template<class T>
74 T getArg(const std::string name, T default_value, bool isFatal = false) {
75 T value = default_value;
76 try {
77 value = this->arg<T>(name);
78 } catch(OptPilotException& e) {
79 if (isFatal) {
80 std::ostringstream exe;
81 exe << "Fatal: argument \"";
82 exe << name;
83 exe << "\" not found!";
84 throw OptPilotException("CmdArguments::getArg", exe.str());
85 } else {
86 if (warned_.count(name) == 0) {
87 std::ostringstream warn;
89 warn << "Warning: argument \"";
90 warn << name;
91 warn << "\" not found! Using default value (";
92 warn << default_value;
93 warn << ").";
94 warn << AnsiColors::Reset << std::endl;
95 std::cout << warn.str() << std::flush;
96 warned_.insert(name);
97 }
98 }
99 }
100
101 return value;
102 }
103
104 template <class T>
105 void addArgument(const std::string& name, const T& value) {
106 std::ostringstream oss;
107 oss << value;
108
109 if (arguments_.find(name) != arguments_.end()) {
110 throw OptPilotException("CmdArguments::addArgument",
111 "Argument '" + name + "' exists");
112 }
113
114 arguments_.insert(std::make_pair(name, oss.str()));
115 }
116
117 template <class T>
118 void replaceArgument(const std::string& name, const T& value) {
119 std::ostringstream oss;
120 oss << value;
121
122 if (arguments_.find(name) == arguments_.end()) {
123 arguments_.insert(std::make_pair(name, oss.str()));
124 return;
125 }
126
127 arguments_.at(name) = oss.str();
128 }
129
130 char** getArguments() const;
131 unsigned int getNumArguments() const {
132 return arguments_.size();
133 }
134 //template<>
135 //size_t getArg<size_t>(const std::string name, bool isFatal = false) {
136 //return getArg<size_t>(name, 0, isFatal);
137 //}
138
139 //template<>
140 //int getArg<int>(const std::string name, bool isFatal = false) {
141 //return getArg<int>(name, 0, isFatal);
142 //}
143
144 //template<>
145 //unsigned int getArg<unsigned int>(const std::string name, bool isFatal = false) {
146 //return getArg<unsigned int>(name, 0, isFatal);
147 //}
148
149 //template<>
150 //std::string getArg<std::string>(const std::string name, bool isFatal = false) {
151 //return getArg<std::string>(name, "", isFatal);
152 //}
153
154private:
156 std::map<std::string, std::string> arguments_;
157
158 std::set<std::string> warned_;
159
161 void addArguments(int argc, char **argv);
162
164 void split(std::string& name, std::string& value, std::string arg);
165
168 template<class T>
169 T arg(const std::string name);
170
171};
172
173typedef std::shared_ptr<CmdArguments> CmdArguments_t;
174
175template<class T>
176inline T CmdArguments::arg(const std::string name) {
177 T t;
178 std::map<std::string, std::string>::iterator it = arguments_.find(name);
179 if (it != arguments_.end()) {
180 std::istringstream iss(arguments_[name]);
181 iss >> t;
182 return t;
183 } else {
184 throw OptPilotException("CmdArguments::getArg", "argument not found!");
185 }
186}
187
188template<>
189inline std::string CmdArguments::arg<std::string>(const std::string name) {
190 std::map<std::string, std::string>::iterator it = arguments_.find(name);
191 if (it != arguments_.end()) {
192 return arguments_[name];
193 } else {
194 throw OptPilotException("CmdArguments::getArg", "argument not found!");
195 }
196}
197
198#endif
const std::string name
std::shared_ptr< CmdArguments > CmdArguments_t
constexpr char Reset[]
Definition AnsiColors.h:5
constexpr char BoldMagenta[]
Definition AnsiColors.h:7
void split(std::string &name, std::string &value, std::string arg)
helper to split string
std::set< std::string > warned_
T arg(const std::string name)
std::map< std::string, std::string > arguments_
container for storing command line options
unsigned int getNumArguments() const
void addArguments(int argc, char **argv)
parse user arguments
T getArg(const std::string name, T default_value, bool isFatal=false)
char ** getArguments() const
void addArgument(const std::string &name, const T &value)
T getArg(const std::string name, bool isFatal=false)
CmdArguments(int argc, char **argv)
void replaceArgument(const std::string &name, const T &value)