Skip to content

Commit

Permalink
feat(cli): handle help like other commands
Browse files Browse the repository at this point in the history
Keep `-h` and `--help` special-cased.
  • Loading branch information
dbohdan committed Sep 30, 2023
1 parent 5f1dde1 commit 43f73b9
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ bool str_prefix(const char* ref, const char* str)
}

typedef enum command {
ENCODE, DECODE, QUANTIZE, INFO, VERSION
ENCODE, DECODE, QUANTIZE, INFO, VERSION, HELP
} command;

int main(int argc, char** argv)
Expand All @@ -427,8 +427,8 @@ int main(int argc, char** argv)
return 1;
}

if (str_prefix(HICOLOR_CLI_CMD_HELP, argv[1])
|| strcmp(argv[1], "-h") == 0
/* The regular "help" command is handled later with other commands. */
if (strcmp(argv[1], "-h") == 0
|| strcmp(argv[1], "--help") == 0) {
help();
return 0;
Expand Down Expand Up @@ -457,6 +457,12 @@ int main(int argc, char** argv)
min_pos_args = 0;
max_pos_args = 0;
opt_command = VERSION;
} else if (str_prefix(HICOLOR_CLI_CMD_HELP, argv[i])) {
allow_opts = false;
command_name = HICOLOR_CLI_CMD_HELP;
min_pos_args = 0;
max_pos_args = 0;
opt_command = HELP;
} else {
fprintf(
stderr,
Expand Down Expand Up @@ -539,5 +545,8 @@ int main(int argc, char** argv)
case VERSION:
version();
return 0;
case HELP:
help();
return 0;
}
}

0 comments on commit 43f73b9

Please sign in to comment.