Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for default int/float/string values? #58

Open
sdaau opened this issue Apr 30, 2022 · 1 comment
Open

Support for default int/float/string values? #58

sdaau opened this issue Apr 30, 2022 · 1 comment

Comments

@sdaau
Copy link

sdaau commented Apr 30, 2022

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
Copy link
Owner

cofyc commented May 4, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants