Subcommand: add argument only to main command
nmoreaud opened this issue · 1 comments
nmoreaud commented
Hello,
I wonder how to declare an argument available only on the main command.
Ex: -v, --version
only on main command, leaving -v
available for verbose on subcommands.
Ex : git --version works
, but git log --version doesn't
p-ranav commented
You can do this:
#include "argparse.hpp"
int main(int argc, char *argv[]) {
argparse::ArgumentParser program("git", "1.1");
// git log subparser
argparse::ArgumentParser log_command("log");
log_command.add_argument("--version")
.flag()
.action([](const auto&) {
std::cout << "2.2\n";
});
program.add_subparser(log_command);
try {
program.parse_args(argc, argv);
}
catch (const std::exception& err) {
std::cerr << err.what() << std::endl;
std::cerr << program;
return 1;
}
}
foo:bar$ ./git --version
1.1
foo:bar$ ./git log --version
2.2