OPAL (Object Oriented Parallel Accelerator Library) 2022.1
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 <string>
33#include <iostream>
34#include <sstream>
35#include <set>
36
37#include "boost/smart_ptr.hpp"
38#include "boost/utility/value_init.hpp"
39
41
43
44public:
45
46 CmdArguments(int argc, char **argv) {
47 addArguments(argc, argv);
48 }
49
51 {}
52
54 {}
55
59 template<class T>
60 T getArg(const std::string name, bool isFatal = false) {
61 boost::value_initialized<T> value;
62 return getArg<T>(name, value, isFatal);
63 }
64
75 template<class T>
76 T getArg(const std::string name, T default_value, bool isFatal = false) {
77 T value = default_value;
78 try {
79 value = this->arg<T>(name);
80 } catch(OptPilotException &e) {
81 if(isFatal) {
82 std::ostringstream exe;
83 exe << "Fatal: argument \"";
84 exe << name;
85 exe << "\" not found!";
86 throw OptPilotException("CmdArguments::getArg", exe.str());
87 } else {
88 if(warned_.count(name) == 0) {
89 std::ostringstream warn;
90 warn << "\033[01;35m";
91 warn << "Warning: argument \"";
92 warn << name;
93 warn << "\" not found! Using default value (";
94 warn << default_value;
95 warn << ").";
96 warn << "\e[0m" << std::endl;
97 std::cout << warn.str() << std::flush;
98 warned_.insert(name);
99 }
100 }
101 }
102
103 return value;
104 }
105
106 template <class T>
107 void addArgument(const std::string &name, const T &value) {
108 std::ostringstream oss;
109 oss << value;
110
111 if (arguments_.find(name) != arguments_.end()) {
112 throw OptPilotException("CmdArguments::addArgument",
113 "Argument '" + name + "' exists");
114 }
115
116 arguments_.insert(std::make_pair(name, oss.str()));
117 }
118
119 template <class T>
120 void replaceArgument(const std::string &name, const T &value) {
121 std::ostringstream oss;
122 oss << value;
123
124 if (arguments_.find(name) == arguments_.end()) {
125 arguments_.insert(std::make_pair(name, oss.str()));
126 return;
127 }
128
129 arguments_.at(name) = oss.str();
130 }
131
132 char** getArguments() const;
133 unsigned int getNumArguments() const {
134 return arguments_.size();
135 }
136 //template<>
137 //size_t getArg<size_t>(const std::string name, bool isFatal = false) {
138 //return getArg<size_t>(name, 0, isFatal);
139 //}
140
141 //template<>
142 //int getArg<int>(const std::string name, bool isFatal = false) {
143 //return getArg<int>(name, 0, isFatal);
144 //}
145
146 //template<>
147 //unsigned int getArg<unsigned int>(const std::string name, bool isFatal = false) {
148 //return getArg<unsigned int>(name, 0, isFatal);
149 //}
150
151 //template<>
152 //std::string getArg<std::string>(const std::string name, bool isFatal = false) {
153 //return getArg<std::string>(name, "", isFatal);
154 //}
155
156private:
157
159 std::map<std::string, std::string> arguments_;
160
161 std::set<std::string> warned_;
162
164 void addArguments(int argc, char **argv);
165
167 void split(std::string &name, std::string &value, std::string arg);
168
171 template<class T>
172 T arg(const std::string name);
173
174};
175
176typedef boost::shared_ptr<CmdArguments> CmdArguments_t;
177
178template<class T>
179inline T CmdArguments::arg(const std::string name) {
180 T t;
182 if(it != arguments_.end()) {
183 std::istringstream iss(arguments_[name]);
184 iss >> t;
185 return t;
186 } else {
187 throw OptPilotException("CmdArguments::getArg", "argument not found!");
188 }
189}
190
191template<>
192inline std::string CmdArguments::arg<std::string>(const std::string name) {
194 if(it != arguments_.end()) {
195 return arguments_[name];
196 } else {
197 throw OptPilotException("CmdArguments::getArg", "argument not found!");
198 }
199}
200
201#endif
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
boost::shared_ptr< CmdArguments > CmdArguments_t
Definition: CmdArguments.h:176
const std::string name
constexpr double e
The value of.
Definition: Physics.h:39
std::string::iterator iterator
Definition: MSLang.h:16
bool warn
Warn flag.
Definition: Options.cpp:33
void split(std::string &name, std::string &value, std::string arg)
helper to split string
std::set< std::string > warned_
Definition: CmdArguments.h:161
T arg(const std::string name)
Definition: CmdArguments.h:179
std::map< std::string, std::string > arguments_
container for storing command line options
Definition: CmdArguments.h:159
unsigned int getNumArguments() const
Definition: CmdArguments.h:133
void addArguments(int argc, char **argv)
parse user arguments
T getArg(const std::string name, T default_value, bool isFatal=false)
Definition: CmdArguments.h:76
char ** getArguments() const
void addArgument(const std::string &name, const T &value)
Definition: CmdArguments.h:107
T getArg(const std::string name, bool isFatal=false)
Definition: CmdArguments.h:60
CmdArguments(int argc, char **argv)
Definition: CmdArguments.h:46
void replaceArgument(const std::string &name, const T &value)
Definition: CmdArguments.h:120