#include <argparse/argparse.hpp>
#include <iostream>
#include <string>
int main() {
argparse::ArgumentParser program("test");
program.add_argument("--color")
.choices("red", "green")
.nargs(1)
.action([](const std::string &value) {
std::cout << "action called with size="
<< value.size() << '\n';
return value;
});
try {
program.parse_args({"test", "--color"});
std::cout << "parse succeeded\n";
} catch (const std::exception &error) {
std::cout << "caught: " << error.what() << '\n';
}
}
I intend the option --color to have a default value when it isn't specified, but when it is, it must have an argument. So either you don't specify --color at all, or you have to specify --color red or --color green. The example program prints:
action called with size=0
parse succeeded
when it should have failed.
I intend the option
--colorto have a default value when it isn't specified, but when it is, it must have an argument. So either you don't specify--colorat all, or you have to specify--color redor--color green. The example program prints:when it should have failed.