From 317d6f5a2fd8f480eae4597dfb7f291b0ffd9e87 Mon Sep 17 00:00:00 2001
From: Marek Vavrusa <marek.vavrusa@nic.cz>
Date: Wed, 2 Nov 2011 11:45:07 +0100
Subject: [PATCH] Added long options.

---
 src/knot/ctl/knotc_main.c | 40 ++++++++++++++++++++++++++-------------
 src/knot/main.c           | 31 +++++++++++++++++++++---------
 2 files changed, 49 insertions(+), 22 deletions(-)

diff --git a/src/knot/ctl/knotc_main.c b/src/knot/ctl/knotc_main.c
index 48cc0dc7a1..d3b68c026b 100644
--- a/src/knot/ctl/knotc_main.c
+++ b/src/knot/ctl/knotc_main.c
@@ -23,6 +23,7 @@
 #include <time.h>
 #include <sys/select.h>
 #include <sys/stat.h>
+#include <getopt.h>
 
 #include "knot/common.h"
 #include "knot/other/error.h"
@@ -39,18 +40,17 @@ enum knotc_constants_t {
 /*! \brief Print help. */
 void help(int argc, char **argv)
 {
-	printf("Usage: %s [parameters] start|stop|restart|reload|running|"
-	       "compile\n",
-	       argv[0]);
+	printf("Usage: %sc [parameters] start|stop|restart|reload|running|"
+	       "compile\n", PACKAGE_NAME);
 	printf("Parameters:\n"
-	       " -c [file]\tSelect configuration file.\n"
-	       " -f\t\tForce operation - override some checks.\n"
-	       " -v\t\tVerbose mode - additional runtime information.\n"
-	       " -V\t\tPrint %s server version.\n"
-	       " -w\t\tWait for the server to finish start/stop operations.\n"
-	       " -i\t\tInteractive mode (do not daemonize).\n"
-	       " -j [num]\tNumber of parallel tasks to run (only for 'compile').\n"
-	       " -h\t\tPrint help and usage.\n",
+	       " -c [file], --config=[file] Select configuration file.\n"
+	       " -j [num], --jobs=[num]     Number of parallel tasks to run (only for 'compile').\n"
+	       " -f, --force                Force operation - override some checks.\n"
+	       " -v, --verbose              Verbose mode - additional runtime information.\n"
+	       " -V, --version              Print %s server version.\n"
+	       " -w, --wait                 Wait for the server to finish start/stop operations.\n"
+	       " -i, --interactive          Interactive mode (do not daemonize).\n"
+	       " -h, --help                 Print help and usage.\n",
 	       PACKAGE_NAME);
 	printf("Actions:\n"
 	       " start     Start %s server zone (no-op if running).\n"
@@ -537,14 +537,28 @@ int execute(const char *action, char **argv, int argc, pid_t pid, int verbose,
 int main(int argc, char **argv)
 {
 	// Parse command line arguments
-	int c = 0;
+	int c = 0, li = 0;
 	int force = 0;
 	int verbose = 0;
 	int wait = 0;
 	int interactive = 0;
 	int jobs = 1;
 	const char* config_fn = 0;
-	while ((c = getopt (argc, argv, "wfc:vij:Vh")) != -1) {
+	
+	/* Long options. */
+	struct option opts[] = {
+		{"wait",        no_argument,       0, 'w'},
+		{"force",       no_argument,       0, 'f'},
+		{"config",      required_argument, 0, 'c'},
+		{"verbose",     no_argument,       0, 'v'},
+		{"interactive", no_argument,       0, 'i'},
+		{"jobs",        required_argument, 0, 'c'},
+		{"version",     no_argument,       0, 'V'},
+		{"help",        no_argument,       0, 'h'},
+		{0, 0, 0, 0}
+	};
+	
+	while ((c = getopt_long(argc, argv, "wfc:vij:Vh", opts, &li)) != -1) {
 		switch (c)
 		{
 		case 'w':
diff --git a/src/knot/main.c b/src/knot/main.c
index 28d06f7553..610cb7674a 100644
--- a/src/knot/main.c
+++ b/src/knot/main.c
@@ -18,6 +18,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
+#include <getopt.h>
+#include "common.h"
 
 #include "knot/common.h"
 #include "knot/other/error.h"
@@ -59,24 +61,35 @@ void interrupt_handle(int s)
 
 void help(int argc, char **argv)
 {
-	printf("Usage: %s [parameters]\n",
-	       argv[0]);
+	printf("Usage: %sd [parameters]\n",
+	       PACKAGE_NAME);
 	printf("Parameters:\n"
-	       " -c [file] Select configuration file.\n"
-	       " -d        Run server as a daemon.\n"
-	       " -v        Verbose mode - additional runtime information.\n"
-	       " -V        Print version of the server.\n"
-	       " -h        Print help and usage.\n");
+	       " -c, --config [file] Select configuration file.\n"
+	       " -d, --daemonize     Run server as a daemon.\n"
+	       " -v, --verbose       Verbose mode - additional runtime information.\n"
+	       " -V, --version       Print version of the server.\n"
+	       " -h, --help          Print help and usage.\n");
 }
 
 int main(int argc, char **argv)
 {
 	// Parse command line arguments
-	int c = 0;
+	int c = 0, li = 0;
 	int verbose = 0;
 	int daemonize = 0;
 	char* config_fn = 0;
-	while ((c = getopt (argc, argv, "c:dvVh")) != -1) {
+	
+	/* Long options. */
+	struct option opts[] = {
+		{"config",    required_argument, 0, 'c'},
+		{"daemonize", no_argument,       0, 'd'},
+		{"verbose",   no_argument,       0, 'v'},
+		{"version",   no_argument,       0, 'V'},
+		{"help",      no_argument,       0, 'h'},
+		{0, 0, 0, 0}
+	};
+	
+	while ((c = getopt_long(argc, argv, "c:dvVh", opts, &li)) != -1) {
 		switch (c)
 		{
 		case 'c':
-- 
GitLab