Explicit boolean option parsed into std::optional
ottmar-zittlau opened this issue · comments
Hi,
I would like to add a boolean option where the user explicitly needs to specify the value.
In addition, I want to parse this option into a std::optional
, because later on it is relevant if the bool has been set or not.
I prepared a small example:
#include <CLI/CLI.hpp>
#include <iostream>
#include <optional>
int main(int argc, const char* argv[]){
CLI::App app;
bool flag = false;
std::optional<bool> optional_flag = std::nullopt;
app.add_option("--flag", flag, "description");
app.add_option("--optional_flag", optional_flag, "description");
app.parse(argc, argv);
std::cout << "flag: " << flag << std::endl;
if(optional_flag){
std::cout << "optional flag: " << optional_flag.value() << std::endl;
}
}
If I call this ./cli11_test --flag true --optional_flag true
I get the error
terminate called after throwing an instance of 'CLI::ConversionError'
what(): Could not convert: --optional_flag = true
I'm already using std::optional
for options of type std::string
and it works nicely. Can I also accomplish this here without explicitly checking the state of the underlying CLI::Option
object?
Thanks and best regards
oz
I tried this and it works as expected with no errors. What compiler and CLI11 version are you using?
Thanks for the quick reply - I was using CLI11 v1.9.1 and gcc 12.
But now I tried with CLI11 v2.4.1 and it worked, thanks for the suggestion.