9using strvec = std::vector<std::string>;
15void cmdlinearguments::quit_with_help() {
41cmdlinearguments cmdline;
44cmdlinearguments::cmdlinearguments() {}
52void cmdlinearguments::initialise_args(
int argc0,
char **argv0) {
54 argv = (
const char **)malloc(argc *
sizeof(
const char *));
55 for (
int i = 0; i < argc; i++)
68strvec cmdlinearguments::values(std::string flag) {
69 strvec valvec = argmap[flag].val_strings;
70 if (argmap.count(flag) == 0) {
71 hila::out0 <<
"Flag '" << flag <<
"' is not recognized!\n";
75 if (valvec.size() == 0) {
76 hila::out0 <<
"\n\nFlag '" << flag <<
"' has no entries"
77 <<
" and the associated vector of strings is of length zero!\n\n";
93void cmdlinearguments::add_flag(std::string flag, std::string help_text) {
94 if (argmap.count(flag) > 0) {
95 hila::out0 <<
"\n###################################################\n";
96 hila::out0 <<
"# Flag " << flag <<
" is already set! Terminating.\n";
97 hila::out0 <<
"###################################################\n\n";
100 argmap[flag] = {std::vector<std::string>(), help_text,
false};
115strvec cmdlinearguments::read_arg_vector(
const char *flag) {
118 std::vector<int> u_ind(argc);
119 for (
int i = 0; i < argc; i++)
121 int *p_ind = u_ind.data();
124 const std::regex forbidden_user_input(
"^-[a-zA-Z].*");
126 for (
int i = 0; i < argc; i++) {
127 const char *p = argv[i];
129 if (std::strcmp(p, flag) == 0) {
131 argmap[flag].present =
true;
144 while (!std::regex_match(p, forbidden_user_input)) {
146 uargs.push_back(std::string(p));
158 if (std::strcmp(p, flag) == 0) {
167 for (
int i = 0; i < argc; i++)
178void cmdlinearguments::fill_argmap() {
180 for (
auto const &p : argmap) {
182 std::vector<std::string> arg_vec = read_arg_vector(p.first.c_str());
183 argmap[std::string(p.first)].val_strings = arg_vec;
186 hila::out0 <<
"There remain unprocessed command-line arguments:\n";
187 for (
int i = 1; i < argc; i++)
199strvec cmdlinearguments::parse_help(std::string help_text) {
201 std::stringstream stream(help_text);
204 while (std::getline(stream, line)) {
205 lines.push_back(line);
214void cmdlinearguments::print_help() {
215 hila::out0 <<
"Recognized command-line flags and their possible arguments:\n";
217 for (
auto const &p : argmap) {
218 std::string flag = p.first;
219 std::string help = p.second.help_text;
220 strvec help_vec = parse_help(help);
221 hila::out0 <<
" " << flag << std::setw(20 - flag.length()) <<
": " << help_vec[0]
223 for (
int i = 1; i < help_vec.size(); i++) {
224 std::string padding =
" ";
236int cmdlinearguments::flag_set(
const char *flag) {
237 if (argmap.count(flag) > 0)
238 return argmap[flag].val_strings.size();
248bool cmdlinearguments::flag_present(
const char *flag) {
249 return argmap[flag].present;
259long cmdlinearguments::get_int(
const char *flag,
int i) {
261 int set = flag_set(flag);
265 opt = argmap[flag].val_strings[i];
267 hila::out0 <<
"Flag '" << flag <<
"' has only " << set + 1
268 <<
" entries. Can't return index " << i <<
".\n";
275 const std::regex permitted_user_input(
"^[+-]?[0-9]+");
276 if (std::regex_match(opt, permitted_user_input)) {
277 val = std::stol(opt);
279 hila::out0 <<
"Expected a number (integer) after command line parameter '" << flag
285 hila::out0 <<
"Flag '" << flag <<
"' is missing an entry!\n";
300double cmdlinearguments::get_double(
const char *flag,
int i) {
302 int set = flag_set(flag);
306 opt = argmap[flag].val_strings[i];
308 hila::out0 <<
"Flag '" << flag <<
"' has only " << set
309 <<
" entries. Can't return index " << i <<
".\n";
318 val = std::stod(opt);
319 }
catch (std::exception &e) {
320 hila::out0 <<
"Expected a number (double) after command line parameter '" << flag
328 hila::out0 <<
"Flag '" << flag <<
"' is missing an entry!\n";
343std::string cmdlinearguments::get_string(
const char *flag,
int i) {
344 int set = flag_set(flag);
347 return argmap[flag].val_strings[i];
349 hila::out0 <<
"Flag '" << flag <<
"' has only " << set + 1
350 <<
" entries. Can't return index " << i <<
".\n";
356 hila::out0 <<
"Flag '" << flag <<
"' is missing an entry!\n";
Implement hila::swap for gauge fields.
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.