muellan / clipp

easy to use, powerful & expressive command line argument parsing for modern C++ / single header / usage & doc generation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Parameter Verification

landersson opened this issue · comments

Hi,

I had q quick look through the docs but apart from generating a "filter", i.e "integer", I didn't find any way to define the required type of options.

For example, if I specify option "n" as an integer, I'd ideally like an automated error message during parsing if the user specifies something that can't be parsed as an integer. I'd also like the integer requirement to be stated in the auto-generated documentation.

Is this possible at the moment?

Error messages have to be generated by the developer. You can check, if parsing fails, either per parameter (see here) or by analyzing the parsing result (see here).
At the moment, I dont't have any default error handling facility included. The requirements vary a lot and I'd only like to include something truly canonical.
However, the debug namespace contains a function for quick analysis:

using namespace clipp;
auto cli = ( /* ... */);
auto result = parse(argc, argv, cli);
if(!result) debug::print(std::cout, result);

Also the filter does only check, if an argument can be matched and not why it couldn't be matched.

Thanks for the suggestions. I'm still a bit uncertain how to go about detecting and handling, for example, an integer parsing error, but I haven't had time to sit down and try properly yet. I'll follow up on this issue if I'm not able to solve it after trying a bit more.

Still, IHMO, being able to specify the expected type of a parameter value (int, float, string, limited set of choices etc) and have the parser able to tell you if the given parameter matches that type or not would be very useful. Naturally, how to actually deal with the parsing error would have to be dealt with by the user of the library.

Anyway, thanks for developing and sharing this library... it looks promising!

I had another look at this... As an example, I'm trying to add an option that takes a mandatory integer argument:

Option 1:
int max;
(option("-m") & value("max=1", max)) % "Maximum value",

If specifying an argument that can't be parsed as an integer, this will set 'max' to zero and I haven't been able to figure out if there is even a way to detect that something went wrong.

Option 2:
int max;
(option("-m") & integer("max=1", max)) % "Maximum value",

This will at least filter out any non-integer arguments, and I can detect that something went wrong by going through res.missing(), as well as checking the vector of unknown arguments populated by using the "any_other" catch-all parameter. But this all seems a little bit too complicated and ambiguous.

I guess the type information for the argument is already specified by passing an int ('max') as the second argument of the value parameter. What I'd like is some convenient way of detecting if the argument couldn't be parsed into the destination variable specified ('max' in my case).

Maybe something like this:

 for(const auto& m : res.parse_errors()) { 
    cout << "Parameter parse error after index " << m.after_index() << ':' << m.parse_error_msg() << '\n';
}

Maybe this is all possible already and I just don't know how do achieve what I want. How would you currently implement what I'm trying to do?