OPAL (Object Oriented Parallel Accelerator Library)  2024.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 <memory>
33 #include <string>
34 #include <iostream>
35 #include <sstream>
36 #include <set>
37 
38 #include "boost/utility/value_init.hpp"
39 
40 #include "Util/OptPilotException.h"
41 
42 class CmdArguments {
43 
44 public:
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 
156 private:
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 
176 typedef std::shared_ptr<CmdArguments> CmdArguments_t;
177 
178 template<class T>
179 inline 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 
191 template<>
192 inline 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
T getArg(const std::string name, bool isFatal=false)
Definition: CmdArguments.h:60
void addArgument(const std::string &name, const T &value)
Definition: CmdArguments.h:107
unsigned int getNumArguments() const
Definition: CmdArguments.h:133
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two distribute and or modify the software for each author s protection and we want to make certain that everyone understands that there is no warranty for this free software If the software is modified by someone else and passed we want its recipients to know that what they have is not the so that any problems introduced by others will not reflect on the original authors reputations any free program is threatened constantly by software patents We wish to avoid the danger that redistributors of a free program will individually obtain patent in effect making the program proprietary To prevent we have made it clear that any patent must be licensed for everyone s free use or not licensed at all The precise terms and conditions for distribution and modification follow GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR DISTRIBUTION AND MODIFICATION This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License The refers to any such program or and a work based on the Program means either the Program or any derivative work under copyright a work containing the Program or a portion of it
Definition: LICENSE:43
std::map< std::string, std::string > arguments_
container for storing command line options
Definition: CmdArguments.h:159
void replaceArgument(const std::string &name, const T &value)
Definition: CmdArguments.h:120
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
void split(std::string &name, std::string &value, std::string arg)
helper to split string
void addArguments(int argc, char **argv)
parse user arguments
std::string::iterator iterator
Definition: MSLang.h:15
CmdArguments(int argc, char **argv)
Definition: CmdArguments.h:46
std::shared_ptr< CmdArguments > CmdArguments_t
Definition: CmdArguments.h:176
T getArg(const std::string name, T default_value, bool isFatal=false)
Definition: CmdArguments.h:76
const std::string name
std::set< std::string > warned_
Definition: CmdArguments.h:161
constexpr double e
The value of .
Definition: Physics.h:39
bool warn
Warn flag.
Definition: Options.cpp:33
char ** getArguments() const
T arg(const std::string name)
Definition: CmdArguments.h:179