diff options
author | Joel Kronqvist <joel.kronqvist@iki.fi> | 2024-05-31 15:29:42 +0300 |
---|---|---|
committer | Joel Kronqvist <joel.kronqvist@iki.fi> | 2024-06-30 08:31:37 +0300 |
commit | 576b7a70dbf6c43651dc77db3a0104c019eed018 (patch) | |
tree | 6ca087eb2e3bd27d876ad2842d67cee2d7e09cbf /config-parser.c | |
parent | 41de123de0453274fb2e5b2192548ce274ad8deb (diff) | |
download | stdu-576b7a70dbf6c43651dc77db3a0104c019eed018.tar.gz stdu-576b7a70dbf6c43651dc77db3a0104c019eed018.zip |
Added `--forward`work/1.1.0
Diffstat (limited to 'config-parser.c')
-rw-r--r-- | config-parser.c | 76 |
1 files changed, 65 insertions, 11 deletions
diff --git a/config-parser.c b/config-parser.c index ef67a8e..b09bbc2 100644 --- a/config-parser.c +++ b/config-parser.c @@ -14,10 +14,11 @@ Result parse_config(int argc, char* argv[]) { Result res; res.success = false; Config tmp; - tmp.precision = 0; - tmp.multiline = false; + tmp.forward = false; tmp.human_readable = false; tmp.help = false; + tmp.multiline = false; + tmp.precision = 0; size_t i = 1; char* argument; @@ -58,7 +59,9 @@ Result parse_config(int argc, char* argv[]) { size_t tacktacklen = 2; if (strncmp(argument, "--", tacktacklen) == 0) { argument += tacktacklen; - if (strcmp(argument, "help") == 0) { + if (strcmp(argument, "forward") == 0) { + tmp.forward = true; + } else if (strcmp(argument, "help") == 0) { tmp.help = true; } else if (strcmp(argument, "human-readable") == 0) { tmp.human_readable = true; @@ -111,6 +114,9 @@ Result parse_config(int argc, char* argv[]) { case 'm': tmp.multiline = true; break; + case 'o': + tmp.forward = true; + break; case 'p': next_is_precision = true; if (opt_i != arglen - 1) { @@ -185,6 +191,7 @@ Result parse_config(int argc, char* argv[]) { } Config* conf = malloc(sizeof(Config)); + conf->forward = tmp.forward; conf->help = tmp.help; conf->human_readable = tmp.human_readable; conf->multiline = tmp.multiline; @@ -245,28 +252,73 @@ char* test_precision_parsing() { } +char* test_forward_parsing() { + int argc = 2; + char* argv1[] = { "stdu", "--forward" }; + char* argv2[] = { "stdu", "-o" }; + Result res1 = parse_config(argc, argv1); + Result res2 = parse_config(argc, argv2); + Config* conf1 = (Config*) res1.result; + Config* conf2 = (Config*) res2.result; + + mt_assert_eq(res1.success, true); + mt_assert_eq(res2.success, true); + mt_assert_eq(conf1->forward, true); + mt_assert_eq(conf2->forward, true); + free(conf1); + free(conf2); + + return 0; +} + +char* test_help_parsing() { + int argc = 2; + char* argv1[] = { "stdu", "--help" }; + char* argv2[] = { "stdu", "-?" }; + Result res1 = parse_config(argc, argv1); + Result res2 = parse_config(argc, argv2); + Config* conf1 = (Config*) res1.result; + Config* conf2 = (Config*) res2.result; + + mt_assert_eq(res1.success, true); + mt_assert_eq(res2.success, true); + mt_assert_eq(conf1->help, true); + mt_assert_eq(conf2->help, true); + free(conf1); + free(conf2); + + return 0; +} + char* test_human_readability_parsing() { int argc = 2; - char* argv[] = { "stdu", "--human-readable" }; - Result res = parse_config(argc, argv); - Config* conf = (Config*) res.result; + char* argv1[] = { "stdu", "--human-readable" }; + char* argv2[] = { "stdu", "-h" }; + Result res1 = parse_config(argc, argv1); + Result res2 = parse_config(argc, argv2); + Config* conf1 = (Config*) res1.result; + Config* conf2 = (Config*) res2.result; - mt_assert_eq(res.success, true); - mt_assert_eq(conf->human_readable, true); - free(conf); + mt_assert_eq(res1.success, true); + mt_assert_eq(res2.success, true); + mt_assert_eq(conf1->human_readable, true); + mt_assert_eq(conf2->human_readable, true); + free(conf1); + free(conf2); return 0; } char* test_multioption_parsing() { int argc = 3; - char* argv[] = { "stdu", "-mh?p", "3"}; + char* argv[] = { "stdu", "-mho?p", "3"}; Result res = parse_config(argc, argv); Config* conf = (Config*) res.result; mt_assert_eq(res.success, true); mt_assert_eq(conf->help, true); mt_assert_eq(conf->human_readable, true); mt_assert_eq(conf->multiline, true); + mt_assert_eq(conf->forward, true); mt_assert_eq(conf->precision, 3); free(conf); @@ -275,7 +327,9 @@ char* test_multioption_parsing() { void parsing_tests() { mt_run_test(test_default_config); - mt_run_test(test_precision_parsing); + mt_run_test(test_forward_parsing); mt_run_test(test_human_readability_parsing); + mt_run_test(test_help_parsing); mt_run_test(test_multioption_parsing); + mt_run_test(test_precision_parsing); } |