From 3ba33321de0790d60dc473acee093de5a3650480 Mon Sep 17 00:00:00 2001 From: Christoph Gysin Date: Sat, 28 Nov 2015 15:47:44 +0200 Subject: Use macros for exit values --- swaymsg/main.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'swaymsg/main.c') diff --git a/swaymsg/main.c b/swaymsg/main.c index 3a2e1ee7..e629bcc2 100644 --- a/swaymsg/main.c +++ b/swaymsg/main.c @@ -12,7 +12,7 @@ #include "log.h" void sway_terminate(void) { - exit(1); + exit(EXIT_FAILURE); } int main(int argc, char **argv) { @@ -52,7 +52,7 @@ int main(int argc, char **argv) { #else fprintf(stdout, "version not detected\n"); #endif - exit(0); + exit(EXIT_SUCCESS); break; } } -- cgit v1.2.3 From 0d55d1a0676704acdab86da231fe16d7e61c4ccf Mon Sep 17 00:00:00 2001 From: Christoph Gysin Date: Sat, 28 Nov 2015 16:09:19 +0200 Subject: swaymsg: Print usage and exit on unknown options --- swaymsg/main.c | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'swaymsg/main.c') diff --git a/swaymsg/main.c b/swaymsg/main.c index e629bcc2..2f2e843a 100644 --- a/swaymsg/main.c +++ b/swaymsg/main.c @@ -30,6 +30,14 @@ int main(int argc, char **argv) { {0, 0, 0, 0} }; + const char *usage = + "Usage: swaymsg [options] [message]\n" + "\n" + " -q, --quiet Be quiet.\n" + " -v, --version Show the version number and quit.\n" + " -s, --socket Use the specified socket.\n" + " -t, --type Specify the message type.\n"; + int c; while (1) { int option_index = 0; @@ -54,6 +62,9 @@ int main(int argc, char **argv) { #endif exit(EXIT_SUCCESS); break; + default: + fprintf(stderr, "%s", usage); + exit(EXIT_FAILURE); } } -- cgit v1.2.3 From bf97a5ada5ea4f8b45d15d00dc7f21487af8eadc Mon Sep 17 00:00:00 2001 From: Christoph Gysin Date: Sat, 28 Nov 2015 16:18:46 +0200 Subject: swaymsg: Add --help option that prints usage --- swaymsg/main.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'swaymsg/main.c') diff --git a/swaymsg/main.c b/swaymsg/main.c index 2f2e843a..8f91dc55 100644 --- a/swaymsg/main.c +++ b/swaymsg/main.c @@ -23,6 +23,7 @@ int main(int argc, char **argv) { init_log(L_INFO); static struct option long_options[] = { + {"help", no_argument, NULL, 'h'}, {"quiet", no_argument, &quiet, 'q'}, {"version", no_argument, NULL, 'v'}, {"socket", required_argument, NULL, 's'}, @@ -33,6 +34,7 @@ int main(int argc, char **argv) { const char *usage = "Usage: swaymsg [options] [message]\n" "\n" + " -h, --help Show help message and quit.\n" " -q, --quiet Be quiet.\n" " -v, --version Show the version number and quit.\n" " -s, --socket Use the specified socket.\n" @@ -41,7 +43,7 @@ int main(int argc, char **argv) { int c; while (1) { int option_index = 0; - c = getopt_long(argc, argv, "qvs:t:", long_options, &option_index); + c = getopt_long(argc, argv, "hqvs:t:", long_options, &option_index); if (c == -1) { break; } -- cgit v1.2.3 From 923c3245ace71ea0e26a0b12746a699fa499f759 Mon Sep 17 00:00:00 2001 From: Christoph Gysin Date: Sat, 28 Nov 2015 16:35:44 +0200 Subject: Fix option parsing Using 'flag' results in duplicate code paths for short and long options. This broke the -q short option in swaymsg, because there was: {"quiet", no_argument, &quiet, 'q'} Which will set quiet to 'q' and return 0, not 'q'. --- swaymsg/main.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'swaymsg/main.c') diff --git a/swaymsg/main.c b/swaymsg/main.c index 8f91dc55..f8c9e14c 100644 --- a/swaymsg/main.c +++ b/swaymsg/main.c @@ -24,7 +24,7 @@ int main(int argc, char **argv) { static struct option long_options[] = { {"help", no_argument, NULL, 'h'}, - {"quiet", no_argument, &quiet, 'q'}, + {"quiet", no_argument, NULL, 'q'}, {"version", no_argument, NULL, 'v'}, {"socket", required_argument, NULL, 's'}, {"type", required_argument, NULL, 't'}, @@ -48,7 +48,8 @@ int main(int argc, char **argv) { break; } switch (c) { - case 0: // Flag + case 'q': // Quiet + quiet = 1; break; case 's': // Socket socket_path = strdup(optarg); -- cgit v1.2.3