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

Subparser example snippet can't work #342

Open
trick2011 opened this issue Apr 2, 2024 · 0 comments
Open

Subparser example snippet can't work #342

trick2011 opened this issue Apr 2, 2024 · 0 comments

Comments

@trick2011
Copy link

The following snippets can't work. It is given in the section Getting Argument and Subparser Instances

argparse::ArgumentParser program("test");

program.add_argument("--dir");
program.at("--dir").default_value(std::string("/home/user"));

program.add_subparser(argparse::ArgumentParser{"walk"});
program.at<argparse::ArgumentParser>("walk").add_argument("depth");

It proposes creating a temporary ArgumentParser instance and passing it to add_subparser. This can't work as add_subparser takes ArgumentParser &parser reference and adds it to m_subparsers which is a referencewrapper to the original object. std::list<std::reference_wrapper<ArgumentParser>> m_subparsers;

As such in my application the compiler complains with:

error: cannot bind non-const lvalue reference of type ‘argparse::ArgumentParser&’ to an rvalue of type ‘argparse::ArgumentParser’
6 | program.add_subparser(argparse::ArgumentParser{"walk"});

I would agree that the intended behaviour is wanted. I see two 'easy' options, either by changing m_subparsers to be a copy of the passed subparser or keeping a separate copy when calling a copy-by-value variant of the add_subparser function.

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