-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArgumentParser.cpp
More file actions
742 lines (742 loc) · 24.1 KB
/
Copy pathArgumentParser.cpp
File metadata and controls
742 lines (742 loc) · 24.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
/* SPDX-License-Identifier: AGPL-3.0-or-later */
#include "ArgumentParser.h"
#include "Log.h"
#include "Settings.h"
#include "Util.h"
namespace fs::settings
{
static map<std::string, std::function<void()>> PARSE_FCT{};
static vector<std::pair<std::string, std::string>> PARSE_HELP{};
static map<std::string, bool> PARSE_REQUIRED{};
static map<std::string, bool> PARSE_HAVE{};
ArgumentParser* PARSER{nullptr};
void ArgumentParser::mark_parsed(const string arg) { PARSE_HAVE.emplace(arg, true); }
bool ArgumentParser::was_parsed(const string arg) { return PARSE_HAVE.contains(arg); }
template <class T>
T parse(auto fct)
{
auto& parser = *PARSER;
parser.mark_parsed(parser.cur_arg());
// HACK: use auto instead of std::function<T()> so call is easier
return static_cast<T>(fct());
}
template <class T>
T parse_once(auto fct)
{
auto& parser = *PARSER;
if (parser.was_parsed(parser.cur_arg()))
{
cout << "\nArgument " << parser.cur_arg() << " already specified\n\n";
parser.show_usage_and_exit();
}
// HACK: use auto instead of std::function<T()> so call is easier
return parse<T>(fct);
}
bool parse_flag(bool not_inverse);
template <class T>
T parse_value()
{
auto& parser = *PARSER;
return parse_once<T>([&] { return stod(parser.get_arg()); });
}
size_t parse_size_t();
string parse_string();
template <class T>
T parse_index()
{
auto& parser = *PARSER;
return parse_once<T>([&] { return T(stod(parser.get_arg())); });
}
void register_argument(string v, string help, bool required, std::function<void()> fct);
template <class T>
void register_setter(
std::function<void(T)> fct_set,
string v,
string help,
bool required,
std::function<T()> fct
)
{
register_argument(v, help, required, [=] { fct_set(fct()); });
}
template <class T>
void register_setter(T& variable, string v, string help, bool required, std::function<T()> fct)
{
register_argument(v, help, required, [&variable, fct] { variable = fct(); });
}
template <class T>
void register_setter(
std::optional<T>& variable,
string v,
string help,
bool required,
std::function<T()> fct
)
{
// if supposed to be required but has a value from settings then shouldn't be required
register_argument(v, help, required && !variable.has_value(), [&variable, fct] {
variable = fct();
});
}
template <class T>
void register_setter(
atomic<T>& variable,
string v,
string help,
bool required,
std::function<T()> fct
)
{
register_argument(v, help, required, [&variable, fct] { variable = fct(); });
}
void register_path_setter(LazyPath& variable, string v, string help, bool required)
{
register_argument(v, help, required, [&variable] {
// always relative to current directory since this was a cli arg
variable = LazyPath{std::filesystem::current_path().generic_string(), parse_string()};
});
}
void register_flag(std::function<void(bool)> fct, bool not_inverse, string v, string help);
void register_flag(bool& variable, bool not_inverse, string v, string help);
template <class T>
void register_index(T& index, string v, string help, bool required)
{
register_argument(v, help, required, [&] { index = parse_index<T>(); });
}
template <class T>
void register_index(std::optional<T>& index, string v, string help, bool required)
{
register_argument(v, help, required && !index.has_value(), [&] { index = parse_index<T>(); });
}
string ArgumentParser::get_args()
{
std::string args{arguments_.at(0)};
for (size_t i = 1; i < arguments_.size(); ++i)
{
args.append(" ");
args.append(arguments_.at(i));
}
return args;
}
string ArgumentParser::format_args() { return std::format("Arguments are:\n {:s}\n", get_args()); }
void ArgumentParser::show_args() { cout << format_args() << "\n"; }
void ArgumentParser::log_args() { logging::note("Arguments are:\n {:s}\n", get_args()); }
static vector<Usage> USAGES{};
void add_usage(const Usage usage) { USAGES.emplace_back(usage); }
void add_usages(const vector<Usage> usages)
{
for (const auto& u : usages)
{
add_usage(u);
}
}
void ArgumentParser::show_usage_and_exit(int exit_code)
{
// NOTE: this assumes there are always optional args
// (but -h, -v, -q should always be there)
for (const auto& usage : USAGES)
{
// FIX: extra space if no positional args
cout << std::format(
"Usage: {:s} {:s} [OPTION]...\n\n{:s}\n\n",
binary_name_,
usage.positional_arg_summary,
usage.description
);
}
cout << " Input Options\n";
// FIX: this should show arguments specific to mode, but it doesn't indicate that on the outputs
for (auto& kv : PARSE_HELP)
{
cout << std::format(" {:<25s} {:s}\n", kv.first, kv.second);
}
exit(exit_code);
}
void ArgumentParser::show_usage_and_exit()
{
show_args();
show_usage_and_exit(-1);
}
void ArgumentParser::show_help_and_exit()
{
// showing help isn't an error
show_usage_and_exit(0);
}
string ArgumentParser::get_arg() noexcept
{
// check if we don't have any more arguments
logging::check_fatal(
cur_arg_ + 1 >= args_expanded().size(),
"Missing argument to --{:s}",
args_expanded().at(cur_arg_)
);
return args_expanded().at(++cur_arg_);
}
size_t parse_size_t()
{
auto& parser = *PARSER;
return parse_once<size_t>([&] { return static_cast<size_t>(stoi(parser.get_arg())); });
}
string parse_string()
{
auto& parser = *PARSER;
return parse_once<string>([&]() { return parser.get_arg(); });
}
void register_argument(string v, string help, bool required, std::function<void()> fct)
{
// HACK: resolve once and fail if not set already
static auto& settings = fs::settings::instance();
// cli is lower case with '-' and settings are uppercase with '_'
const auto as_setting = [&]() {
// start after any '-' at front
string s{v.substr(v.find_first_not_of('-'))};
std::transform(s.begin(), s.end(), s.begin(), [](const auto c) -> int {
if ('-' == c)
{
return '_';
}
return std::toupper(c);
});
return s;
}();
PARSE_FCT.emplace(v, fct);
PARSE_HELP.emplace_back(v, help);
logging::debug("Checking if already have {:s}", as_setting);
required = required && !settings.found(as_setting);
PARSE_REQUIRED.emplace(v, required);
}
void register_flag(std::function<void(bool)> fct, bool not_inverse, string v, string help)
{
register_argument(v, help, false, [=] { fct(parse_flag(not_inverse)); });
}
void register_flag(atomic<bool>& variable, bool not_inverse, string v, string help)
{
register_argument(v, help, false, [=, &variable] { variable = parse_flag(not_inverse); });
}
void register_flag(bool& variable, bool not_inverse, string v, string help)
{
register_argument(v, help, false, [=, &variable] { variable = parse_flag(not_inverse); });
}
ArgumentParser::ArgumentParser(
const Usage usage,
const int argc,
const char* const argv[],
const PositionalArgumentsRequired require_positional
)
: ArgumentParser(vector<Usage>{usage}, argc, argv, require_positional)
{ }
ArgumentParser::ArgumentParser(
const vector<Usage> usages,
const int argc,
const char* const argv[],
const PositionalArgumentsRequired require_positional
)
: ArgumentParser(
usages,
[&]() {
vector<std::string> args{};
for (auto i = 0; i < argc; ++i)
{
args.emplace_back(argv[i]);
}
return args;
}(),
require_positional
)
{ }
ArgumentParser::ArgumentParser(
const vector<Usage> usages,
const vector<string> arguments,
const PositionalArgumentsRequired require_positional
)
: ArgumentParser(
usages,
arguments,
[&]() {
auto bin = arguments.at(0);
replace(bin.begin(), bin.end(), '\\', '/');
const auto end = max(static_cast<size_t>(0), bin.rfind('/') + 1);
auto directory = bin.substr(0, end);
auto name = bin.substr(end, bin.size() - end);
return std::make_pair(directory, name);
}(),
require_positional
)
{ }
// HACK: already parsed binary from arg 0
ArgumentParser::ArgumentParser(
const vector<Usage> usages,
const vector<string> arguments,
const string binary_directory,
const string binary_name,
const PositionalArgumentsRequired require_positional
)
: require_positional_{require_positional}, cur_arg_{1}, arguments_{arguments},
// HACK: already parsed binary from arg 0
binary_directory_{binary_directory}, binary_name_{binary_name}
{
// HACK: need output directory so find first thing without a -
auto output_directory = [&]() -> string {
size_t i = 1;
while (i < arguments.size())
{
const string arg = arguments.at(i);
if (!arg.starts_with("-"))
{
auto d = arg;
replace(d.begin(), d.end(), '\\', '/');
if ('/' != d[d.length() - 1])
{
d += '/';
}
return d;
}
++i;
}
return {};
}(); // HACK: count -v and -q before anything to get right log level
constexpr auto log_default = logging::level::note;
logging::set_log_level(log_default);
for (const auto& arg : arguments)
{
if (arg.starts_with("-") && !arg.starts_with(("--")))
{
// HACK: not quite right if somehow a positional arg could start with '-' and have letters
// increment for each -v and decrement for each -q
for (const auto c : arg)
{
if ('q' == c)
{
logging::decrease_log_level();
}
else if ('v' == c)
{
logging::increase_log_level();
}
}
}
}
// FIX: doing this here means we always see the settings if we haven't adjusted log level
// if there is a settings.ini in the output directory then use that
logging::note("Checking for {:s}", output_directory + "settings.ini");
Settings::setRoot(binary_directory_, output_directory);
logging::check_fatal(nullptr != PARSER, "Parser initialized multiple times");
PARSER = this;
add_usages(usages);
fs::show_debug_settings();
assert(1 == cur_arg_);
// HACK: revert log level so -v and -q set it
logging::set_log_level(log_default);
register_flag(help_requested_, true, "-h", "Show help");
// can be used multiple times
register_argument("-v", "Increase output level", false, &logging::increase_log_level);
// if they want to specify -v and -q then that's fine
register_argument("-q", "Decrease output level", false, &logging::decrease_log_level);
}
Settings& ArgumentParser::parse_args()
{
// HACK: resolve once and fail if not set already
static auto& settings = fs::settings::instance();
auto& args = args_expanded();
if (1 == args.size())
{
help_requested_ = true;
}
while (cur_arg_ < args.size())
{
const string arg = args.at(cur_arg_);
bool is_positional = !arg.starts_with("-");
if (!is_positional)
{
// check for single letter flags or '--'
if (PARSE_FCT.find(arg) != PARSE_FCT.end())
{
logging::debug("Found option for argument '{:s}'", arg);
try
{
PARSE_FCT[arg]();
}
catch (std::exception&)
{
// cur_arg_ would be incremented while trying to parse at this point, so -1 is 'arg'
cout << std::format(
"\n'{:s}' is not a valid value for argument {:s}\n\n", args.at(cur_arg_), arg
);
show_usage_and_exit();
}
}
else
{
if (arg.starts_with("--"))
{
// anything starting with '--' should be a flag, but it's not a valid one so complain
cout << std::format("\n'{:s}' is not a valid option\n\n", arg);
show_usage_and_exit();
}
is_positional = true;
}
}
if (is_positional)
{
// this is a positional argument so add to that list
positional_args_.emplace_back(arg);
logging::debug("Found positional argument '{:s}'", arg);
}
++cur_arg_;
}
if (help_requested_)
{
return settings;
}
for (auto& kv : PARSE_REQUIRED)
{
if (kv.second && PARSE_HAVE.end() == PARSE_HAVE.find(kv.first))
{
exit(logging::fatal("{:s} must be specified", kv.first));
}
}
if ((PositionalArgumentsRequired::Required == require_positional_)
== (0 == positional_args_.size()))
{
show_usage_and_exit();
}
// HACK: should never happen
return settings;
}
bool ArgumentParser::has_positional() const { return (cur_positional_ < positional_args_.size()); };
string ArgumentParser::get_positional()
{
if (!has_positional())
{
logging::error("Not enough positional arguments");
show_usage_and_exit();
}
// return from front and advance to next
return positional_args_[cur_positional_++];
}
void ArgumentParser::done_positional()
{
// should be exactly at size since increments after getting argument
if (positional_args_.size() != cur_positional_)
{
logging::error("Too many positional arguments");
show_usage_and_exit();
}
// HACK: resolve once and fail if not set already
static const auto& settings = settings::instance();
// HACK: save settings here since should be parsed
settings.saveTo(settings.output_directory);
}
static const Usage USAGE_MAIN{
"Run simulations and save output in the specified directory",
"<output_dir> <yyyy-mm-dd> <lat> <lon> <HH:MM>"
};
static const Usage USAGE_SURFACE{
"Calculate probability surface and save output in the specified directory",
"surface <output_dir> <yyyy-mm-dd> <lat> <lon> <HH:MM>"
};
static const Usage USAGE_TEST{
"Run test cases and save output in the specified directory",
"test <output_dir>"
};
static const vector<Usage> DEFAULT_USAGES{USAGE_MAIN, USAGE_SURFACE, USAGE_TEST};
Settings& SettingsArgumentParser::parse_args() { return ArgumentParser::parse_args(); }
MainArgumentParser::MainArgumentParser(const int argc, const char* const argv[])
: SettingsArgumentParser(DEFAULT_USAGES, argc, argv)
{
// HACK: resolve once and fail if not set already
static auto& settings = fs::settings::instance();
register_flag(settings.save_as_ascii, true, "--ascii", "Save grids as .asc");
register_flag(settings.save_as_tiff, false, "--no-tiff", "Do not save grids as .tif");
if (arguments_.size() > 1 && 0 == strcmp(arguments_.at(1).c_str(), "test"))
{
settings.mode = Mode::Test;
cur_arg_ += 1;
skipped_args_ = 1;
}
if (arguments_.size() > 1 && 0 == strcmp(arguments_.at(1).c_str(), "surface"))
{
settings.mode = Mode::Surface;
// skip 'surface' argument if present
cur_arg_ += 1;
skipped_args_ = 1;
}
if (Mode::Test == settings.mode)
{
// defaults for test mode - no way to specify others right now
const auto year = 2020;
const auto month = 6;
const auto day = 15;
const auto hour = 12;
const auto minute = 0;
settings.start_date = to_tm(year, month, day, hour, minute);
settings.latitude = 49.3911;
settings.longitude = -84.7395;
logging::note("Running in test mode");
// if we have a directory and nothing else then use defaults for single run
// if we have 'all' then overrride specified indices, but then filter down to the subset that
// matches what was specified
register_setter<
MathSize>(settings.hours, "--hours", "Duration in hours", false, &parse_value<MathSize>);
register_setter<string>(settings.fuel_name, "--fuel", "FBP fuel type", false, &parse_string);
register_index<Ffmc>(settings.ffmc, "--ffmc", "Constant Fine Fuel Moisture Code", false);
register_index<Dmc>(settings.dmc, "--dmc", "Constant Duff Moisture Code", false);
register_index<Dc>(settings.dc, "--dc", "Constant Drought Code", false);
register_setter<
MathSize>(settings.wind_direction, "--wd", "Constant wind direction", false, &parse_value<MathSize>);
register_setter<
MathSize>(settings.wind_speed, "--ws", "Constant wind speed", false, &parse_value<MathSize>);
register_setter<
SlopeSize>(settings.slope, "--slope", "Constant slope", false, &parse_value<SlopeSize>);
register_setter<
AspectSize>(settings.aspect, "--aspect", "Constant slope aspect/azimuth", false, &parse_value<AspectSize>);
register_setter<size_t>(
[&](const auto v) { settings.static_curing = v; },
"--curing",
"Specify static grass curing",
false,
&parse_size_t
);
register_flag(settings.force_greenup, true, "--force-greenup", "Force green up for all fires");
register_flag(
settings.force_no_greenup, true, "--force-no-greenup", "Force no green up for all fires"
);
}
else
{
register_flag(settings.save_individual, true, "-i", "Save individual maps for simulations");
register_flag(settings.run_async, false, "-s", "Run in synchronous mode");
register_flag(settings.save_points, true, "--points", "Save simulation points to file");
register_flag(
settings.save_intensity, false, "--no-intensity", "Do not output intensity grids"
);
register_flag(
settings.save_probability, false, "--no-probability", "Do not output probability grids"
);
register_flag(settings.save_occurrence, true, "--occurrence", "Output occurrence grids");
register_flag(
settings.save_simulation_area, true, "--sim-area", "Output simulation area grids"
);
register_path_setter(
settings.raster_root, "--raster-root", "Use specified directory as raster root", false
);
register_path_setter(
settings.fuel_lookup, "--fuel-lut", "Use specified fuel lookup table", false
);
register_setter<
DurationSize>(settings.utc_offset, "--tz", "UTC offset (hours)", false, &parse_value<DurationSize>);
register_setter<size_t>(
[&](const auto v) { settings.static_curing = v; },
"--curing",
"Specify static grass curing",
false,
&parse_size_t
);
register_flag(settings.force_greenup, true, "--force-greenup", "Force green up for all fires");
register_flag(
settings.force_no_greenup, true, "--force-no-greenup", "Force no green up for all fires"
);
register_setter<string>(
settings.log_file_name, "--log", "Output log file", false, &parse_string
);
register_setter<size_t>(
settings.salt,
"--salt",
"Specify salt to use for random seeds (default 0)",
false,
&parse_size_t
);
if (Mode::Surface == settings.mode)
{
logging::note("Running in probability surface mode");
register_index<Ffmc>(settings.ffmc, "--ffmc", "Constant Fine Fuel Moisture Code", true);
register_index<Dmc>(settings.dmc, "--dmc", "Constant Duff Moisture Code", true);
register_index<Dc>(settings.dc, "--dc", "Constant Drought Code", true);
register_setter<
MathSize>(settings.wind_direction, "--wd", "Constant wind direction", true, &parse_value<MathSize>);
register_setter<
MathSize>(settings.wind_speed, "--ws", "Constant wind speed", true, &parse_value<MathSize>);
}
else
{
register_path_setter(settings.wx_file_name, "--wx", "Input weather file", true);
register_flag(
settings.deterministic,
true,
"--deterministic",
"Run deterministically (100% chance of spread & survival)"
);
register_setter<
ThresholdSize>(settings.confidence_level, "--confidence", "Use specified confidence level", false, &parse_value<ThresholdSize>);
register_path_setter(settings.perimeter, "--perim", "Start from perimeter", false);
register_setter<size_t>(
settings.initial_size, "--size", "Start from size", false, &parse_size_t
);
// HACK: want different text for same flag so define here too
register_index<Ffmc>(settings.ffmc, "--ffmc", "Startup Fine Fuel Moisture Code", true);
register_index<Dmc>(settings.dmc, "--dmc", "Startup Duff Moisture Code", true);
register_index<Dc>(settings.dc, "--dc", "Startup Drought Code", true);
register_index<Precipitation>(
settings.apcp_prev,
"--apcp_prev",
"Startup precipitation between 1200 yesterday and start of hourly weather",
false
);
}
register_setter<string>(
[&](const auto v) { settings.output_date_offsets = OutputDateOffsets{v}; },
"--output_date_offsets",
"Override output date offsets",
false,
&parse_string
);
}
if (Mode::Simulation == settings.mode)
{
register_flag(
settings.no_search,
true,
"--no-search",
"Do not search for a start location if start point is non-fuel"
);
}
}
Settings& MainArgumentParser::parse_args()
{
auto& settings = SettingsArgumentParser::parse_args();
if (help_requested())
{
return settings;
}
// fs::show_debug_settings();
// parse positional arguments
// output directory is always the first thing
// positional arguments all start with <output_dir> after mode (if applicable)
// "./firestarr [surface] <output_dir> <yyyy-mm-dd> <lat> <lon> <HH:MM> [options] [-v | -q]"
settings.output_directory = [&]() {
auto d = get_positional();
replace(d.begin(), d.end(), '\\', '/');
if ('/' != d[d.length() - 1])
{
d += '/';
}
return d;
}();
// if name starts with "/" then it's an absolute path, otherwise append to working directory
settings.log_file = (settings.log_file_name.starts_with("/") ? "" : settings.output_directory)
+ settings.log_file_name;
// HACK: ensure settings initialized before doing this
// probabalistic surface is computationally impossible at this point
if (settings.is_surface())
{
settings.deterministic = true;
}
if (!settings.is_test())
{
// handle surface/simulation positional arguments
// positional arguments should be:
// "./firestarr [surface] <output_dir> <yyyy-mm-dd> <lat> <lon> <HH:MM> [options] [-v | -q]"
// require all positional arguments or none
if (has_positional())
{
// NOTE: these will overwrite any values in the settings file that exist
settings.start_date = parse_date(get_positional());
auto& start_date = settings.start_date.value();
settings.latitude = stod(get_positional());
settings.longitude = stod(get_positional());
string arg(get_positional());
if (5 == arg.size() && ':' == arg[2])
{
try
{
add_time(start_date, arg);
}
catch (std::exception&)
{
show_usage_and_exit();
}
}
}
else
{
auto check_have = [&](const string& v) {
logging::check_fatal(
!settings.found(v), "No positional arguments specified and missing value for {:s}", v
);
};
for (const auto& k : {"START_DATE", "LATITUDE", "LONGITUDE", "START_TIME"})
{
check_have(k);
}
}
}
else
{
// test mode
if (has_positional())
{
const auto arg = get_positional();
if (0 != strcmp(arg.c_str(), "all"))
{
logging::error(
"Only positional argument allowed for test mode aside from output directory is 'all' but got '{:s}'",
arg
);
show_usage_and_exit();
}
settings.test_all = true;
}
}
done_positional();
return settings;
}
string ArgumentParser::cur_arg() { return args_expanded().at(cur_arg_); };
bool parse_flag(bool not_inverse)
{
return parse_once<bool>([not_inverse] { return not_inverse; });
}
vector<string>& ArgumentParser::args_expanded()
{
// if empty then not parsed, or parsing is fast since no arguments
if (arguments_expanded_.empty())
{
arguments_expanded_ = [&]() {
vector<std::string> args{};
for (const auto& s : arguments_)
{
if (s.starts_with("-") && !s.starts_with("--"))
{
// break anything starting with just one - into individual letters
for (size_t i = 1; i < s.length(); ++i)
{
const string arg = string("-") + s.at(i);
// if this isn't a flag then don't expand it
if (PARSE_FCT.find(arg) != PARSE_FCT.end())
{
args.emplace_back(arg);
}
else if (1 == i)
{
// if this is just the start of a non-flag then leave it alone
args.emplace_back(s);
break;
}
else
{
exit(logging::fatal(
"Invalid argument {:s} found as part of combined flag argument {:s}", arg, s
));
}
}
}
else
{
args.emplace_back(s);
}
}
return args;
}();
}
return arguments_expanded_;
}
}