From: Christian Weisgerber Subject: Do not treat -h, -V as errors To: gameoftrees@openbsd.org Date: Tue, 29 Sep 2020 00:15:55 +0200 do not treat the -h and -V flags as errors When run with the -h or -V option, output the help or version text to stdout and exit with success (0). Only write usage and help information to stderr and exit with error (1) if there is a mistake in the command syntax. -------------------- Please check that I got all combinations right. OK? diff 9814e6a376df853a88deb889d68f178c803ad8ca /home/naddy/got blob - b8f662a112135fe0364e2a25eb0fe2976ba10cf1 file + got/got.c --- got/got.c +++ got/got.c @@ -83,7 +83,7 @@ struct got_cmd { const char *cmd_alias; }; -__dead static void usage(int); +__dead static void usage(int, int); __dead static void usage_init(void); __dead static void usage_import(void); __dead static void usage_clone(void); @@ -171,16 +171,16 @@ static struct got_cmd got_commands[] = { }; static void -list_commands(void) +list_commands(FILE *fp) { int i; - fprintf(stderr, "commands:"); + fprintf(fp, "commands:"); for (i = 0; i < nitems(got_commands); i++) { struct got_cmd *cmd = &got_commands[i]; - fprintf(stderr, " %s", cmd->cmd_name); + fprintf(fp, " %s", cmd->cmd_name); } - fputc('\n', stderr); + fputc('\n', fp); } int @@ -206,7 +206,7 @@ main(int argc, char *argv[]) Vflag = 1; break; default: - usage(hflag); + usage(hflag, 1); /* NOTREACHED */ } } @@ -218,11 +218,11 @@ main(int argc, char *argv[]) if (Vflag) { got_version_print_str(); - return 1; + return 0; } if (argc <= 0) - usage(hflag); + usage(hflag, 0); signal(SIGINT, catch_sigint); signal(SIGPIPE, catch_sigpipe); @@ -254,18 +254,20 @@ main(int argc, char *argv[]) } fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]); - list_commands(); + list_commands(stderr); return 1; } __dead static void -usage(int hflag) +usage(int hflag, int status) { - fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n", + FILE *fp = (status == 0) ? stdout : stderr; + + fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n", getprogname()); if (hflag) - list_commands(); - exit(1); + list_commands(fp); + exit(status); } static const struct got_error * blob - 4d3c8ce6aa1ab5d45f4d36bede4d892708b96873 file + tog/tog.c --- tog/tog.c +++ tog/tog.c @@ -76,7 +76,7 @@ struct tog_cmd { void (*cmd_usage)(void); }; -__dead static void usage(int); +__dead static void usage(int, int); __dead static void usage_log(void); __dead static void usage_diff(void); __dead static void usage_blame(void); @@ -5480,28 +5480,30 @@ done: } static void -list_commands(void) +list_commands(FILE *fp) { int i; - fprintf(stderr, "commands:"); + fprintf(fp, "commands:"); for (i = 0; i < nitems(tog_commands); i++) { struct tog_cmd *cmd = &tog_commands[i]; - fprintf(stderr, " %s", cmd->name); + fprintf(fp, " %s", cmd->name); } - fputc('\n', stderr); + fputc('\n', fp); } __dead static void -usage(int hflag) +usage(int hflag, int status) { - fprintf(stderr, "usage: %s [-h] [-V | --version] [command] " - "[arg ...]\n", getprogname()); + FILE *fp = (status == 0) ? stdout : stderr; + + fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n", + getprogname()); if (hflag) { - fprintf(stderr, "lazy usage: %s path\n", getprogname()); - list_commands(); + fprintf(fp, "lazy usage: %s path\n", getprogname()); + list_commands(fp); } - exit(1); + exit(status); } static char ** @@ -5587,7 +5589,7 @@ tog_log_with_path(int argc, char *argv[]) goto done; fprintf(stderr, "%s: '%s' is no known command or path\n", getprogname(), argv[0]); - usage(1); + usage(1, 1); /* not reached */ } @@ -5645,7 +5647,7 @@ main(int argc, char *argv[]) Vflag = 1; break; default: - usage(hflag); + usage(hflag, 1); /* NOTREACHED */ } } @@ -5657,12 +5659,12 @@ main(int argc, char *argv[]) if (Vflag) { got_version_print_str(); - return 1; + return 0; } if (argc == 0) { if (hflag) - usage(hflag); + usage(hflag, 0); /* Build an argument vector which runs a default command. */ cmd = &tog_commands[0]; argc = 1; @@ -5682,7 +5684,7 @@ main(int argc, char *argv[]) if (cmd == NULL) { if (argc != 1) - usage(0); + usage(0, 1); /* No command specified; try log with a path */ error = tog_log_with_path(argc, argv); } else { -- Christian "naddy" Weisgerber naddy@mips.inka.de