clipp icon indicating copy to clipboard operation
clipp copied to clipboard

Support std::optional<..>

Open tversteeg opened this issue 4 years ago • 3 comments

I would like to be able to parse a struct containing std::optional<..> fields. For example:

struct arg_values {
     std::optional<int> pid;
} values;

auto cli = (
    clipp::option("--pid") & clipp::opt_integer("PID", values.parent_pid)
);

tversteeg avatar Sep 16 '19 14:09 tversteeg

Good idea. I should definitly add built-in support for std::optional.

muellan avatar Sep 18 '19 07:09 muellan

One thing this would provide is a straight forward way to check if an optional argument was passed or not.

Is there currently a canonical way to do that? I guess I could use .call(...) with some lambda that sets a bool to true, but that seems quite verbose. I was thinking something like:

option("--foo").set_true(foo_passed) & value(foo)

Or a way to get a handle the "option object" and query it after command line parsing, something like foo_option.was_passed().

NikolausDemmel avatar Oct 07 '20 23:10 NikolausDemmel

Is there currently a canonical way to do that?

To answer my own question, according to https://github.com/muellan/clipp#actions I guess you can do:

bool foo_passed = false;
int foo = 0;
option("--foo").set(foo_passed) & value("FOO", foo);

NikolausDemmel avatar Oct 08 '20 23:10 NikolausDemmel