Skip to content

Commit ea5552b

Browse files
committed
Refactor arg parser
1 parent 533adaf commit ea5552b

File tree

2 files changed

+26
-25
lines changed

2 files changed

+26
-25
lines changed

src/arg-parser.hpp

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,30 @@ namespace cest
88
{
99
CommandLineOptions parseArgs(int argc, const char *argv[])
1010
{
11-
CommandLineOptions options = {0};
11+
CommandLineOptions options;
12+
std::map<std::string, std::function<void()>> parseSingle = {
13+
{"--help", [&]() { options.help = true; }},
14+
{"-h", [&]() { options.help = true; }},
15+
{"--randomize", [&]() { options.randomize = true; }},
16+
{"-r", [&]() { options.randomize = true; }},
17+
{"--json", [&]() { options.json_output = true; }},
18+
{"-j", [&]() { options.json_output = true; }},
19+
{"--only-suite-result", [&]() { options.only_test_suite_result = true; }},
20+
{"-o", [&]() { options.only_test_suite_result = true; }},
21+
{"--tree-suite-result", [&]() { options.only_test_suite_result = true; }},
22+
{"-t", [&]() { options.tree_test_suite_result = true; }}
23+
};
1224

1325
if (argc > 1)
1426
{
1527
for (int i = 0; i < argc; ++i)
1628
{
17-
if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0)
18-
{
19-
options.help = true;
20-
}
29+
const auto arg = std::string(argv[i]);
2130

22-
if (strcmp(argv[i], "-r") == 0 || strcmp(argv[i], "--randomize") == 0)
23-
{
24-
options.randomize = true;
25-
}
26-
27-
if (strcmp(argv[i], "-j") == 0 || strcmp(argv[i], "--json") == 0)
28-
{
29-
options.json_output = true;
30-
}
31-
32-
if (strcmp(argv[i], "-o") == 0 || strcmp(argv[i], "--only-suite-result") == 0)
33-
{
34-
options.only_test_suite_result = true;
35-
}
36-
37-
if (strcmp(argv[i], "-t") == 0 || strcmp(argv[i], "--tree-suite-result") == 0)
38-
{
39-
options.tree_test_suite_result = true;
40-
}
31+
if (parseSingle.contains(arg))
32+
parseSingle[arg]();
4133

42-
if (strcmp(argv[i], "-s") == 0 || strcmp(argv[i], "--seed") == 0)
34+
if (arg == "-s" || arg == "--seed")
4335
{
4436
if (i + 1 < argc)
4537
{

src/types.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,15 @@ namespace cest
6565
bool json_output;
6666
bool only_test_suite_result;
6767
bool tree_test_suite_result;
68+
69+
CommandLineOptions() :
70+
help(false),
71+
randomize(false),
72+
random_seed(0),
73+
random_seed_present(false),
74+
json_output(false),
75+
only_test_suite_result(false),
76+
tree_test_suite_result(false) {}
6877
};
6978

7079
struct CestGlobals

0 commit comments

Comments
 (0)