Browse Source

Allow for long options using getopt_long

Marc Di Luzio 5 years ago
parent
commit
baff9c0363
2 changed files with 58 additions and 39 deletions
  1. 44 26
      daemon/main.c
  2. 14 13
      data/gamemoded.8.in

+ 44 - 26
daemon/main.c

@@ -56,6 +56,7 @@ POSSIBILITY OF SUCH DAMAGE.
 #include "gamemode_client.h"
 #include "logging.h"
 
+#include <getopt.h>
 #include <signal.h>
 #include <stdlib.h>
 #include <string.h>
@@ -64,12 +65,13 @@ POSSIBILITY OF SUCH DAMAGE.
 
 #define USAGE_TEXT                                                                                 \
 	"Usage: %s [-d] [-l] [-r] [-t] [-h] [-v]\n\n"                                                  \
-	"  -d  daemonize self after launch\n"                                                          \
-	"  -l  log to syslog\n"                                                                        \
-	"  -r  request gamemode and pause\n"                                                           \
-	"  -t  run tests\n"                                                                            \
-	"  -h  print this help\n"                                                                      \
-	"  -v  print version\n"                                                                        \
+	"  -r, --request            Request gamemode and pause\n"                                      \
+	"  -s, --status             Query the status of gamemode\n"                                    \
+	"  -d, --daemonize          Daemonize self after launch\n"                                     \
+	"  -l, --log-to-syslog      Log to syslog\n"                                                   \
+	"  -r, --test               Run tests\n"                                                       \
+	"  -h, --help               Print this help\n"                                                 \
+	"  -v, --version            Print version\n"                                                   \
 	"\n"                                                                                           \
 	"See man page for more information.\n"
 
@@ -102,8 +104,17 @@ int main(int argc, char *argv[])
 	bool daemon = false;
 	bool use_syslog = false;
 	int opt = 0;
-	int status;
-	while ((opt = getopt(argc, argv, "dlsrtvh")) != -1) {
+
+	/* Options struct for getopt_long */
+	static struct option long_options[] = {
+		{ "daemonize", no_argument, 0, 'd' }, { "log-to-syslog", no_argument, 0, 'l' },
+		{ "request", no_argument, 0, 'r' },   { "test", no_argument, 0, 't' },
+		{ "status", no_argument, 0, 's' },    { "help", no_argument, 0, 'h' },
+		{ "version", no_argument, 0, 'v' }
+	};
+	static const char *short_options = "dls::r::tvh";
+
+	while ((opt = getopt_long(argc, argv, short_options, long_options, 0)) != -1) {
 		switch (opt) {
 		case 'd':
 			daemon = true;
@@ -111,31 +122,41 @@ int main(int argc, char *argv[])
 		case 'l':
 			use_syslog = true;
 			break;
-		case 's': {
-			if ((status = gamemode_query_status()) < 0) {
+
+		case 's':
+
+			switch (gamemode_query_status()) {
+			case 0:
+				LOG_MSG("gamemode is inactive\n");
+				break;
+			case 1:
+				LOG_MSG("gamemode is active\n");
+				break;
+			case -1:
 				LOG_ERROR("gamemode status request failed: %s\n", gamemode_error_string());
 				exit(EXIT_FAILURE);
-			} else if (status > 0) {
-				LOG_MSG("gamemode is active\n");
-			} else {
-				LOG_MSG("gamemode is inactive\n");
+			default:
+				LOG_ERROR("gamemode_query_status returned unexpected value 2\n");
+				exit(EXIT_FAILURE);
 			}
 
 			exit(EXIT_SUCCESS);
-			break;
-		}
+
 		case 'r':
+
 			if (gamemode_request_start() < 0) {
 				LOG_ERROR("gamemode request failed: %s\n", gamemode_error_string());
 				exit(EXIT_FAILURE);
 			}
 
-			if ((status = gamemode_query_status()) == 2) {
+			switch (gamemode_query_status()) {
+			case 2:
 				LOG_MSG("gamemode request succeeded and is active\n");
-			} else if (status == 1) {
+				break;
+			case 1:
 				LOG_ERROR("gamemode request succeeded and is active but registration failed\n");
 				exit(EXIT_FAILURE);
-			} else {
+			case 0:
 				LOG_ERROR("gamemode request succeeded but is not active\n");
 				exit(EXIT_FAILURE);
 			}
@@ -153,23 +174,20 @@ int main(int argc, char *argv[])
 			}
 
 			exit(EXIT_SUCCESS);
-			break;
-		case 't':
-			status = game_mode_run_client_tests();
+
+		case 't': {
+			int status = game_mode_run_client_tests();
 			exit(status);
-			break;
+		}
 		case 'v':
 			LOG_MSG(VERSION_TEXT);
 			exit(EXIT_SUCCESS);
-			break;
 		case 'h':
 			LOG_MSG(USAGE_TEXT, argv[0]);
 			exit(EXIT_SUCCESS);
-			break;
 		default:
 			fprintf(stderr, USAGE_TEXT, argv[0]);
 			exit(EXIT_FAILURE);
-			break;
 		}
 	}
 

+ 14 - 13
data/gamemoded.8.in

@@ -4,7 +4,7 @@
 .SH NAME
 gamemoded \- optimises system performance on demand
 .SH SYNOPSIS
-\fBgamemoded\fR [\fB\-d\fR] [\fB\-l\fR] [\fB\-h\fR] [\fB\-v\fR]
+\fBgamemoded\fR [OPTIONS...]
 .SH DESCRIPTION
 \fBGameMode\fR is a daemon/lib combo for Linux that allows games to request a set of optimisations be temporarily applied to the host OS.
 
@@ -12,26 +12,27 @@ The design has a clear cut abstraction between the host daemon and library (\fBg
 
 \fBGameMode\fR was designed primarily as a stop-gap solution to problems with the Intel and AMD CPU powersave or ondemand governors, but is intended to be expanded beyond just CPU governor states, as there are a wealth of automation tasks one might want to apply.
 .SH OPTIONS
+
 .TP 8
-.B \-d
-Run the daemon as a separate process (daemonize it)
-.TP 8
-.B \-l
-Log to syslog
+.B \-r, \-\-request
+Request gamemode and pause
 .TP 8
-.B \-r
-Request gamemode and wait for any signal
+.B \-s, \-\-status
+Query the status of gamemode
 .TP 8
-.B \-s
-Query the current status of gamemode
+.B \-d, \-\-daemonize
+Run the daemon as a separate process (daemonize it)
 .TP 8
-.B \-h
+.B \-l, \-\-log-to-syslog
+Log to syslog
+.TP 8 
+.B \-h, \-\-help
 Print help text
 .TP 8
-.B \-t
+.B \-t, \-\-test
 Run diagnostic tests on the current installation
 .TP 8
-.B \-v
+.B \-v, \-\-version
 Print the version
 
 .SH USAGE