[4.1] ff50855 Expose the varnishd optstring via -x

Dridi Boukelmoune dridi.boukelmoune at gmail.com
Tue Oct 31 09:53:05 UTC 2017


commit ff50855279e06808b09508348332bf18751312d1
Author: Dridi Boukelmoune <dridi.boukelmoune at gmail.com>
Date:   Mon Sep 25 10:53:08 2017 +0200

    Expose the varnishd optstring via -x
    
    This helps write scripts like this:
    
        optstring=$(varnishd -x optstring)
    
        while getopts $optstring opt
        do
            case $opt in
            n)
                # handle $OPTARG
                ;;
            # handle other options
            *)
                # ignore unneeded options
                ;;
            esac
        done
    
        varnishd "$@"
    
    Otherwise if optstring is not kept in sync, getopts will stop processing
    options if it encounters one that is not specified.

diff --git a/bin/varnishd/mgt/mgt_main.c b/bin/varnishd/mgt/mgt_main.c
index 611695f..368c6f4 100644
--- a/bin/varnishd/mgt/mgt_main.c
+++ b/bin/varnishd/mgt/mgt_main.c
@@ -74,6 +74,8 @@ struct VSC_C_mgt	*VSC_C_mgt;
 
 static struct vpf_fh *pfh = NULL;
 
+static const char opt_spec[] = "a:b:Cdf:Fh:i:j:l:M:n:P:p:r:S:s:T:t:VW:x:";
+
 /*--------------------------------------------------------------------*/
 
 static void
@@ -255,6 +257,7 @@ usage(void)
 	fprintf(stderr, FMT, "", "  -W epoll");
 #endif
 	fprintf(stderr, FMT, "", "  -W poll");
+	fprintf(stderr, FMT, "-x optstring", "List of getopt options");
 
 #undef FMT
 	exit(1);
@@ -466,7 +469,6 @@ init_params(struct cli *cli)
 	MCF_InitParams(cli);
 }
 
-
 /*--------------------------------------------------------------------*/
 
 static void
@@ -571,8 +573,7 @@ main(int argc, char * const *argv)
 	init_params(cli);
 	cli_check(cli);
 
-	while ((o = getopt(argc, argv,
-	    "a:b:Cdf:Fh:i:j:l:M:n:P:p:r:S:s:T:t:VW:x:")) != -1) {
+	while ((o = getopt(argc, argv, opt_spec)) != -1) {
 		/*
 		 * -j must be the first argument if specified, because
 		 * it (may) affect subsequent argument processing.
@@ -681,6 +682,10 @@ main(int argc, char * const *argv)
 				mgt_DumpRstVsl();
 				exit(0);
 			}
+			if (!strcmp(optarg, "optstring")) {
+				printf("%s\n", opt_spec);
+				exit(0);
+			}
 			usage();
 			break;
 		default:
diff --git a/doc/sphinx/reference/varnishd.rst b/doc/sphinx/reference/varnishd.rst
index 4506a63..f1aa09c 100644
--- a/doc/sphinx/reference/varnishd.rst
+++ b/doc/sphinx/reference/varnishd.rst
@@ -154,6 +154,11 @@ OPTIONS
 
   Specifies the waiter type to use.
 
+-x optstring
+
+  Print the optstring parameter to ``getopt(3)`` to help writing
+  wrapper scripts.
+
 .. _opt_h:
 
 Hash Algorithm


More information about the varnish-commit mailing list