Snippet/lib that handles command line arguments parsing/validation

Go To StackoverFlow.com

0

I have to write some command line utility that should be able to take bunch of arguments of different types and with dependences between them.

I am lazy enough to reinvent the wheel and assume that there is some already made snippets/mini libs that can handle this type of task.

It would be great to have api where I could describe validation rules, defaults, etc, then feed the raw argc/argv to it and get map of parsed values.

What I need:

  • defaults for keys/args
  • validation of keys/args (some keys should be ints in some range, some keys can take one of predefined string values)
  • dependency between keys/args (if key --foo set - user should also provide value for --bar key)

Thanks.

2012-04-04 08:15
by Eugene Loy
It seems you are lazy to do your homework.If it is so, please tag the Q appropriately - Alok Save 2012-04-04 08:16
getopt/getopt_long is what I usually use, but your requirements are way beyond its scope - SirDarius 2012-04-04 08:19
If you can't find such a library, maybe you should take the time to make a library yourself? You can then use it for future applications, and maybe even publish it as open source for others to benefit - Some programmer dude 2012-04-04 08:22
@SirDarius Yup, getopt seems to be too plain for what I need - Eugene Loy 2012-04-04 08:24
By the way, such a library would tend to fall into the inner platform effect since the DSL needed to describe the default values, validation, dependency, etc, would be better coded directly using C++ - SirDarius 2012-04-04 08:26
Also, some people suggest Boost.Program_option, but it seems too heavy. I'd like to know about alternatives, if there are any.. - Eugene Loy 2012-04-04 08:27


3

You can use the library of Boost program_options:

The documentation with examples is here:

http://www.boost.org/doc/libs/1_42_0/doc/html/program_options/tutorial.html

2012-04-04 12:59
by Sebtm
Ads