Skip to content
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_subcommand_used doesn't work if same argument is added to parent and child #327

Open
MartyMcFlyInTheSky opened this issue Feb 29, 2024 · 0 comments

Comments

@MartyMcFlyInTheSky
Copy link

I think that's a bug. Consider this small program:

#include <iostream>
#include <filesystem>
#include <fstream> /* ifstream */
#include <string>
#include <cstdint> /* uint16_t */
#include <argparse/argparse.hpp>


int main(int argc, char* argv[]) {

    // Parse parameters with subcommands
    argparse::ArgumentParser program("dbclient");
    // !! remove the below argument then it works..
    program.add_argument("config_file_path")
        .default_value<std::filesystem::path>(DEFAULT_CONFIG_PATH)
        .required();  // Make positional arguments mandatory

        argparse::ArgumentParser push_command("push");  // Create the program object
        push_command.add_description("Uploads data specified in the given config file to the database.");
        push_command.add_argument("config_file_path")
            .default_value<std::filesystem::path>(DEFAULT_CONFIG_PATH)
            .required();  // Make positional arguments mandatory

        argparse::ArgumentParser pull_command("pull");  // Create the program object
        pull_command.add_description("Downloads data given by the database to the specified output directory.");
        pull_command.add_argument("config_file_path")
            .default_value<std::filesystem::path>(DEFAULT_CONFIG_PATH)
            .required();  // Make positional arguments mandatory


    program.add_subparser(push_command);
    program.add_subparser(pull_command);

    program.parse_args(argc, argv); // Parse the arguments

    if (program.is_subcommand_used(push_command)) {
        std::cout << "Push command used" << std::endl;
    } else if (program.is_subcommand_used(pull_command)) {
        std::cout << "Pull command used" << std::endl;
    } else {
        std::cout << "No command used apparently" << std::endl;
    }

}

If i start this with main push it will output "No command used apparently". I would have expected it to print "Push command used". It works if I remove the add_argument to the program, it will correctly print "Push command used".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant