[ccan] [PATCH] make ccan/opt accept k, M suffixes for long-destined options

Rusty Russell rusty at rustcorp.com.au
Thu Apr 28 14:16:32 EST 2011


On Thu, 28 Apr 2011 14:31:54 +1200, Douglas Bagnall <douglas at paradise.net.nz> wrote:
> You perhaps don't want this one in, at least in its current state.
> 
> It makes ccan/opt accept 'k' and 'M' suffixes for arguments destined
> to be stored in long or unsigned long integers.  That is, these all do
> the same thing:
> 
> $ foo -x 5M
> $ foo -x $((5 * 1024 * 1024))
> $ foo -x 5242880
> $ foo -x 5120k
> 
> Some reasons not to want it are that it assumes 'k' means 1024, when
> 1000 might be more useful in general; and that it comes with
> absolutely no tests (or documentation).
> 
> OTOH, it is really useful if you are dealing with big numbers on the
> command line and hate counting digits.
> 
> I can do tests and documentation if you like the idea.

It should be a separate helper, IMHO (BTW you realize there's nothing
special about the built-in ones, and you can write your own for your
project?).

Anyway, I'd call this opt_set_longval_bi (potentially with a _suffix_si
version later if people want 10-based versions).

Anyway, I looked at POSIX for guidance on what postfixes should be
supported, and it really only specifies 'k' (1024).  Looking at the dd
source, it supports 'k' and 'K', then MGTPE (and mgt as undocumented
compatibility options).

So, I'd say you'd want to do those, with an overflow check of course.
And please implement opt_get_longval_bi (which should choose the minimum
suffix which allows an exact specification).

What fun!

Thanks!
Rusty.


More information about the ccan mailing list