From f49210113a8e8940b381f3f8b06ea4eea65e8578 Mon Sep 17 00:00:00 2001 From: "D. Bohdan" Date: Sat, 30 Sep 2023 11:44:29 +0000 Subject: [PATCH] feat(cli): catch unknown options Use "unknown" instead of "invalid". It may be less ambiguous. (The option/command is not implemented at all, not just invalid in the context.) --- cli.c | 24 ++++++++++++++++++------ tests/hicolor.test | 13 +++++++++---- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/cli.c b/cli.c index eebad63..1743f35 100644 --- a/cli.c +++ b/cli.c @@ -427,11 +427,13 @@ int main(int argc, char** argv) return 1; } - /* The regular "help" command is handled later with other commands. */ - if (strcmp(argv[1], "-h") == 0 - || strcmp(argv[1], "--help") == 0) { - help(); - return 0; + /* The regular "help" command is handled later with the rest. */ + for (int i = 0; i < argc; i++) { + if (strcmp(argv[i], "-h") == 0 + || strcmp(argv[i], "--help") == 0) { + help(); + return 0; + } } int i = 1; @@ -466,7 +468,8 @@ int main(int argc, char** argv) } else { fprintf( stderr, - HICOLOR_CLI_ERROR "invalid command \"%s\"\n", argv[i] + HICOLOR_CLI_ERROR "unknown command \"%s\"\n", + argv[i] ); usage(stderr); @@ -489,6 +492,15 @@ int main(int argc, char** argv) } else if (strcmp(argv[i], "-n") == 0 || strcmp(argv[i], "--no-dither") == 0) { opt_dither = false; + } else { + fprintf( + stderr, + HICOLOR_CLI_ERROR "unknown option \"%s\"\n", + argv[i] + ); + usage(stderr); + return 1; + } i++; diff --git a/tests/hicolor.test b/tests/hicolor.test index 47772e4..f28c0d3 100755 --- a/tests/hicolor.test +++ b/tests/hicolor.test @@ -195,13 +195,18 @@ tcltest::test quantize-2.3 {bad input} -body { } -returnCodes error -match glob -result {error: can't load PNG file*} -tcltest::test invalid-command-1.1 {} -body { +tcltest::test unknown-command-1.1 {} -body { hicolor -5 src.png -} -returnCodes error -match glob -result {error: invalid command "-5"*} +} -returnCodes error -match glob -result {error: unknown command "-5"*} -tcltest::test invalid-command-1.2 {} -body { +tcltest::test unknown-command-1.2 {} -body { hicolor encoder -} -returnCodes error -match glob -result {error: invalid command "encoder"*} +} -returnCodes error -match glob -result {error: unknown command "encoder"*} + + +tcltest::test unknown-option-1.1 {} -body { + hicolor encode --wrong fo bar +} -returnCodes error -match glob -result {error: unknown option "--wrong"*} tcltest::test no-arguments-1.1 {} -body {