[PATCH iproute2-next 4/7] netshaper: put help to stdout and usage to stderr

Stephen Hemminger <[email protected]>
Newsgroups org.kernel.vger.netdev
Message-ID <[email protected]>
Similar change to other iproute2 commands.
Syntax error should print to stderr and exit with non-zero.
Help command should print to stdout and exit with zero status.

Signed-off-by: Stephen Hemminger <[email protected]>
---
 netshaper/netshaper.c | 31 ++++++++++++++++---------------
 1 file changed, 16 insertions(+), 15 deletions(-)

diff --git a/netshaper/netshaper.c b/netshaper/netshaper.c
index 3b47d43d..85999dd5 100644
--- a/netshaper/netshaper.c
+++ b/netshaper/netshaper.c
@@ -26,9 +26,11 @@
 static struct rtnl_handle gen_rth = { .fd = -1 };
 static int genl_family = -1;
 
-static void usage(void)
+static void usage(bool help) __attribute__((noreturn));
+
+static void usage(bool help)
 {
-	fprintf(stderr,
+	fprintf(help ? stdout : stderr,
 		"Usage: netshaper [ OPTIONS ] { COMMAND | help }\n"
 		"OPTIONS := { -V[ersion] | -c[olor] | -help }\n"
 		"COMMAND := { set | get | delete | group } dev DEVNAME\n"
@@ -46,6 +48,8 @@ static void usage(void)
 		"       HANDLE_ID       := UINT (required for queue/node, optional for netdev)\n"
 		"       BW_MIN/BW_MAX   := UINT{ kbit | mbit | gbit }\n"
 		"       WEIGHT          := UINT\n");
+
+	exit(help ? EXIT_SUCCESS : EXIT_FAILURE);
 }
 
 static const char *net_shaper_scope_names[NET_SHAPER_SCOPE_MAX + 1] = {
@@ -243,7 +247,7 @@ static int do_cmd(int argc, char **argv, int cmd)
 
 			if (strcmp(*argv, "scope") != 0) {
 				fprintf(stderr, "What is \"%s\"\n", *argv);
-				usage();
+				usage(false);
 				return -1;
 			}
 			NEXT_ARG();
@@ -270,7 +274,7 @@ static int do_cmd(int argc, char **argv, int cmd)
 				NEXT_ARG();
 				if (strcmp(*argv, "id") != 0) {
 					fprintf(stderr, "What is \"%s\"\n", *argv);
-					usage();
+					usage(false);
 					return -1;
 				}
 				NEXT_ARG();
@@ -282,7 +286,7 @@ static int do_cmd(int argc, char **argv, int cmd)
 			}
 		} else {
 			fprintf(stderr, "What is \"%s\"\n", *argv);
-			usage();
+			usage(false);
 			return -1;
 		}
 		argc--;
@@ -363,7 +367,7 @@ static int parse_scope_id(const char *what, int *argcp, char ***argvp, int *scop
 		NEXT_ARG();
 		if (strcmp(*argv, "id") != 0) {
 			fprintf(stderr, "What is \"%s\"\n", *argv);
-			usage();
+			usage(false);
 			return -1;
 		}
 		NEXT_ARG();
@@ -517,7 +521,7 @@ static int do_group(int argc, char **argv)
 			continue;
 		} else {
 			fprintf(stderr, "What is \"%s\"\n", *argv);
-			usage();
+			usage(false);
 			goto free_leaves;
 		}
 		argc--;
@@ -609,8 +613,7 @@ int main(int argc, char **argv)
 			opt++;
 
 		if (strcmp(opt, "-help") == 0) {
-			usage();
-			exit(0);
+			usage(true);
 		} else if (strcmp(opt, "-Version") == 0 ||
 			   strcmp(opt, "-V") == 0) {
 			printf("netshaper utility, %s\n", version);
@@ -627,6 +630,9 @@ int main(int argc, char **argv)
 
 	check_enable_color(color, 0);
 
+	if (argc > 1 && strcmp(argv[1], "help") == 0)
+		usage(true);
+
 	if (genl_init_handle(&gen_rth, NET_SHAPER_FAMILY_NAME, &genl_family))
 		exit(1);
 
@@ -642,15 +648,10 @@ int main(int argc, char **argv)
 			return do_cmd(argc - 1, argv + 1, NET_SHAPER_CMD_GET);
 		if (strcmp(*argv, "group") == 0)
 			return do_group(argc - 1, argv + 1);
-		if (strcmp(*argv, "help") == 0) {
-			usage();
-			return 0;
-		}
 		fprintf(stderr,
 			"Command \"%s\" is unknown, try \"netshaper help\".\n",
 			*argv);
 		exit(-1);
 	}
-	usage();
-	exit(-1);
+	usage(false);
 }
-- 
2.53.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.