opt: allow show callbacks to return false. Sometimes, arguments are optional, so it's useful to decide at runtime whether to print a default in --help. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ccan: update show callback to take explicit length. For now we still use 80, but we could vary that in future. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ccan: allow user to set some bits in opt_table.type. In particular, Core Lightning wants to use this to flag arguments which can be specified multiple times (without overriding). Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ccan: break out routine to parse a single long argument. Core Lightning (ab)uses opt for config file parsing; this makes that quite a bit neater! Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
opt: add opt_unregister. Sometimes we all make mistakes. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
opt: add new parse_early_args_incomplete. If we have plugins, and those can register args, we have a problem finding the plugin dir! So, do a best-effort incomplete parse. Note that this can screw up in theory if we have "--unknown --foo" since we don't know if unknown takes an argument (in which case, ignore --foo) or not. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Mark unused arguments in many modules. Either with UNNEEDED (if the module already used ccan/compiler) or with (void) casting. The only other change is in ccan/list/test/run-CCAN_LIST_DEBUG.c, because the linenumbers change and thus it needs updating. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
opt: add opt_usage_exit_fail. I've been using opt_usage_and_exit() but that exits status 0. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
opt: add an int decrementing helper function 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>
opt: add float/double helpers. bfgminer/cgminer/sgminer want these. Con implemented some, but these are independently written (with tests!) Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
opt: relicense to GPLv2+ (from GPLv3+) Acked by contributors: Luke-Jr <luke@dashjr.org>: I don't have any objections Douglas Bagnall <douglas@halo.gen.nz>: Fine by me. Joel Stanley <joel@jms.id.au>: Fine with me. Brad Hards <bradh@frogmouth.net>: Ack. No response from Joey (trivial warning patch) or Andreas Schlick (one trivial patch, but he also wrote opt_free() (which was a 2 line function). Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
opt: don't wordwrap when description line starts with whitespace. This was suggested by Luke, though his version insisted on using tab. This one is a bit more complex, but allows either tab or space. Suggested-by: Luke Dashjr <luke-jr+git@utopios.org> Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
opt: add allocator setting. Good for tal usage. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
opt: add OPT_EARLY and opt_early_parse. Parsing options like --verbose and --debug can be a pain. You need to have everything set up before invoking parse_args(), but that may be a significant amount of work, for which you may want verbose or debugging enabled. Thus the concept of "early" args: you can nominate arguments to be parse before anything else, using opt_early_parse().
opt: fix up outdated comments in documentation.
opt: complete coverage, enhance opt_free_table. No point checking malloc failure in usage(), since we don't elsewhere. We get 100% coverage with -O (due to code elimination) or 64 bit.
opt: functions to show integer values with kMGTPE suffixes As with the set_ functions, there are twelve permutations of integer size, base, and signedness. The supported sizes are int, long, and long long. For example, this: char buf1[OPT_SHOW_LEN]; char buf2[OPT_SHOW_LEN]; unsigned i = 1024000; opt_show_uintval_bi(buf1, &i); opt_show_uintval_si(buf2, &i); will put "1000k" in buf1, and "1024k" in buf2. Unlike the opt_set_ functions, these use unsigned arithmetic for unsigned values. (32 bit bug using sizeof(suffixes) instead of strlen(suffixes) fixed by Rusty)
opt: add integer helpers that accept k, M, G, T, P, E suffixes These functions come in two flavours: those ending with "_si", which have 1000-based interpretations of the suffixes; and those ending with "_bi", which use base 1024. There are versions for signed and unsigned int, long, and long long destinations, with tests for all 12 new functions. The tests get a bit repetitive, I am afraid. As an example, if the -x option were using the opt_set_intval_bi function, then all of these would do the same thing: $ foo -x 5M $ foo -x $((5 * 1024 * 1024)) $ foo -x 5242880 $ foo -x 5120k quite what that thing is depends on the size of your int -- people with 16 bit ints would see an "out of range" error message. The arithmetic for unsigned variations is actually done using signed long long integers, so the maximum possible value is LLONG_MAX, not ULLONG_MAX. This follows the practice of existing functions, and avoids tedious work.
various: add LICENSE comments.
typesafe_cb: simplify, preserve namespace. Get rid of many variants, which were just confusing for most people. Keep typesafe_cb(), typesafe_cb_preargs() and typesafe_cb_postarts(), and rework cast_if_type() into typesafe_cb_cast() so we stay in our namespace. I should have done this as soon as I discovered the limitation that the types have to be defined if I want const-taking callbacks.