-
Greetings all, love the library. I'd like to validate an option range that is dependent on another option. I think this code snippet will show what I intend. MyApp::MyApp() : CLI::App("Description", "Appname")
{
client_cnt = 100;
clientidx_start = 0;
add_option("--client-cnt", client_cnt)->check(CLI::Range(1, 10000));
add_option("--clientidx-start", clientidx_start)->check(CLI::PositiveNumber & CLI::Validator([&]() {
if ((client_cnt + clientidx_start) > 10000) {
return "The number of clients exceeds the maxmium client index of 10000";
}
return "";
}));
}
I'm essentially wanting to constrain these two options such that (client_cnt + clientidx_start) is < 10000. What would be the best way to do this? (The validator + lambda are more for show, not correct) Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
MyApp::MyApp() : CLI::App("Description", "Appname")
{
client_cnt = 100;
clientidx_start = 0;
add_option("--client-cnt", client_cnt)->check(CLI::Range(1, 10000))->trigger_on_parse();
add_option("--clientidx-start", clientidx_start)->check(CLI::PositiveNumber & CLI::Validator([&](const std::string &checkString) {
std::uint32_t val{0};
details::lexical_cast(checkString, val);
if ((client_cnt + val) > 10000) {
return "The number of clients exceeds the maximum client index of 10000";
}
return "";
}));
} I think this will work(haven't checked exactly). Another option would be just do the range checking individually and add a small callback on the app itself that will run after all the individual options, that does a final check and throws a That would look like MyApp::MyApp() : CLI::App("Description", "Appname")
{
client_cnt = 100;
clientidx_start = 0;
add_option("--client-cnt", client_cnt)->check(CLI::Range(1, 10000));
add_option("--clientidx-start", clientidx_start)->check(CLI::Range(0, 9998));
final_callback([&]() {
if ((client_cnt + clientidx_start) > 10000) {
throw CLI::ValidationError("The number of clients exceeds the maximum client index of 10000");
}
});
} |
Beta Was this translation helpful? Give feedback.
I think this will work(haven't checked exactly).
Two things, --client-cnt would have the trigger_on_parse() modifier set, this means th…