From 9a2a61ca289aeee256e104dda807431380eba319 Mon Sep 17 00:00:00 2001 From: Onur Gündüz Date: Wed, 17 Jun 2026 03:29:44 +0200 Subject: cli: reject unknown --options instead of treating them as text --- src/cli.c | 12 ++++++++++++ tests/test_cli.c | 10 ++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/cli.c b/src/cli.c index 2729c25..0e3ed7d 100644 --- a/src/cli.c +++ b/src/cli.c @@ -647,6 +647,18 @@ int main(int argc, char *argv[]) { else if (!strcmp(f, "-h") || !strcmp(f, "--help")) { print_usage(prog); return 0; + } else if (!strcmp(f, "--")) { + /* End of options: everything after is positional text, even if it + * starts with a dash. */ + arg_start++; + break; + } else if (f[0] == '-' && f[1] == '-') { + /* An unrecognized --option is a hard error, not silently-embedded + * text. Catches typos and stale flags (e.g. the removed --backend) + * loudly instead of running the default path or embedding the flag. */ + fprintf(stderr, "unknown option: %s\n", f); + print_usage(prog); + return 1; } else break; arg_start++; diff --git a/tests/test_cli.c b/tests/test_cli.c index 8d56f09..7543717 100644 --- a/tests/test_cli.c +++ b/tests/test_cli.c @@ -178,6 +178,16 @@ int main(int argc, char **argv) { for (size_t i = 0; i < sizeof(bad) / sizeof(bad[0]); i++) expect(run_cli(bad[i].args, NULL) == 1, bad[i].what); + /* Unknown --options fail loudly instead of being silently embedded as text + * (guards against typos and removed flags like --backend). A bare -- ends + * option parsing so dash-prefixed text can still be embedded. */ + snprintf(args, sizeof(args), "-d '%s' --backend cuda hi", g_dir); + expect(run_cli(args, NULL) == 1, "unknown --option exits 1"); + expect(file_contains(g_err, "unknown option"), "unknown --option names the flag"); + snprintf(args, sizeof(args), "-d '%s' -- '-dashy text'", g_dir); + expect(run_cli(args, NULL) == 0, "-- terminator allows dash-prefixed text"); + expect(stdout_is_embedding(dims.hidden), "dash text after -- embeds as one input"); + if (g_failures) { fprintf(stderr, "%d CLI check(s) failed\n", g_failures); return 1; -- cgit v1.3.1