Skip to content

Commit

Permalink
feat(cli): print command name on too many/few args
Browse files Browse the repository at this point in the history
  • Loading branch information
dbohdan committed Sep 30, 2023
1 parent f4e1460 commit 5f1dde1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
37 changes: 29 additions & 8 deletions cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@
#define HICOLOR_CLI_ERROR "error: "
#define HICOLOR_CLI_NO_MEMORY_EXIT_CODE 255

#define HICOLOR_CLI_CMD_ENCODE "encode"
#define HICOLOR_CLI_CMD_QUANTIZE "quantize"
#define HICOLOR_CLI_CMD_DECODE "decode"
#define HICOLOR_CLI_CMD_INFO "info"
#define HICOLOR_CLI_CMD_VERSION "version"
#define HICOLOR_CLI_CMD_HELP "help"

bool check_and_report_error(char* step, hicolor_result res)
{
if (res == HICOLOR_OK) return false;
Expand Down Expand Up @@ -408,6 +415,7 @@ int main(int argc, char** argv)
command opt_command = ENCODE;
bool opt_dither = true;
hicolor_version opt_version = HICOLOR_VERSION_6;
const char* command_name;
char* arg_src;
char* arg_dest;
bool allow_opts = true;
Expand All @@ -419,7 +427,7 @@ int main(int argc, char** argv)
return 1;
}

if (str_prefix("help", argv[1])
if (str_prefix(HICOLOR_CLI_CMD_HELP, argv[1])
|| strcmp(argv[1], "-h") == 0
|| strcmp(argv[1], "--help") == 0) {
help();
Expand All @@ -428,19 +436,24 @@ int main(int argc, char** argv)

int i = 1;

if (str_prefix("encode", argv[i])) {
if (str_prefix(HICOLOR_CLI_CMD_ENCODE, argv[i])) {
command_name = HICOLOR_CLI_CMD_ENCODE;
opt_command = ENCODE;
} else if (str_prefix("decode", argv[i])) {
} else if (str_prefix(HICOLOR_CLI_CMD_DECODE, argv[i])) {
allow_opts = false;
command_name = HICOLOR_CLI_CMD_DECODE;
opt_command = DECODE;
} else if (str_prefix("quantize", argv[i])) {
} else if (str_prefix(HICOLOR_CLI_CMD_QUANTIZE, argv[i])) {
command_name = HICOLOR_CLI_CMD_QUANTIZE;
opt_command = QUANTIZE;
} else if (str_prefix("info", argv[i])) {
} else if (str_prefix(HICOLOR_CLI_CMD_INFO, argv[i])) {
allow_opts = false;
command_name = HICOLOR_CLI_CMD_INFO;
max_pos_args = 1;
opt_command = INFO;
} else if (str_prefix("version", argv[i])) {
} else if (str_prefix(HICOLOR_CLI_CMD_VERSION, argv[i])) {
allow_opts = false;
command_name = HICOLOR_CLI_CMD_VERSION;
min_pos_args = 0;
max_pos_args = 0;
opt_command = VERSION;
Expand Down Expand Up @@ -479,13 +492,21 @@ int main(int argc, char** argv)
int rem_args = argc - i;

if (rem_args < min_pos_args) {
fprintf(stderr, HICOLOR_CLI_ERROR "too few arguments\n");
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) {
fprintf(stderr, HICOLOR_CLI_ERROR "too many arguments\n");
fprintf(
stderr,
HICOLOR_CLI_ERROR "too many arguments to command \"%s\"\n",
command_name
);
usage(stderr);
return 1;
}
Expand Down
3 changes: 2 additions & 1 deletion tests/hicolor.test
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ tcltest::test help-1.3 {} -body {

tcltest::test encode-1.1 {} -body {
hicolor encode
} -returnCodes error -match glob -result {*too few arg*}
} -returnCodes error -match glob -result {*no source image given to command\
"encode"*}

tcltest::test encode-1.2 {} -body {
hicolor encode photo.png
Expand Down

0 comments on commit 5f1dde1

Please sign in to comment.