Skip to content

Commit

Permalink
feat(cli): print usage before error
Browse files Browse the repository at this point in the history
The usage message is multiline.
It is better to not make the user look several lines up
to find what went wrong.
  • Loading branch information
dbohdan committed Nov 18, 2024
1 parent 40aa101 commit df884ed
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
8 changes: 4 additions & 4 deletions cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand Down
6 changes: 3 additions & 3 deletions tests/hicolor.test
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit df884ed

Please sign in to comment.