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

Implicit template type of 'present' is misleading. #366

Open
EddyXorb opened this issue Jul 9, 2024 · 0 comments
Open

Implicit template type of 'present' is misleading. #366

EddyXorb opened this issue Jul 9, 2024 · 0 comments

Comments

@EddyXorb
Copy link

EddyXorb commented Jul 9, 2024

Say I have

int main(int argc, char** argv)
{
std::unique_ptr<argparse::ArgumentParser> parser =
	std::make_unique<argparse::ArgumentParser>("finland-course-optimization");
parser.add_argument("--number").scan<'i', int>()
parser.parse_args(argc,argv);

int n;
if (parser.present("number"))
   n = parser.get<int>("number");
}

This will compile.
And crash during runtime with 'bad any cast'.
If I omit the if and use 'get' directly, then it will parse correctly. Strange!

I lost one hour finding out that if I use 'present', there is default template argument 'std::string' that will throw the exception.
To correct the above example, one has to write 'present("number")'.

In my opinion the default template argument is quite a trap and should be omitted if possible.
Another possibility would be to work with constexpr type lists and avoid successful compilation if the type is wrongly specified. This would be the best approach in my opinion.
It could perhaps be realized using enums for the arguments and magic_params.

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