CommandLineHelpers.h
Go to the documentation of this file.
1 // This file is part of the MercuryDPM project (https://www.mercurydpm.org).
2 // Copyright (c), The MercuryDPM Developers Team. All rights reserved.
3 // License: BSD 3-Clause License; see the LICENSE file in the root directory.
4 
5 #ifndef MERCURYDPM_COMMANDLINE_HELPERS_H
6 #define MERCURYDPM_COMMANDLINE_HELPERS_H
7 
8 #include "Math/ExtendedMath.h"
9 
10 namespace helpers
11 {
15  std::size_t countLeadingDashes(const std::string & s);
16 
20  bool removeFromCommandline(int& argc, char* argv[], const std::string & varName, int nArgs);
21 
25  bool readFromCommandLine(int argc, char *argv[], const std::string & varName);
26 
27  template<typename T>
28  T readFromCommandLine(int argc, char *argv[], const std::string & varName, T value)
29  {
30  const size_t nDashes = countLeadingDashes(varName);
31  for (int i = 0; i < argc-1; ++i) {
32  if (varName == argv[i]) {
33  value = atof(argv[i+1]);
34  logger(INFO, "readFromCommandLine: % set to % ", varName.substr(nDashes), value);
35  return value;
36  }
37  }
38  //if the variable is not found
39  logger(INFO, "readFromCommandLine: % set to default value % ", varName.substr(nDashes), value);
40  return value;
41  }
42 
43  template<typename T, size_t n>
44  std::array<T,n> readArrayFromCommandLine(int argc, char *argv[], const std::string & varName, std::array<T,n> value)
45  {
46  const size_t nDashes = countLeadingDashes(varName);
47  for (int i = 0; i < argc-1; ++i) {
48  if (varName == argv[i]) {
49  int j = i + 1;
50  std::stringstream out;
51  for (auto& v : value) {
52  v = atof(argv[j]);
53  out << v << ' ';
54  ++j;
55  }
56  logger(INFO, "readArrayFromCommandLine: % set to % ", varName.substr(nDashes), out.str());
57  return value;
58  }
59  }
60  //if the variable is not found
61  std::stringstream out;
62  for (auto& v : value) out << v << ' ';
63  logger(INFO, "readArrayFromCommandLine: % set to default value % ", varName.substr(nDashes), out.str());
64  return value;
65  }
66 
67  template<typename T>
68  std::vector<T> readVectorFromCommandLine(int argc, char *argv[], const std::string & varName, size_t n, std::vector<T> values)
69  {
70  const size_t nDashes = countLeadingDashes(varName);
71  for (int i = 0; i < argc-1; ++i) {
72  if (varName == argv[i]) {
73  // read until the next argument starts
74  values.resize(0);
75  std::stringstream out;
76  for (int j = i+1; j < argc && argv[j][0] != '-'; ++j) {
77  values.push_back(atof(argv[j]));
78  out << values.back() << ' ';
79  }
80  logger(INFO, "readVectorFromCommandLine: % set to % ", varName.substr(nDashes), out.str());
81  return values;
82  }
83  }
84  //if the variable is not found
85  std::stringstream out;
86  for (auto& v: values) out << v << ' ';
87  logger(INFO, "readVectorFromCommandLine: % set to default value % ", varName.substr(nDashes), out.str());
88  return values;
89  }
90 
91  template<>
92  std::string readFromCommandLine<std::string>(int argc, char* argv[], const std::string & varName, std::string value);
93 }
94 
95 #endif // MERCURYDPM_COMMANDLINE_HELPERS_H
Array< int, Dynamic, 1 > v
Definition: Array_initializer_list_vector_cxx11.cpp:1
int i
Definition: BiCGSTAB_step_by_step.cpp:9
const unsigned n
Definition: CG3DPackingUnitTest.cpp:11
Logger< MERCURYDPM_LOGLEVEL > logger("MercuryKernel")
Definition of different loggers with certain modules. A user can define its own custom logger here.
RealScalar s
Definition: level1_cplx_impl.h:130
#define INFO(i)
Definition: mumps_solver.h:54
squared absolute value
Definition: GlobalFunctions.h:87
Definition: CommandLineHelpers.h:11
bool removeFromCommandline(int &argc, char *argv[], const std::string &varName, int nArgs)
May be used to hide arguments from argc and argv.
Definition: CommandLineHelpers.cc:41
std::size_t countLeadingDashes(const std::string &s)
Counts the leading dash ('-') characters in a string.
Definition: CommandLineHelpers.cc:12
std::array< T, n > readArrayFromCommandLine(int argc, char *argv[], const std::string &varName, std::array< T, n > value)
Definition: CommandLineHelpers.h:44
bool readFromCommandLine(int argc, char *argv[], const std::string &varName)
Returns true if command line arguments contain varName, false else.
Definition: CommandLineHelpers.cc:99
std::vector< T > readVectorFromCommandLine(int argc, char *argv[], const std::string &varName, size_t n, std::vector< T > values)
Definition: CommandLineHelpers.h:68
std::string string(const unsigned &i)
Definition: oomph_definitions.cc:286
std::ofstream out("Result.txt")
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2