clipp icon indicating copy to clipboard operation
clipp copied to clipboard

Parameter Verification

Open landersson opened this issue 5 years ago • 3 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?

landersson avatar Nov 29 '18 23:11 landersson

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.

muellan avatar Nov 30 '18 09:11 muellan

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!

landersson avatar Nov 30 '18 10:11 landersson

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?

landersson avatar Dec 01 '18 03:12 landersson