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

Parser using parent parser shows help message for parent parser instead of own message #165

Closed
ericonr opened this issue Apr 13, 2022 · 6 comments

Comments

@ericonr
Copy link
Contributor

ericonr commented Apr 13, 2022

Hi!

I'm using parent parsers with argparse, but it seems that, when used, the automatic -h,--help and -v,--version options no longer exist in the "child" parser.

int main(int argc, char *argv[])
{
    argparse::ArgumentParser parent_args("parent");
    parent_args.add_argument("-b").help("board").required().scan<'d', int>();

    argparse::ArgumentParser child("child");
    decode_args.add_parents(parent_args);

    try {
        child.parse_args(argc, argv);
    } catch (const std::runtime_error &err) {
        fprintf(stderr, "argparse error: %s\n", err.what());
        return 1;
    }
}

Passing -b <value> works just fine, but not -h or -v, nor their long versions. If I use the parent parser though, everything works.

I get the following error:

$ ./parent -h
argparse error: -h: expected 0 argument(s). 1 provided.
@ericonr
Copy link
Contributor Author

ericonr commented Apr 13, 2022

This has been fixed in version 2.3, I will try to update the version in meson's wrapdb.

Sorry for the noise, and thanks for the project!

@ericonr ericonr closed this as completed Apr 13, 2022
@ericonr
Copy link
Contributor Author

ericonr commented Apr 13, 2022

sorry for the noise again

The help message, however, is the wrong one (the parent's), as is the version number. Reopening with new title.

@ericonr ericonr reopened this Apr 13, 2022
@ericonr ericonr changed the title Parent parser loses default options Parser using parent parser shows help message for parent parser instead of own message Apr 13, 2022
@ericonr
Copy link
Contributor Author

ericonr commented Apr 13, 2022

This can be worked around by initializing the parent parser with argparse::ArgumentParser parent_args("parent", "1.0", argparse::default_arguments::none);

Maybe documenting it is a good enough stopgap instead of changing how initialization as a whole works. I can make a PR for the README, in that case.

@skrobinson
Copy link
Contributor

Thank you for your report. Let me look into this and get back to you next week.

@skrobinson
Copy link
Contributor

Digging into the project history, I'm guessing the expected practice is to disable the parent parser default arguments. But that paragraph did not get copied into the README.

You can see the Python Docs are the source of ArgumentParser::add_parents() documentation

You could adapt the relevant Python paragraph (immediately under the code example) to include in Parent Parsers. And we should cite the source of all these
direct quotes.

@ericonr
Copy link
Contributor Author

ericonr commented Apr 16, 2022

Thank you for the suggestions! I will take a look and try to come up with a PR :)

skrobinson added a commit to skrobinson/argparse that referenced this issue Jan 5, 2023
This replaces the verbiage copied from the Python argparse documentation
and makes the code sample more concrete. This illustrates how to avoid the
multiple help output problem reported in p-ranav#165.

Closes p-ranav#165

Signed-off-by: Sean Robinson <[email protected]>
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

2 participants