[master] e097065 Expose the varnishd optstring via -x

Dridi Boukelmoune dridi.boukelmoune at gmail.com
Mon Oct 30 16:10:09 UTC 2017


commit e0970652f9226a5a49887c086812313bd8c72934
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 aa1bb74..b90e76d 100644
--- a/bin/varnishd/mgt/mgt_main.c
+++ b/bin/varnishd/mgt/mgt_main.c
@@ -74,6 +74,8 @@ static struct vpf_fh *pfh2 = NULL;
 static struct vfil_path *vcl_path = NULL;
 static VTAILQ_HEAD(,f_arg) f_args = VTAILQ_HEAD_INITIALIZER(f_args);
 
+static const char opt_spec[] = "a:b:Cdf:Fh:i:I:j:l:M:n:P:p:r:S:s:T:t:VW:x:";
+
 int optreset;	// Some has it, some doesn't.  Cheaper than auto*
 
 /*--------------------------------------------------------------------*/
@@ -106,6 +108,7 @@ usage(void)
 	printf(FMT, "-x vsl", "VSL record documentation");
 	printf(FMT, "-x cli", "CLI command documentation");
 	printf(FMT, "-x builtin", "Builtin VCL program");
+	printf(FMT, "-x optstring", "List of getopt options");
 
 	printf("\nOperations options:\n");
 
@@ -297,6 +300,8 @@ mgt_x_arg(const char *x_arg)
 		mgt_DumpRstCli();
 	else if (!strcmp(x_arg, "builtin"))
 		mgt_DumpBuiltin();
+	else if (!strcmp(x_arg, "optstring"))
+		(void)printf("%s\n", opt_spec);
 	else
 		ARGV_ERR("Invalid -x argument\n");
 }
@@ -423,8 +428,6 @@ mgt_f_read(const char *fn)
 	VTAILQ_INSERT_TAIL(&f_args, fa, list);
 }
 
-static const char opt_spec[] = "a:b:Cdf:Fh:i:I:j:l:M:n:P:p:r:S:s:T:t:VW:x:";
-
 int
 main(int argc, char * const *argv)
 {
diff --git a/doc/sphinx/reference/varnishd.rst b/doc/sphinx/reference/varnishd.rst
index 7c31eea..90ab324 100644
--- a/doc/sphinx/reference/varnishd.rst
+++ b/doc/sphinx/reference/varnishd.rst
@@ -17,7 +17,7 @@ SYNOPSIS
 
 varnishd [-a [name=][address][:port][,PROTO]] [-b host[:port]] [-C] [-d] [-F] [-f config] [-h type[,options]] [-I clifile] [-i identity] [-j jail[,jailoptions]] [-l vsl[,vsm]] [-M address:port] [-n name] [-P file] [-p param=value] [-r param[,param...]] [-S secret-file] [-s [name=]kind[,options]] [-T address[:port]] [-t TTL] [-V] [-W waiter]
 
-varnishd [-x parameter|vsl|cli|builtin]
+varnishd [-x parameter|vsl|cli|builtin|optstring]
 
 varnishd [-?]
 
@@ -123,6 +123,11 @@ outputs documentation in reStructuredText, aka RST).
 
   Print the contents of the default VCL program ``builtin.vcl``.
 
+-x optstring
+
+  Print the optstring parameter to ``getopt(3)`` to help writing
+  wrapper scripts.
+
 Operations options
 ------------------
 


More information about the varnish-commit mailing list