[ccan] [PATCH 2/2] opt: add an int decrementing helper function

Douglas Bagnall douglas at halo.gen.nz
Fri Jun 20 12:34:02 EST 2014


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 at halo.gen.nz>
---
 ccan/opt/helpers.c | 6 ++++++
 ccan/opt/opt.h     | 3 ++-
 2 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/ccan/opt/helpers.c b/ccan/opt/helpers.c
index e531a7d..c557f96 100644
--- a/ccan/opt/helpers.c
+++ b/ccan/opt/helpers.c
@@ -165,6 +165,12 @@ char *opt_inc_intval(int *i)
 	return NULL;
 }
 
+char *opt_dec_intval(int *i)
+{
+	(*i)--;
+	return NULL;
+}
+
 /* Display version string. */
 char *opt_version_and_exit(const char *version)
 {
diff --git a/ccan/opt/opt.h b/ccan/opt/opt.h
index f891002..727dacd 100644
--- a/ccan/opt/opt.h
+++ b/ccan/opt/opt.h
@@ -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_dec_intval(int *i);
 
 /* Display version string to stdout, exit(0). */
 char *opt_version_and_exit(const char *version);
-- 
1.8.3.2



More information about the ccan mailing list