From eec4787413e8590bb313d0537ec3b2360d2292c3 Mon Sep 17 00:00:00 2001 From: Fatih Cetinkaya <965295+devfacet@users.noreply.github.com> Date: Wed, 25 Mar 2026 22:48:30 -0400 Subject: [PATCH] Add space support to flags --- src/tl_app.c | 13 ++++++++++--- tests/unit/tl_app_test.c | 15 +++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/tl_app.c b/src/tl_app.c index 498bd00..71bb3e7 100644 --- a/src/tl_app.c +++ b/src/tl_app.c @@ -36,9 +36,16 @@ bool tl_lookup_flag(const char *flag) { const char *tl_get_flag(const char *flag) { size_t flag_len = strlen(flag); for (int i = 1; i < arg_count; i++) { - // If the argument starts with the flag and is followed by '=' then - if (strncmp(args[i], flag, flag_len) == 0 && args[i][flag_len] == '=') { - return args[i] + flag_len + 1; // +1 to skip the '=' + if (strncmp(args[i], flag, flag_len) != 0) { + continue; + } + // If the argument is followed by '=' then return the value after '=' + if (args[i][flag_len] == '=') { + return args[i] + flag_len + 1; + } + // If the argument is an exact match and the next argument exists then return it + if (args[i][flag_len] == '\0' && i + 1 < arg_count) { + return args[i + 1]; } } return NULL; diff --git a/tests/unit/tl_app_test.c b/tests/unit/tl_app_test.c index f9a5848..750f557 100644 --- a/tests/unit/tl_app_test.c +++ b/tests/unit/tl_app_test.c @@ -36,6 +36,19 @@ static void test_tl_get_flag(void) { TEST_ASSERT_NULL(tl_get_flag("--nonexistent-key")); } +static void test_tl_get_flag_space(void) { + char *argv[] = {"program", "--key", "value"}; + tl_init_app(3, argv); + TEST_ASSERT_EQUAL_STRING("value", tl_get_flag("--key")); +} + +static void test_tl_init_app_space(void) { + char *argv[] = {"program", "--debug-level", "3"}; + tl_init_app(3, argv); + TEST_ASSERT_TRUE(tl_lookup_flag("--debug-level")); + TEST_ASSERT_EQUAL_STRING("3", tl_get_flag("--debug-level")); +} + int main(void) { UNITY_BEGIN(); @@ -43,6 +56,8 @@ int main(void) { RUN_TEST(test_tl_parse_args); RUN_TEST(test_tl_lookup_flag); RUN_TEST(test_tl_get_flag); + RUN_TEST(test_tl_get_flag_space); + RUN_TEST(test_tl_init_app_space); return UNITY_END(); }