9using strvec = std::vector<std::string>;
15void cmdlinearguments::quit_with_help() {
46cmdlinearguments cmdline;
49cmdlinearguments::cmdlinearguments() {}
57void cmdlinearguments::initialise_args(
int argc0,
char **argv0) {
59 argv = (
const char **)malloc(argc *
sizeof(
const char *));
60 for (
int i = 0; i < argc; i++)
73strvec cmdlinearguments::values(
const std::string &flag) {
74 strvec valvec = argmap[flag].val_strings;
75 if (argmap.count(flag) == 0) {
76 hila::out0 <<
"Flag '" << flag <<
"' is not recognized!\n";
80 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";
100void cmdlinearguments::add_flag(
const std::string &flag,
const std::string &help_text,
101 std::string aux,
int number) {
102 if (argmap.count(flag) > 0) {
103 hila::out0 <<
"\n###################################################\n";
104 hila::out0 <<
"# Flag " << flag <<
" is already set! Terminating.\n";
105 hila::out0 <<
"###################################################\n\n";
108 argmap[flag] = {std::vector<std::string>(), aux, help_text, number,
false};
123strvec cmdlinearguments::read_arg_vector(
const char *flag) {
126 std::vector<int> u_ind(argc);
127 for (
int i = 0; i < argc; i++)
129 int *p_ind = u_ind.data();
132 const std::regex forbidden_user_input(
"^-[a-zA-Z].*");
134 for (
int i = 0; i < argc; i++) {
135 const char *p = argv[i];
139 if (std::strcmp(p, flag) == 0) {
142 argmap[flag].present =
true;
155 while (!std::regex_match(p, forbidden_user_input)) {
157 uargs.push_back(std::string(p));
170 if (std::strcmp(p, flag) == 0) {
178 if (argmap[flag].number >= 0) {
179 if (argmap[flag].number != n) {
180 hila::out0 <<
"Error: command line flag " << flag <<
" requires "
181 << argmap[flag].number <<
" arguments\n";
191 for (
int i = 0; i < argc; i++)
202void cmdlinearguments::fill_argmap() {
204 for (
auto const &p : argmap) {
206 std::vector<std::string> arg_vec = read_arg_vector(p.first.c_str());
207 argmap[std::string(p.first)].val_strings = arg_vec;
210 hila::out0 <<
"There remain unprocessed command-line arguments:\n";
211 for (
int i = 1; i < argc; i++)
223strvec cmdlinearguments::parse_help(std::string help_text) {
225 std::stringstream stream(help_text);
228 while (std::getline(stream, line)) {
229 lines.push_back(line);
238void cmdlinearguments::print_help() {
239 hila::out0 <<
"Recognized command-line flags and their possible arguments:\n";
241 const std::string padding =
" ";
242 for (
auto const &p : argmap) {
243 const std::string &flag = p.first;
244 const std::string &help = p.second.help_text;
245 const std::string &aux = p.second.aux;
246 strvec help_vec = parse_help(help);
247 hila::out0 <<
" " << flag <<
" " << aux << std::setw(22 - flag.length() - aux.length() - 1)
248 <<
": " << help_vec[0] <<
"\n";
249 for (
int i = 1; i < help_vec.size(); i++) {
261int cmdlinearguments::flag_set(
const char *flag) {
262 if (argmap.count(flag) > 0)
263 return argmap[flag].val_strings.size();
273bool cmdlinearguments::flag_present(
const char *flag) {
274 return argmap[flag].present;
284long cmdlinearguments::get_int(
const char *flag,
int i) {
286 int set = flag_set(flag);
290 opt = argmap[flag].val_strings[i];
292 hila::out0 <<
"Flag '" << flag <<
"' has only " << set + 1
293 <<
" entries. Can't return index " << i <<
".\n";
300 const std::regex permitted_user_input(
"^[+-]?[0-9]+");
301 if (std::regex_match(opt, permitted_user_input)) {
302 val = std::stol(opt);
304 hila::out0 <<
"Expected a number (integer) after command line parameter '" << flag
310 hila::out0 <<
"Flag '" << flag <<
"' is missing an entry!\n";
325double cmdlinearguments::get_double(
const char *flag,
int i) {
327 int set = flag_set(flag);
331 opt = argmap[flag].val_strings[i];
333 hila::out0 <<
"Flag '" << flag <<
"' has only " << set
334 <<
" entries. Can't return index " << i <<
".\n";
343 val = std::stod(opt);
344 }
catch (std::exception &e) {
345 hila::out0 <<
"Expected a number (double) after command line parameter '" << flag
353 hila::out0 <<
"Flag '" << flag <<
"' is missing an entry!\n";
368std::string cmdlinearguments::get_string(
const char *flag,
int i,
bool clear) {
369 int set = flag_set(flag);
372 auto t = argmap[flag].val_strings[i];
374 argmap[flag].val_strings[i].clear();
377 hila::out0 <<
"Flag '" << flag <<
"' has only " << set + 1
378 <<
" entries. Can't return index " << i <<
".\n";
384 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.
int number
number of args for each flag. < 0: unspecified