10using strvec = std::vector<std::string>;
16void cmdlinearguments::quit_with_help() {
47cmdlinearguments cmdline;
50cmdlinearguments::cmdlinearguments() {}
58void cmdlinearguments::initialise_args(
int argc0,
char **argv0) {
60 argv = (
const char **)malloc(argc *
sizeof(
const char *));
61 for (
int i = 0; i < argc; i++)
74strvec cmdlinearguments::values(
const std::string &flag) {
75 strvec valvec = argmap[flag].val_strings;
76 if (argmap.count(flag) == 0) {
77 hila::out0 <<
"Flag '" << flag <<
"' is not recognized!\n";
81 if (valvec.size() == 0) {
82 hila::out0 <<
"\n\nFlag '" << flag <<
"' has no entries"
83 <<
" and the associated vector of strings is of length zero!\n\n";
101void cmdlinearguments::add_flag(
const std::string &flag,
const std::string &help_text,
102 std::string aux,
int number) {
103 if (argmap.count(flag) > 0) {
104 hila::out0 <<
"\n###################################################\n";
105 hila::out0 <<
"# Flag " << flag <<
" is already set! Terminating.\n";
106 hila::out0 <<
"###################################################\n\n";
109 argmap[flag] = {std::vector<std::string>(), aux, help_text, number,
false};
124strvec cmdlinearguments::read_arg_vector(
const char *flag) {
127 std::vector<int> u_ind(argc);
128 for (
int i = 0; i < argc; i++)
130 int *p_ind = u_ind.data();
133 const std::regex forbidden_user_input(
"^-[a-zA-Z].*");
135 for (
int i = 0; i < argc; i++) {
136 const char *p = argv[i];
140 if (std::strcmp(p, flag) == 0) {
143 argmap[flag].present =
true;
156 while (!std::regex_match(p, forbidden_user_input)) {
158 uargs.push_back(std::string(p));
171 if (std::strcmp(p, flag) == 0) {
179 if (argmap[flag].number >= 0) {
180 if (argmap[flag].number != n) {
181 hila::out0 <<
"Error: command line flag " << flag <<
" requires "
182 << argmap[flag].number <<
" arguments\n";
192 for (
int i = 0; i < argc; i++)
203void cmdlinearguments::fill_argmap() {
205 for (
auto const &p : argmap) {
207 std::vector<std::string> arg_vec = read_arg_vector(p.first.c_str());
208 argmap[std::string(p.first)].val_strings = arg_vec;
211 hila::out0 <<
"There remain unprocessed command-line arguments:\n";
212 for (
int i = 1; i < argc; i++)
224strvec cmdlinearguments::parse_help(std::string help_text) {
226 std::stringstream stream(help_text);
229 while (std::getline(stream, line)) {
230 lines.push_back(line);
239void cmdlinearguments::print_help() {
240 hila::out0 <<
"Recognized command-line flags and their possible arguments:\n";
242 const std::string padding =
" ";
243 for (
auto const &p : argmap) {
244 const std::string &flag = p.first;
245 const std::string &help = p.second.help_text;
246 const std::string &aux = p.second.aux;
247 strvec help_vec = parse_help(help);
248 hila::out0 <<
" " << flag <<
" " << aux << std::setw(22 - flag.length() - aux.length() - 1)
249 <<
": " << help_vec[0] <<
"\n";
250 for (
int i = 1; i < help_vec.size(); i++) {
262int cmdlinearguments::flag_set(
const char *flag) {
263 if (argmap.count(flag) > 0)
264 return argmap[flag].val_strings.size();
274bool cmdlinearguments::flag_present(
const char *flag) {
275 return argmap[flag].present;
285long cmdlinearguments::get_int(
const char *flag,
int i) {
287 int set = flag_set(flag);
291 opt = argmap[flag].val_strings[i];
293 hila::out0 <<
"Flag '" << flag <<
"' has only " << set + 1
294 <<
" entries. Can't return index " << i <<
".\n";
301 const std::regex permitted_user_input(
"^[+-]?[0-9]+");
302 if (std::regex_match(opt, permitted_user_input)) {
303 val = std::stol(opt);
305 hila::out0 <<
"Expected a number (integer) after command line parameter '" << flag
311 hila::out0 <<
"Flag '" << flag <<
"' is missing an entry!\n";
326double cmdlinearguments::get_double(
const char *flag,
int i) {
328 int set = flag_set(flag);
332 opt = argmap[flag].val_strings[i];
334 hila::out0 <<
"Flag '" << flag <<
"' has only " << set
335 <<
" entries. Can't return index " << i <<
".\n";
343 auto [p, ec] = std::from_chars(opt.data(), opt.data() + opt.size(), val);
344 if (p == opt.data()) {
345 hila::out0 <<
"Expected a number (double) after command line parameter '" << flag
362 hila::out0 <<
"Flag '" << flag <<
"' is missing an entry!\n";
377std::string cmdlinearguments::get_string(
const char *flag,
int i,
bool clear) {
378 int set = flag_set(flag);
381 auto t = argmap[flag].val_strings[i];
383 argmap[flag].val_strings[i].clear();
386 hila::out0 <<
"Flag '" << flag <<
"' has only " << set + 1
387 <<
" entries. Can't return index " << i <<
".\n";
393 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