Open PeterMitrano opened 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
?
It doesn't work even with ./a.out --cactus 10
. It stills give std::bad_cast
I use this one instead, it works: https://github.com/Taywee/args
Same failure with me
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.
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");
For anyone who is interested, I have fixed the bug in 'retrieve' myself. https://github.com/jiwoong-choi/argparse
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
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:
Expected output:
Observed output:
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.