MADNESS 0.10.1
commandlineparser.h
Go to the documentation of this file.
1//
2// Created by Florian Bischoff on 2/10/21.
3//
4
5#ifndef MADNESS_COMMANDLINEPARSER_H
6#define MADNESS_COMMANDLINEPARSER_H
7
8#include<map>
9#include<algorithm>
10namespace madness {
11/// very simple command line parser
12
13/// parser reads out key/value pairs from the command line of the from --key=val or --key
14/// currently no error handling, feel free to add.
16
17 std::map<std::string, std::string> keyval;
18
22
23 // parse command line arguments
24 // mp2 --mp2='maxiter 10; freeze 1' --dft:maxiter=20 --Xmpi:debug=true
25 commandlineparser(int argc, char **argv) {
27 std::vector<std::string> allArgs_raw(argv, argv + argc);
28 allArgs_raw.erase(allArgs_raw.begin()); // first argument is the name of the binary
29 for (auto &a : allArgs_raw) {
30 // special treatment for the input file: no hyphens
33 std::replace_copy(a.begin(), a.end(), a.begin(), '=', ' ');
34 std::string key, val;
35 std::stringstream sa(a);
36 sa >> key;
37 val=a.substr(key.size());
38 if (key=="input") set_keyval("user_defined_input_file","1");
39
40 // special treatment for file names: keep captal/lower case
41 if (key=="file") set_keyval_keep_case(key,val);
42 else set_keyval(key,val);
43 }
44 }
45
46 /// set default values from the command line
47 void set_defaults() {
48 keyval["input"]="input";
49// keyval["geometry"]="input_file";
50 }
51
52 void print_map() const {
53 for (auto&[key, val] : keyval) {
54 printf("%20s %20s \n", key.c_str(), val.c_str());
55 }
56 }
57
58 bool key_exists(std::string key) const {
59 return (keyval.count(tolower(key))==1);
60 }
61
62 std::string value(const std::string key) const {
63 std::string msg= "key not found: " + key;
64 MADNESS_CHECK_THROW(key_exists(key), msg.c_str());
65 return keyval.find(tolower(key))->second;
66 }
67
68 void set_keyval(const std::string key, const std::string value) {
70 }
71
72 void set_keyval_keep_case(const std::string key, const std::string value) {
74 }
75
76public:
77
78 /// special option: the input file has no hyphens in front and is just a value
79 std::string check_for_input_file(std::string line) {
80 if (line[0]=='-') return line;
81 auto words=split(line,"=");
82 if (words.size()==1) line="input="+line;
83 return line;
84 }
85 /// make lower case
86 static std::string tolower(std::string s) {
87 std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c){ return std::tolower(c); });
88 return s;
89 }
90
91 /// split a string s into a vector of strings, using delimiter
92
93 /// @param[in] s the string (pass by value!)
94 static std::vector<std::string> split(std::string s, const std::string delimiter) {
95 std::size_t pos = 0;
96 std::string token;
97 std::vector<std::string> result;
98 while ((pos = s.find(delimiter)) != std::string::npos) {
99 token = s.substr(0, pos);
100 result.push_back(token);
101 s.erase(0, pos + delimiter.length());
102 }
103 result.push_back(s);
104 return result;
105 }
106
107 static std::string remove_front_hyphens(const std::string arg) {
108 std::size_t first=arg.find_first_not_of('-');
109 return arg.substr(first);
110 }
111
112 static std::string remove_first_equal(const std::string arg) {
113 std::string result=arg;
114 const std::string item="=";
115 const std::string blank=" ";
116 auto it=std::find_first_of(result.begin(),result.end(),item.begin(),item.end());
117 std::replace(it,it+1,item.front(),blank.front());
118 return result;
119 }
120
121 /// remove all blanks
122 static std::string remove_blanks(const std::string arg) {
123 std::string str2 = arg;
124 str2.erase(std::remove_if(str2.begin(), str2.end(),
125 [](unsigned char x){return std::isspace(x);}),str2.end());
126 return str2;
127 }
128
129 /// remove blanks at the beginning and the end only
130 static std::string trim_blanks(const std::string arg) {
131 if (arg.size()==0) return arg;
132 std::size_t first=arg.find_first_not_of(' ');
133 std::size_t last=arg.find_last_not_of(' ');
134 return arg.substr(first,last-first+1);
135 }
136
137 static std::string base_name(std::string const & path, std::string const & delims = "/")
138 {
139 return path.substr(path.find_last_of(delims) + 1);
140 }
141
142 static std::string remove_extension(std::string const & filename)
143 {
144 std::size_t p=filename.find_last_of('.');
145 return p > 0 && p != std::string::npos ? filename.substr(0, p) : filename;
146 }
147
148};
149}
150#endif //MADNESS_COMMANDLINEPARSER_H
std::filesystem::path path
Definition InputWriter.cpp:3
char * p(char *buf, const char *name, int k, int initial_level, double thresh, int order)
Definition derivatives.cc:72
Tensor< typename Tensor< T >::scalar_type > arg(const Tensor< T > &t)
Return a new tensor holding the argument of each element of t (complex types only)
Definition tensor.h:2503
#define MADNESS_CHECK_THROW(condition, msg)
Check a condition — even in a release build the condition is always evaluated so it can have side eff...
Definition madness_exception.h:207
Namespace for all elements and tools of MADNESS.
Definition DFParameters.h:10
static const char * filename
Definition legendre.cc:96
static const double a
Definition nonlinschro.cc:118
static const double c
Definition relops.cc:10
very simple command line parser
Definition commandlineparser.h:15
static std::string trim_blanks(const std::string arg)
remove blanks at the beginning and the end only
Definition commandlineparser.h:130
void set_keyval_keep_case(const std::string key, const std::string value)
Definition commandlineparser.h:72
std::string value(const std::string key) const
Definition commandlineparser.h:62
void set_keyval(const std::string key, const std::string value)
Definition commandlineparser.h:68
bool key_exists(std::string key) const
Definition commandlineparser.h:58
std::map< std::string, std::string > keyval
Definition commandlineparser.h:17
static std::string base_name(std::string const &path, std::string const &delims="/")
Definition commandlineparser.h:137
static std::string tolower(std::string s)
make lower case
Definition commandlineparser.h:86
void set_defaults()
set default values from the command line
Definition commandlineparser.h:47
commandlineparser(int argc, char **argv)
Definition commandlineparser.h:25
static std::string remove_first_equal(const std::string arg)
Definition commandlineparser.h:112
static std::string remove_extension(std::string const &filename)
Definition commandlineparser.h:142
static std::string remove_blanks(const std::string arg)
remove all blanks
Definition commandlineparser.h:122
commandlineparser()
Definition commandlineparser.h:19
std::string check_for_input_file(std::string line)
special option: the input file has no hyphens in front and is just a value
Definition commandlineparser.h:79
void print_map() const
Definition commandlineparser.h:52
static std::string remove_front_hyphens(const std::string arg)
Definition commandlineparser.h:107
static std::vector< std::string > split(std::string s, const std::string delimiter)
split a string s into a vector of strings, using delimiter
Definition commandlineparser.h:94