Skip to content

Commit e069708

Browse files
committed
WIP: convert to not use global variables in ArgumentParser
1 parent 28e8c73 commit e069708

2 files changed

Lines changed: 22 additions & 19 deletions

File tree

src/cpp/fs/ArgumentParser.cpp

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ static map<std::string, bool> PARSE_REQUIRED{};
1010
static map<std::string, bool> PARSE_HAVE{};
1111
static string BIN_DIR{};
1212
static string BIN_NAME{};
13-
static int ARGC = 0;
1413
static size_t SKIPPED_ARGS = 0;
15-
static const char* const* ARGV = nullptr;
1614
static int CUR_ARG = 0;
1715
ArgumentParser* PARSER{nullptr};
1816
void ArgumentParser::mark_parsed(const char* arg) { PARSE_HAVE.emplace(arg, true); }
@@ -79,11 +77,11 @@ void register_index(T& index, string v, string help, bool required)
7977
}
8078
string ArgumentParser::get_args()
8179
{
82-
std::string args(ARGV[0]);
83-
for (auto i = 1; i < ARGC; ++i)
80+
std::string args(argv_[0]);
81+
for (auto i = 1; i < argc_; ++i)
8482
{
8583
args.append(" ");
86-
args.append(ARGV[i]);
84+
args.append(argv_[i]);
8785
}
8886
return args;
8987
}
@@ -142,8 +140,8 @@ void ArgumentParser::show_help_and_exit()
142140
const char* ArgumentParser::get_arg() noexcept
143141
{
144142
// check if we don't have any more arguments
145-
fs::logging::check_fatal(CUR_ARG + 1 >= ARGC, "Missing argument to --%s", ARGV[CUR_ARG]);
146-
return ARGV[++CUR_ARG];
143+
fs::logging::check_fatal(CUR_ARG + 1 >= argc_, "Missing argument to --%s", argv_[CUR_ARG]);
144+
return argv_[++CUR_ARG];
147145
}
148146
size_t parse_size_t()
149147
{
@@ -184,16 +182,14 @@ ArgumentParser::ArgumentParser(
184182
const char* const argv[],
185183
const PositionalArgumentsRequired require_positional
186184
)
187-
: require_positional_{require_positional}
185+
: require_positional_{require_positional}, argc_{argc}, argv_{argv}
188186
{
189187
logging::check_fatal(nullptr != PARSER, "Parser initialized multiple times");
190188
PARSER = this;
191189
add_usages(usages);
192190
fs::show_debug_settings();
193-
ARGC = argc;
194-
ARGV = argv;
195191
assert(0 == CUR_ARG);
196-
auto bin = string(ARGV[CUR_ARG++]);
192+
auto bin = string(argv_[CUR_ARG++]);
197193
replace(bin.begin(), bin.end(), '\\', '/');
198194
const auto end = max(static_cast<size_t>(0), bin.rfind('/') + 1);
199195
BIN_DIR = bin.substr(0, end);
@@ -207,9 +203,9 @@ ArgumentParser::ArgumentParser(
207203
}
208204
void ArgumentParser::parse_args()
209205
{
210-
while (CUR_ARG < ARGC)
206+
while (CUR_ARG < argc_)
211207
{
212-
const string arg = ARGV[CUR_ARG];
208+
const string arg = argv_[CUR_ARG];
213209
bool is_positional = !arg.starts_with("-");
214210
if (!is_positional)
215211
{
@@ -224,7 +220,7 @@ void ArgumentParser::parse_args()
224220
catch (std::exception&)
225221
{
226222
// CUR_ARG would be incremented while trying to parse at this point, so -1 is 'arg'
227-
printf("\n'%s' is not a valid value for argument %s\n\n", ARGV[CUR_ARG], arg.c_str());
223+
printf("\n'%s' is not a valid value for argument %s\n\n", argv_[CUR_ARG], arg.c_str());
228224
show_usage_and_exit();
229225
}
230226
}
@@ -307,7 +303,7 @@ MainArgumentParser::MainArgumentParser(const int argc, const char* const argv[])
307303
{
308304
register_flag(&Settings::setSaveAsAscii, true, "--ascii", "Save grids as .asc");
309305
register_flag(&Settings::setSaveAsTiff, false, "--no-tiff", "Do not save grids as .tif");
310-
if (ARGC > 1 && 0 == strcmp(ARGV[1], "test"))
306+
if (argc_ > 1 && 0 == strcmp(argv_[1], "test"))
311307
{
312308
fs::logging::note("Running in test mode");
313309
mode = TEST;
@@ -386,7 +382,7 @@ MainArgumentParser::MainArgumentParser(const int argc, const char* const argv[])
386382
false,
387383
&parse_size_t
388384
);
389-
if (ARGC > 1 && 0 == strcmp(ARGV[1], "surface"))
385+
if (argc_ > 1 && 0 == strcmp(argv_[1], "surface"))
390386
{
391387
fs::logging::note("Running in probability surface mode");
392388
mode = SURFACE;
@@ -435,12 +431,12 @@ MainArgumentParser::MainArgumentParser(const int argc, const char* const argv[])
435431
false,
436432
&parse_raw
437433
);
438-
if (2 == ARGC && 0 == strcmp(ARGV[CUR_ARG], "-h"))
434+
if (2 == argc_ && 0 == strcmp(argv_[CUR_ARG], "-h"))
439435
{
440436
// HACK: just do this for now
441437
show_help_and_exit();
442438
}
443-
else if (3 > (ARGC - SKIPPED_ARGS))
439+
else if (3 > (argc_ - SKIPPED_ARGS))
444440
{
445441
show_usage_and_exit();
446442
}
@@ -495,7 +491,7 @@ FwiWeather MainArgumentParser::get_yesterday_weather() const
495491
dc
496492
};
497493
}
498-
const char* ArgumentParser::cur_arg() { return ARGV[CUR_ARG]; };
494+
const char* ArgumentParser::cur_arg() { return argv_[CUR_ARG]; };
499495
bool parse_flag(bool not_inverse)
500496
{
501497
return parse_once<bool>([not_inverse] { return not_inverse; });

src/cpp/fs/ArgumentParser.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ class ArgumentParser
6868
void show_usage_and_exit(int exit_code);
6969
void show_usage_and_exit();
7070
void show_help_and_exit();
71+
int argc() const { return argc_; }
72+
73+
protected:
74+
const int argc_;
75+
const char* const* argv_;
76+
string binary_directory_{};
77+
string binary_name_{};
7178
};
7279
enum MODE
7380
{

0 commit comments

Comments
 (0)