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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
diff --git a/got/got.c b/got/got.c
index e2b4927..7b77c62 100644
--- a/got/got.c
+++ b/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 *
diff --git a/tog/tog.c b/tog/tog.c
index 4d3c8ce..a99744c 100644
--- a/tog/tog.c
+++ b/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 {