9using strvec = std::vector<std::string>;
15void cmdlinearguments::quit_with_help()
43cmdlinearguments cmdline;
46cmdlinearguments::cmdlinearguments() {}
54void cmdlinearguments::initialise_args(
int argc0,
char **argv0) {
56 argv = (
const char **)malloc(argc *
sizeof(
const char *));
57 for (
int i = 0; i < argc; i++)
70strvec cmdlinearguments::values(std::string flag)
72 strvec valvec = argmap[flag].val_strings;
73 if (argmap.count(flag) == 0)
75 hila::out0 <<
"Flag '" << flag <<
"' is not recognized!\n";
79 if (valvec.size() == 0)
81 hila::out0 <<
"\n\nFlag '" << flag <<
"' has no entries"
82 <<
" and the associated vector of strings is of length zero!\n\n";
98void cmdlinearguments::add_flag(std::string flag, std::string help_text)
100 if (argmap.count(flag) > 0)
102 hila::out0 <<
"\n###################################################\n";
103 hila::out0 <<
"# Flag " << flag <<
" is already set! Terminating.\n";
104 hila::out0 <<
"###################################################\n\n";
109 argmap[flag] = {std::vector<std::string>(), help_text,
false};
124strvec cmdlinearguments::read_arg_vector(
const char *flag)
129 for (
int i = 0; i < argc; i++) u_ind[i] = -1;
133 const std::regex forbidden_user_input(
"^-[a-zA-Z].*");
135 for (
int i = 0; i < argc; i++)
137 const char *p = argv[i];
139 if (std::strcmp(p, flag) == 0)
142 argmap[flag].present =
true;
156 while (!std::regex_match(p, forbidden_user_input))
159 uargs.push_back(std::string(p));
171 if (std::strcmp(p, flag) == 0)
181 for (
int i = 0; i < argc; i++)
if (u_ind[i] < 0) argv[j++] = argv[i];
190void cmdlinearguments::fill_argmap()
193 for (
auto const& p : argmap)
196 std::vector<std::string> arg_vec = read_arg_vector(p.first.c_str());
197 argmap[std::string(p.first)].val_strings = arg_vec;
201 hila::out0 <<
"There remain unprocessed command-line arguments:\n";
202 for (
int i = 1; i < argc; i++)
214strvec cmdlinearguments::parse_help(std::string help_text)
217 std::stringstream stream(help_text);
220 while (std::getline(stream, line))
222 lines.push_back(line);
231void cmdlinearguments::print_help()
233 hila::out0 <<
"Recognized command-line flags and their possible arguments:\n";
235 for (
auto const& p : argmap)
237 std::string flag = p.first;
238 std::string help = p.second.help_text;
239 strvec help_vec = parse_help(help);
240 hila::out0 <<
" " << flag << std::setw(20 - flag.length())
241 <<
": " << help_vec[0] <<
"\n";
242 for (
int i = 1; i < help_vec.size(); i++)
244 std::string padding =
" ";
257int cmdlinearguments::flag_set(
const char *flag)
259 if (argmap.count(flag) > 0)
260 return argmap[flag].val_strings.size();
269bool cmdlinearguments::flag_present(
const char *flag)
271 return argmap[flag].present;
281long cmdlinearguments::get_int(
const char *flag,
int i)
284 int set = flag_set(flag);
289 opt = argmap[flag].val_strings[i];
291 hila::out0 <<
"Flag '" << flag <<
"' has only " << set + 1
292 <<
" entries. Can't return index " << i <<
".\n";
299 const std::regex permitted_user_input(
"^[+-]?[0-9]+");
300 if (std::regex_match(opt, permitted_user_input)) {
301 val = std::stol(opt);
305 hila::out0 <<
"Expected a number (integer) after command line parameter '" << flag
312 hila::out0 <<
"Flag '" << flag <<
"' is missing an entry!\n";
327double cmdlinearguments::get_double(
const char *flag,
int i)
330 int set = flag_set(flag);
335 opt = argmap[flag].val_strings[i];
337 hila::out0 <<
"Flag '" << flag <<
"' has only " << set
338 <<
" entries. Can't return index " << i <<
".\n";
348 val = std::stod(opt);
350 catch(std::exception &e)
352 hila::out0 <<
"Expected a number (double) after command line parameter '" << flag
361 hila::out0 <<
"Flag '" << flag <<
"' is missing an entry!\n";
376std::string cmdlinearguments::get_string(
const char *flag,
int i)
378 int set = flag_set(flag);
382 return argmap[flag].val_strings[i];
384 hila::out0 <<
"Flag '" << flag <<
"' has only " << set + 1
385 <<
" entries. Can't return index " << i <<
".\n";
393 hila::out0 <<
"Flag '" << flag <<
"' is missing an entry!\n";
Invert diagonal + const. matrix using Sherman-Morrison formula.
std::ostream out0
This writes output only from main process (node 0)
void finishrun()
Normal, controlled exit - all nodes must call this. Prints timing information and information about c...
Struct to hold structured information on used command line arguments.
bool present
A boolean telling whether the flag was found in argv.
strvec val_strings
Strings corresponding to read in cmdline parameters.
std::string help_text
A string describing the use of the flag.