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

The example code does not work as expected. #8

Open
PeterMitrano opened this issue Oct 24, 2017 · 8 comments
Open

The example code does not work as expected. #8

PeterMitrano opened this issue Oct 24, 2017 · 8 comments

Comments

@PeterMitrano
Copy link

The example in the read me does not work, which is no surprise because I don't see how any code to convert strings to number types.

Input:

./a.out --cactus=10

Expected output:

program exits with code 10

Observed output:

terminate called after throwing an instance of 'std::bad_cast'
  what():  std::bad_cast
Aborted (core dumped)

I normally would just move and and assume the project was broken, but it's the first result on google when you search "argparse C++", so I feel like it should be maintained and work as expected.

@Regan-Koopmans
Copy link

I agree. Would you be keen to help get it to a production-ready standard? I think we could do it over a few days.

I believe the argument parsing is currently designed to be of the form:

./a.out --cactus 10

But I think it would be good to accomodate the form you are talking about as well.

We might be able to do string to numeric conversion by inspecing the type_info?

@asm95
Copy link

asm95 commented Nov 7, 2018

It doesn't work even with ./a.out --cactus 10. It stills give std::bad_cast

@PeterMitrano
Copy link
Author

I use this one instead, it works: https://github.com/Taywee/args

@vinjn
Copy link

vinjn commented Jul 5, 2019

Same failure with me

@PeterMitrano
Copy link
Author

alternatively, @hbristow could simply make the repository private or delete it so that maybe google will stop bringing people here. I've done this with my open projects which gather attention but I am not interested in maintaining.

@thxmxx
Copy link

thxmxx commented Aug 16, 2019

You can retrieve the value as a string and then call stoi() .

int cactus = stoi(parser.retrieve<std::string>("cactus"));

You can also modify the retrieve method to something like this:

    template <typename T>
    T retrieve(const String& name) {
        if (index_.count(delimit(name)) == 0) throw std::out_of_range("Key not found");
        size_t N = index_[delimit(name)];
        if(std::is_same<T, int>::value) return static_cast<T>(stoi(variables_[N].castTo<std::string>()));
        if(std::is_same<T, float>::value) return static_cast<T>(stof(variables_[N].castTo<std::string>()));
    if(std::is_same<T, double>::value) return static_cast<T>(stod(variables_[N].castTo<std::string>()));
        return variables_[N].castTo<T>();
    }

And then you can call int cactus = parser.retrieve<int>("cactus");

@jiwoong-choi
Copy link

For anyone who is interested, I have fixed the bug in 'retrieve' myself.
https://github.com/jiwoong-choi/argparse

@wanjiadenghuo111
Copy link

For anyone who is interested, I have promote its fault tolerance, let it use easy. also format the code style of allman.
https://github.com/wanjiadenghuo111/argparse

base on: https://github.com/jiwoong-choi/argparse

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

7 participants