parsing command option with default values and range constrains in C

Posted by agramfort on Stack Overflow See other posts from Stack Overflow or by agramfort
Published on 2010-12-26T19:44:51Z Indexed on 2010/12/26 19:53 UTC
Read the original article Hit count: 150

Filed under:
|
|

Hi,

I need to parse command line arguments in C. My arguments are basically int or float with default values and range constrains.

I've started to implement something that look like this:

option_float(float* out, int argc, char* argv, char* name, description,
    float default_val, int is_optional, float min_value, float max_value)

which I call for example with:

float* pct;
option_float(pct, argc, argv, "pct", "My super percentage option", 50, 1,
    FALSE, 0, 100)

however I don't want to reinvent the wheel !

My objective is to have error checking of range constrains, throw an error when the option is not optional and is not set. And generate the help message usually given by usage() function.

The usage text would look like this:

--pct     My super percentage option (default : 50). Should be in [0, 100]

I've started with getopt but it is too limited for what I want to do and I feel it still requires me to write too much code for a simple usecase like this.

thanks

© Stack Overflow or respective owner

Related posts about c

    Related posts about parsing