Skip to content

Commit

Permalink
feat(cli): catch unknown options
Browse files Browse the repository at this point in the history
Use "unknown" instead of "invalid".
It may be less ambiguous.
(The option/command is not implemented at all,
not just invalid in the context.)
  • Loading branch information
dbohdan committed Sep 30, 2023
1 parent 43f73b9 commit f492101
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
24 changes: 18 additions & 6 deletions cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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++;
Expand Down
13 changes: 9 additions & 4 deletions tests/hicolor.test
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit f492101

Please sign in to comment.