Parse command line input wrapped in a string

This is the simplest implementation of the functionality described in
the short description. This patch only deals with  the arguments seperated by
whitespaces, and process them as is, without any shell processing like
expanding wildcards or replacing variables.
This commit is contained in:
HuangZonghao 2022-04-29 00:45:45 -04:00
parent 8185e6bb3a
commit 95c77095e3

View File

@ -1772,6 +1772,9 @@ namespace cxxopts
ParseResult
parse(int argc, const char* const* argv);
ParseResult
parse(std::string argv_str);
OptionAdder
add_options(std::string group = "");
@ -2284,6 +2287,30 @@ Options::parse(int argc, const char* const* argv)
return parser.parse(argc, argv);
}
inline
ParseResult
Options::parse(std::string argv_str)
{
std::vector<std::string> tokens;
std::string token;
std::istringstream iss(argv_str);
while (std::getline(iss, token, ' '))
{
tokens.push_back(token);
}
const char** argv = new const char*[tokens.size()];
for (int i = 0; i < tokens.size(); ++i) {
argv[i] = tokens[i].c_str();
}
OptionParser parser(*m_options, m_positional, m_allow_unrecognised);
auto parse_result = parser.parse(tokens.size(), argv);
delete argv;
return parse_result;
}
inline ParseResult
OptionParser::parse(int argc, const char* const* argv)
{