commit 6879ba4225a833ea466b3512329293abfd9bf33a from: Christian Weisgerber date: Thu Oct 01 15:15:22 2020 UTC 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. tweak and ok stsp commit - 0f84e4996c5a10d8ffca30998a7c99883e142e9e commit + 6879ba4225a833ea466b3512329293abfd9bf33a blob - e2b4927b8bb743bb45718b5c21b186f11b4f918a blob + 7b77c62ca03d78d5a88d8053e2281321de9c2b84 --- 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, hflag ? 0 : 1); 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 blob + a99744c546674ba7c12f31a138da72c698c6902d --- 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()); - if (hflag) { - fprintf(stderr, "lazy usage: %s path\n", getprogname()); - list_commands(); + FILE *fp = (status == 0) ? stdout : stderr; + + fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n", + getprogname()); + if (hflag) { + 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 {