]> git.ozlabs.org Git - ccan/commitdiff
opt: add an int decrementing helper function
authorDouglas Bagnall <douglas@halo.gen.nz>
Fri, 20 Jun 2014 02:34:02 +0000 (14:34 +1200)
committerRusty Russell <rusty@rustcorp.com.au>
Mon, 23 Jun 2014 00:57:23 +0000 (10:27 +0930)
opt_dec_intval decrements an int value, just as opt_inc_intval
increments.

There is not much more to say, other than it allows this
kind of thing, with balanced opposing options:

    static int opt_verbosity = 0;
    static struct opt_table options[] = {
            OPT_WITHOUT_ARG("-q|--quiet", opt_dec_intval,
                            &opt_verbosity, "print less"),
            OPT_WITHOUT_ARG("-v|--verbose", opt_inc_intval,
                            &opt_verbosity, "print more"),
            OPT_ENDTABLE
    };

which is an occasionally seen idiom.  It allows, e.g., people who like
quiet to use `alias foo='foo -q'`, while letting them get back to
normal and verbose modes with various amounts of '-v's.

Signed-off-by: Douglas Bagnall <douglas@halo.gen.nz>
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ccan/opt/helpers.c
ccan/opt/opt.h

index e531a7d3aa2bc9d250e0608f21f0604d66e13733..c557f96d8488d01d3e1297ea235f6c2c8b76fb4f 100644 (file)
@@ -165,6 +165,12 @@ char *opt_inc_intval(int *i)
        return NULL;
 }
 
        return NULL;
 }
 
+char *opt_dec_intval(int *i)
+{
+       (*i)--;
+       return NULL;
+}
+
 /* Display version string. */
 char *opt_version_and_exit(const char *version)
 {
 /* Display version string. */
 char *opt_version_and_exit(const char *version)
 {
index f891002fa53890be40251b3e2557c4af3d2cd15e..727dacd0f148594a87c1b465c8180438b778a349 100644 (file)
@@ -450,8 +450,9 @@ void opt_show_ulonglongval_si(char buf[OPT_SHOW_LEN], const unsigned long long *
 
 
 
 
 
 
-/* Increment. */
+/* Increment and decrement. */
 char *opt_inc_intval(int *i);
 char *opt_inc_intval(int *i);
+char *opt_dec_intval(int *i);
 
 /* Display version string to stdout, exit(0). */
 char *opt_version_and_exit(const char *version);
 
 /* Display version string to stdout, exit(0). */
 char *opt_version_and_exit(const char *version);