diff --git a/src/commander.c b/src/commander.c index 8298d1a..24a23e7 100644 --- a/src/commander.c +++ b/src/commander.c @@ -46,9 +46,12 @@ command_help(command_t *self) { int i; for (i = 0; i < self->option_count; ++i) { + int omittedShort = 0; command_option_t *option = &self->options[i]; - printf(" %s, %-25s %s\n" - , option->small + omittedShort = option->small == NULL; + printf(" %s%c %-25s %s\n" + , omittedShort ? " " : option->small + , omittedShort ? ' ' : ',' , option->large_with_arg , option->description); } @@ -207,7 +210,7 @@ command_parse_args(command_t *self, int argc, char **argv) { command_option_t *option = &self->options[j]; // match flag - if (!strcmp(arg, option->small) || !strcmp(arg, option->large)) { + if ((option->small != NULL && !strcmp(arg, option->small)) || !strcmp(arg, option->large)) { self->arg = NULL; // required diff --git a/test.c b/test.c index e96b576..1edf185 100644 --- a/test.c +++ b/test.c @@ -17,6 +17,11 @@ optional(command_t *self) { printf("optional: %s\n", self->arg); } +static void +omit_short(command_t *self) { + printf("omitted short\n"); +} + int main(int argc, char **argv){ command_t cmd; @@ -24,6 +29,7 @@ main(int argc, char **argv){ command_option(&cmd, "-v", "--verbose", "enable verbose stuff", verbose); command_option(&cmd, "-r", "--required ", "required arg", required); command_option(&cmd, "-o", "--optional [arg]", "optional arg", optional); + command_option(&cmd, NULL, "--omit-short", "omit short version by passing NULL", omit_short); command_parse(&cmd, argc, argv); printf("additional args:\n"); for (int i = 0; i < cmd.argc; ++i) {