-
Notifications
You must be signed in to change notification settings - Fork 261
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is there a way to create a default subparser? #390
Comments
I think this is actually what I want... #include <iostream>
#include <argparse/argparse.hpp>
int main(int argc, const char** argv) {
argparse::ArgumentParser program("compiler");
program.add_description("test please ignore");
argparse::ArgumentParser sub_command("compile");
sub_command.add_argument("-o").default_value(std::string("a.out")).nargs(1);
program.add_subparser(sub_command);
program.add_argument("--test").default_value(std::string{"test"});
try {
auto unknown_args = program.parse_known_args(argc, argv);
if (unknown_args.size()) {
sub_command.parse_args(unknown_args);
}
} catch (const std::exception& err) {
std::cerr << err.what() << std::endl;
std::cerr << program;
std::exit(1);
}
std::cout << program.get<std::string>("--test") << std::endl;
if (program.is_subcommand_used(sub_command)) {
std::cout << sub_command.get<std::string>("-o") << std::endl;
}
} but there seems to be a bug with the functionality? If you specify 2360 for (const auto &opt : m_optional_arguments) {
2361 if (!opt.m_implicit_value.has_value()) {
2362 // not a flag, requires a value
2363 if (!opt.m_is_used) {
2364 throw std::runtime_error(
2365 "Zero positional arguments expected, did you mean " +
2366 opt.get_usage_full());
2367 }
2368 }
2369 }
Oddly enough, as shown above, it also shows the |
Ohh, it's because it skips the first argument... Passing a dummy value (the subparser's name) seems to fix this. I also need to modify the subparser-usage check to see if the subparser was used via the super-parser, or directly (using ArgumentParser::operator bool): #include <iostream>
#include <argparse/argparse.hpp>
int main(int argc, const char** argv) {
argparse::ArgumentParser program("compiler");
program.add_description("test please ignore");
argparse::ArgumentParser sub_command("compile");
sub_command.add_argument("-o").default_value(std::string("a.out")).nargs(1);
program.add_subparser(sub_command);
program.add_argument("--test").default_value(std::string{"test"});
try {
auto unknown_args = program.parse_known_args(argc, argv);
if (unknown_args.size()) {
std::vector<std::string> compiler_args{"compile"};
compiler_args.reserve(unknown_args.size() + 1);
compiler_args.insert(
compiler_args.end(), unknown_args.begin(), unknown_args.end()
);
sub_command.parse_args(compiler_args);
}
} catch (const std::exception& err) {
std::cerr << err.what() << std::endl;
std::cerr << program;
std::exit(1);
}
std::cout << program.get<std::string>("--test") << std::endl;
if (program.is_subcommand_used(sub_command) || sub_command) {
std::cout << sub_command.get<std::string>("-o") << std::endl;
}
} |
I'd like the ability to use one of my subparsers as the default one if no subparser is specified. It is somewhat possible to do as:
But if your subparser is not specified, you're guaranteed an error on the initial parse:
Failed to parse '...', did you mean 'subparser1'
This half measure also falls a part if you have arguments on the parent parser that you want set as well.
Is there another way to do this currently?
The text was updated successfully, but these errors were encountered: