[master] fd21faf Handle synopsis and options via the vut code

Federico G. Schwindt fgsch at lodoss.net
Sat Jun 4 18:19:05 CEST 2016


commit fd21faff891451b8a1699b2bdb5c1f11977d2df2
Author: Federico G. Schwindt <fgsch at lodoss.net>
Date:   Sat Jun 4 16:26:26 2016 +0100

    Handle synopsis and options via the vut code

diff --git a/bin/varnishstat/Makefile.am b/bin/varnishstat/Makefile.am
index 5c0a179..82303fa 100644
--- a/bin/varnishstat/Makefile.am
+++ b/bin/varnishstat/Makefile.am
@@ -8,9 +8,10 @@ bin_PROGRAMS = varnishstat
 
 varnishstat_SOURCES = \
 	varnishstat.h \
-	\
 	varnishstat.c \
-	varnishstat_curses.c
+	varnishstat_curses.c \
+	varnishstat_options.h \
+	varnishstat_options.c
 
 varnishstat_CFLAGS = \
 	@SAN_CFLAGS@
diff --git a/bin/varnishstat/varnishstat.c b/bin/varnishstat/varnishstat.c
index c7746d6..bf7cc5b 100644
--- a/bin/varnishstat/varnishstat.c
+++ b/bin/varnishstat/varnishstat.c
@@ -41,11 +41,16 @@
 #include <math.h>
 #include <stdint.h>
 
+#include "vapi/voptget.h"
+#include "vapi/vsl.h"
+#include "vdef.h"
 #include "vnum.h"
 #include "vtim.h"
+#include "vut.h"
 
 #include "varnishstat.h"
 
+static const char progname[] = "varnishstat";
 
 /*--------------------------------------------------------------------*/
 
@@ -247,50 +252,38 @@ list_fields(struct VSM_data *vd)
 /*--------------------------------------------------------------------*/
 
 static void
