CommandLineHelpers.h
Go to the documentation of this file.
1 //Copyright (c) 2013-2023, The MercuryDPM Developers Team. All rights reserved.
2 //For the list of developers, see <http://www.MercuryDPM.org/Team>.
3 //
4 //Redistribution and use in source and binary forms, with or without
5 //modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name MercuryDPM nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 //ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 //WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 //DISCLAIMED. IN NO EVENT SHALL THE MERCURYDPM DEVELOPERS TEAM BE LIABLE FOR ANY
19 //DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 //(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 //LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 //ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 //(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 //SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 #ifndef MERCURYDPM_COMMANDLINE_HELPERS_H
27 #define MERCURYDPM_COMMANDLINE_HELPERS_H
28 
29 #include "Math/ExtendedMath.h"
30 
31 namespace helpers
32 {
36  bool removeFromCommandline(int& argc, char* argv[], std::string varName, int nArgs);
37 
41  bool readFromCommandLine(int argc, char *argv[], std::string varName);
42 
43  template<typename T>
44  T readFromCommandLine(int argc, char *argv[], std::string varName, T value)
45  {
46  for (int i = 0; i < argc-1; ++i) {
47  if (varName == argv[i]) {
48  value = atof(argv[i+1]);
49  logger(INFO, "readFromCommandLine: % set to % ", varName.substr(1), value);
50  return value;
51  }
52  }
53  //if the variable is not found
54  logger(INFO, "readFromCommandLine: % set to default value % ", varName.substr(1), value);
55  return value;
56  }
57 
58  template<typename T, size_t n>
59  std::array<T,n> readArrayFromCommandLine(int argc, char *argv[], std::string varName, std::array<T,n> value)
60  {
61  for (int i = 0; i < argc-1; ++i) {
62  if (varName == argv[i]) {
63  unsigned j = i+1;
64  std::stringstream out;
65  for (auto& v : value) {
66  v = atof(argv[j]);
67  out << v << ' ';
68  ++j;
69  }
70  logger(INFO, "readArrayFromCommandLine: % set to % ", varName.substr(1), out.str());
71  return value;
72  }
73  }
74  //if the variable is not found
75  std::stringstream out;
76  for (auto& v : value) out << v << ' ';
77  logger(INFO, "readArrayFromCommandLine: % set to default value % ", varName.substr(1), out.str());
78  return value;
79  }
80 
81  template<typename T>
82  std::vector<T> readVectorFromCommandLine(int argc, char *argv[], std::string varName, size_t n, std::vector<T> values)
83  {
84  for (int i = 0; i < argc-1; ++i) {
85  if (varName == argv[i]) {
86  // read until the next argument starts
87  values.resize(0);
88  std::stringstream out;
89  for (int j = i+1; j<argc and argv[j][0]!='-'; ++j) {
90  values.push_back(atof(argv[j]));
91  out << values.back() << ' ';
92  }
93  logger(INFO, "readVectorFromCommandLine: % set to % ", varName.substr(1), out.str());
94  return values;
95  }
96  }
97  //if the variable is not found
98  std::stringstream out;
99  for (auto& v: values) out << v << ' ';
100  logger(INFO, "readVectorFromCommandLine: % set to default value % ", varName.substr(1), out.str());
101  return values;
102  }
103 
104  template<>
105  std::string readFromCommandLine<std::string>(int argc, char* argv[], std::string varName, std::string value);
106 }
107 
108 #endif // MERCURYDPM_COMMANDLINE_HELPERS_H
const unsigned n
Definition: CG3DPackingUnitTest.cpp:32
LL< Log::INFO > INFO
Info log level.
Definition: Logger.cc:55
Logger< MERCURYDPM_LOGLEVEL > logger("MercuryKernel")
Definition of different loggers with certain modules. A user can define its own custom logger here.
const std::complex< Mdouble > i
Definition: ExtendedMath.h:51
Definition: CommandLineHelpers.h:32
bool readFromCommandLine(int argc, char *argv[], std::string varName)
Returns true if command line arguments contain varName, false else.
Definition: CommandLineHelpers.cc:103
std::array< T, n > readArrayFromCommandLine(int argc, char *argv[], std::string varName, std::array< T, n > value)
Definition: CommandLineHelpers.h:59
std::vector< T > readVectorFromCommandLine(int argc, char *argv[], std::string varName, size_t n, std::vector< T > values)
Definition: CommandLineHelpers.h:82
bool removeFromCommandline(int &argc, char *argv[], std::string varName, int nArgs)
May be used to hide arguments from argc and argv.
Definition: CommandLineHelpers.cc:43