From df884ede5477d2564e1a9862755ca021270bb74d Mon Sep 17 00:00:00 2001 From: "D. Bohdan" Date: Mon, 18 Nov 2024 08:22:28 +0000 Subject: [PATCH] feat(cli): print usage before error The usage message is multiline. It is better to not make the user look several lines up to find what went wrong. --- cli.c | 8 ++++---- tests/hicolor.test | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/cli.c b/cli.c index 5150134..21ff0f2 100644 --- a/cli.c +++ b/cli.c @@ -658,12 +658,12 @@ int main( max_pos_args = 0; opt_command = HELP; } else { + usage(stderr); fprintf( stderr, HICOLOR_CLI_ERROR "unknown command \"%s\"\n", argv[i] ); - usage(stderr); return 1; } @@ -690,12 +690,12 @@ int main( || strcmp(argv[i], "--no-dither") == 0) { opt_dither = HICOLOR_NO_DITHER; } else { + usage(stderr); fprintf( stderr, HICOLOR_CLI_ERROR "unknown option \"%s\"\n", argv[i] ); - usage(stderr); return 1; } @@ -706,22 +706,22 @@ int main( int rem_args = argc - i; if (rem_args < min_pos_args) { + usage(stderr); fprintf( stderr, HICOLOR_CLI_ERROR "no source image given to command \"%s\"\n", command_name ); - usage(stderr); return 1; } if (rem_args > max_pos_args) { + usage(stderr); fprintf( stderr, HICOLOR_CLI_ERROR "too many arguments to command \"%s\"\n", command_name ); - usage(stderr); return 1; } diff --git a/tests/hicolor.test b/tests/hicolor.test index 1083359..aab7c93 100755 --- a/tests/hicolor.test +++ b/tests/hicolor.test @@ -204,16 +204,16 @@ tcltest::test quantize-2.3 {bad input} -body { tcltest::test unknown-command-1.1 {} -body { hicolor -5 src.png -} -returnCodes error -match glob -result {error: unknown command "-5"*} +} -returnCodes error -match glob -result {usage:*error: unknown command "-5"} tcltest::test unknown-command-1.2 {} -body { hicolor encoder -} -returnCodes error -match glob -result {error: unknown command "encoder"*} +} -returnCodes error -match glob -result {usage:*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"*} +} -returnCodes error -match glob -result {usage:*error: unknown option "--wrong"} tcltest::test no-arguments-1.1 {} -body {