-usage(void)
+usage(int status)
 {
-#define FMT "    %-28s # %s\n"
-	fprintf(stderr, "usage: varnishstat "
-	    "[-1lV] [-f field] [-t seconds|<off>] "
-	    VSC_n_USAGE "\n");
-	fprintf(stderr, FMT, "-1", "Print the statistics to stdout.");
-	fprintf(stderr, FMT, "-f field", "Field inclusion glob");
-	fprintf(stderr, FMT, "",
-	    "If it starts with '^' it is used as an exclusion list.");
-	fprintf(stderr, FMT, "-l",
-	    "Lists the available fields to use with the -f option.");
-	fprintf(stderr, FMT, "-n varnish_name",
-	    "The varnishd instance to get logs from.");
-	fprintf(stderr, FMT, "-N filename",
-	    "Filename of a stale VSM instance.");
-	fprintf(stderr, FMT, "-t seconds|<off>",
-	    "Timeout before returning error on initial VSM connection.");
-	fprintf(stderr, FMT, "-V", "Display the version number and exit.");
-	fprintf(stderr, FMT, "-x",
-	    "Print statistics to stdout as XML.");
-	fprintf(stderr, FMT, "-j",
-	    "Print statistics to stdout as JSON.");
-#undef FMT
-	exit(1);
+	const char **opt;
+
+	fprintf(stderr, "Usage: %s <options>\n\n", progname);
+	fprintf(stderr, "Options:\n");
+	for (opt = vopt_spec.vopt_usage; *opt != NULL; opt +=2)
+		fprintf(stderr, " %-25s %s\n", *opt, *(opt + 1));
+	exit(status);
 }
 
 int
 main(int argc, char * const *argv)
 {
-	int c;
 	struct VSM_data *vd;
 	double t_arg = 5.0, t_start = NAN;
 	int once = 0, xml = 0, json = 0, f_list = 0, curses = 0;
+	signed char opt;
 	int i;
 
+	VUT_Init(progname, argc, argv, &vopt_spec);
 	vd = VSM_New();
 	AN(vd);
 
-	while ((c = getopt(argc, argv, VSC_ARGS "1f:lVxjt:")) != -1) {
-		switch (c) {
+	while ((opt = getopt(argc, argv, vopt_spec.vopt_optstring)) != -1) {
+		switch (opt) {
 		case '1':
 			once = 1;
 			break;
+		case 'h':
+			/* Usage help */
+			usage(0);
 		case 'l':
 			f_list = 1;
 			break;
@@ -319,10 +312,10 @@ main(int argc, char * const *argv)
 			json = 1;
 			break;
 		default:
-			if (VSC_Arg(vd, c, optarg) > 0)
+			if (VSC_Arg(vd, opt, optarg) > 0)
 				break;
 			fprintf(stderr, "%s\n", VSM_Error(vd));
-			usage();
+			usage(1);
 		}
 	}
 
diff --git a/bin/varnishstat/varnishstat_options.c b/bin/varnishstat/varnishstat_options.c
new file mode 100644
index 0000000..60b8d28
--- /dev/null
+++ b/bin/varnishstat/varnishstat_options.c
@@ -0,0 +1,34 @@
+/*-
+ * Copyright (c) 2016 Varnish Software AS
+ * All rights reserved.
+ *
+ * Author: Federico G. Schwindt <fgsch at lodoss.net>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * Option definitions for varnishstat
+ */
+
+#include <stdlib.h>
+#define VOPT_DEFINITION
+#define VOPT_INC "varnishstat_options.h"
+#include "vapi/voptget.h"
diff --git a/bin/varnishstat/varnishstat_options.h b/bin/varnishstat/varnishstat_options.h
new file mode 100644
index 0000000..10d4c7e
--- /dev/null
+++ b/bin/varnishstat/varnishstat_options.h
@@ -0,0 +1,73 @@
+/*-
+ * Copyright (c) 2016 Varnish Software AS
+ * All rights reserved.
+ *
+ * Author: Federico G. Schwindt <fgsch at lodoss.net>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * Option definitions for varnishstat
+ */
+
+#include "vapi/vapi_options.h"
+#include "vut_options.h"
+
+#define STAT_OPT_1							\
+	VOPT("1", "[-1]", "Print the statistics to stdout",		\
+	    "Instead of presenting a continuously updated display,"	\
+	    " print the statistics to stdout."				\
+	)
+#define STAT_OPT_f							\
+	VOPT("f:", "[-f field]", "Field inclusion glob",		\
+	    "Field inclusion glob."					\
+	    " A field glob consists of three parts, type, ident and"	\
+	    " name, where ident is optional. Each part can contain a"	\
+	    " '*' character at the end to match a prefix. Use"		\
+	    " backslash to escape characters. If the argument starts"	\
+	    " with '^' it is used as an exclusion glob. Multiple -f"	\
+	    " arguments may be given, and they will be applied in"	\
+	    " order."							\
+	)
+#define STAT_OPT_j							\
+	VOPT("j", "[-j]", "Print statistics to stdout as JSON",		\
+	    "Print statistics to stdout as JSON."			\
+	)
+#define STAT_OPT_l							\
+	VOPT("l", "[-l]",						\
+	    "Lists the available fields to use with the -f option",	\
+	    "Lists the available fields to use with the -f option."	\
+	)
+#define STAT_OPT_x							\
+	VOPT("x", "[-x]", "Print statistics to stdout as XML",		\
+	    "Print statistics to stdout as XML."			\
+	)
+
+STAT_OPT_1
+STAT_OPT_f
+VUT_OPT_h
+STAT_OPT_j
+STAT_OPT_l
+VUT_OPT_n
+VUT_OPT_N
+VUT_OPT_t
+VUT_OPT_V
+STAT_OPT_x
diff --git a/doc/sphinx/Makefile.am b/doc/sphinx/Makefile.am
index 6baf438..7a55e4b 100644
--- a/doc/sphinx/Makefile.am
+++ b/doc/sphinx/Makefile.am
@@ -164,6 +164,13 @@ include/varnishhist_synopsis.rst: $(top_builddir)/bin/varnishhist/varnishhist
 BUILT_SOURCES += include/varnishhist_options.rst \
 	 include/varnishhist_synopsis.rst
 
+include/varnishstat_options.rst: $(top_builddir)/bin/varnishstat/varnishstat
+	$(top_builddir)/bin/varnishstat/varnishstat --options > $@
+include/varnishstat_synopsis.rst: $(top_builddir)/bin/varnishstat/varnishstat
+	$(top_builddir)/bin/varnishstat/varnishstat --synopsis > $@
+BUILT_SOURCES += include/varnishstat_options.rst \
+	 include/varnishstat_synopsis.rst
+
 include/vsl-tags.rst: $(top_builddir)/lib/libvarnishapi/vsl2rst
 	$(top_builddir)/lib/libvarnishapi/vsl2rst > $@
 BUILT_SOURCES += include/vsl-tags.rst
diff --git a/doc/sphinx/reference/varnishstat.rst b/doc/sphinx/reference/varnishstat.rst
index a0619ae..5901a06 100644
--- a/doc/sphinx/reference/varnishstat.rst
+++ b/doc/sphinx/reference/varnishstat.rst
@@ -15,60 +15,17 @@ Varnish Cache statistics
 SYNOPSIS
 ========
 
-varnishstat [-1] [-x] [-j] [-f field] [-l] [-n varnish_name] [-N filename] [-t seconds|<off>] [-V]
-
-.. TODO: autogenerate this synopsis like the others.
+.. include:: ../include/varnishstat_synopsis.rst
+varnishstat |synopsis|
 
 DESCRIPTION
 ===========
 
 The varnishstat utility displays statistics from a running varnishd(1) instance.
 
-OPTIONS
-=======
-
 The following options are available:
 
--1
-	Instead of presenting a continuously updated display, print
-	the statistics to stdout.
-
--f
-	Field inclusion glob. A field glob consists of three parts,
-        type, ident and name, where ident is optional. Each part can
-        contain a '*' character at the end to match a prefix. Use
-        backslash to escape characters. If the argument starts with
-        '^' it is used as an exclusion glob. Multiple -f arguments may
-        be given, and they will be applied in order.
-
--l
-	Lists the available fields to use with the -f option.
-
--n
-	Specifies the name of the varnishd instance to get logs from.
-	If -n is not specified, the host name is used.
-
--N
-	Specify a the filename of a stale VSM instance. When using
-        this option the abandonment checking is disabled.
-
--t seconds|<off>
-	Timeout before returning error on initial VSM connection.  If
-	set the VSM connection is retried every 0.5 seconds for this
-	any seconds. If zero the connection is attempted only once and
-	will fail immediately if unsuccessful. If set to "off", the
-	connection will not fail, allowing the utility to start and
-	wait indefinitely for the Varnish instance to appear.
-	Defaults to 5 seconds.
-
--V
-	Display the version number and exit.
-
--x
-	Displays the result as XML.
-
--j
-	Displays the result as JSON.
+.. include:: ../include/varnishstat_options.rst
 
 CURSES MODE
 ===========
diff --git a/man/Makefile.am b/man/Makefile.am
index 05a1618..adb76c2 100644
--- a/man/Makefile.am
+++ b/man/Makefile.am
@@ -58,8 +58,9 @@ varnishlog.1: \
 	$(top_builddir)/doc/sphinx/include/varnishlog_synopsis.rst
 	${RST2MAN} $(RST2ANY_FLAGS) $(top_srcdir)/doc/sphinx/reference/varnishlog.rst $@
 
-# XXX add _options.rst and _synopsis.rst here when it's been _opt2rst'ed
-varnishstat.1: $(top_srcdir)/doc/sphinx/reference/varnishstat.rst
+varnishstat.1: $(top_srcdir)/doc/sphinx/reference/varnishstat.rst \
+	$(top_builddir)/doc/sphinx/include/varnishstat_options.rst \
+	$(top_builddir)/doc/sphinx/include/varnishstat_synopsis.rst
 	${RST2MAN} $(RST2ANY_FLAGS) $(top_srcdir)/doc/sphinx/reference/varnishstat.rst $@
 
 varnishtest.1: $(top_srcdir)/doc/sphinx/reference/varnishtest.rst



More information about the varnish-commit mailing list