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

Using std::vector<std::string> instead of argc, argv #66

Closed
gouarin opened this issue Aug 16, 2017 · 2 comments
Closed

Using std::vector<std::string> instead of argc, argv #66

gouarin opened this issue Aug 16, 2017 · 2 comments

Comments

@gouarin
Copy link

gouarin commented Aug 16, 2017

Before you reply to my issue #65, I have a question.

In our program, we need to parse a std::string instead of using argc, argv. The options are in this string. So to be able to use cxxopts I do a hack of the Options class and add two functions

void parse(std::vector<std::string>& argv);
void checked_parse_arg 
        (
        std::vector<std::string>& argv,
        int& current,
        std::shared_ptr<cxxopts::OptionDetails> value,
        const std::string& name
        );

The use case is

std::istringstream iss(line);
std::vector<std::string> results((std::istream_iterator<std::string>(iss)),
                                                  std::istream_iterator<std::string>());
auto options = get_options();
options.parse(results);

where line is the string to parse and we split this line with the space delimiter and set the result in a vector of string.

Did you already think about this use case ? What is the best thing to do ? Add a PR with this work ?

@jarro2783
Copy link
Owner

I had to use a hack in the tests to pass in an array of character pointers for argv. Giving it a vector of strings would obviously be much simpler.

This seems pretty reasonable to do. What if you define parse(const std::vector<std::string>&) so that it constructs an array of pointers to the string.c_str() values and pass that to the actual parse?

Do you ever care about any unswallowed arguments though? You could deal with that by modifying the input vector or returning a new one.

@gouarin
Copy link
Author

gouarin commented Aug 18, 2017

@jarro2783 I have fixed my implementation with the current version of cxxopts. So you can do the release.

@SylvainCorlay, I let you do the conda package when the release will be done.

@gouarin gouarin closed this as completed Aug 18, 2017
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

2 participants