cofyc/argparse

Support for default int/float/string values?

sdaau opened this issue · 1 comments

sdaau commented

Thanks for a great library!

I was just wondering, how could I implement default values easily?

That is, if --myoption argument is not specified on the command line, then, say, the corresponding float myoption in C code gets the value (for example) 5.0 ...

I have seen in the code https://github.com/cofyc/argparse/blob/master/argparse.h, the last three arguments to the macros are:

 *  `callback`:
 *    function is called when corresponding argument is parsed.
 *
 *  `data`:
 *    associated data. Callbacks can use it like they want.
 *
 *  `flags`:
 *    option flags.

So, I guess, I'd have to implement a custom callback for all such options, and then assign data in the callback, in case the command line argument was not found on the command line. But then, I'm not sure how could I check in the callback, whether the command line argument was present or not?

Is there an example that shows such usage (with default values for arguments, if they are not set on the command line)?

cofyc commented

argparse/tests/basic.c

Lines 19 to 22 in c612dc0

int force = 0;
int test = 0;
int int_num = 0;
float flt_num = 0.f;

You can initialize variables with default values. If no arguments provided, the default values will not be overridden.