hbristow / argparse

A slimline C++ class for parsing command-line arguments, with an interface similar to python's class of the same name
BSD 3-Clause "New" or "Revised" License
267 stars 68 forks source link

The example code does not work as expected. #8

Open PeterMitrano opened 6 years ago

PeterMitrano commented 6 years ago

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 commented 6 years ago

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 commented 5 years ago

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

PeterMitrano commented 5 years ago

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

vinjn commented 5 years ago

Same failure with me

PeterMitrano commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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

wanjiadenghuo111 commented 4 years ago

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