Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/tl_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/tl_app_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,28 @@ 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();

RUN_TEST(test_tl_init_app);
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();
}