diff --git a/README.md b/README.md index ede30ef..a26b77f 100644 --- a/README.md +++ b/README.md @@ -75,6 +75,16 @@ To parse the command line do: auto result = options.parse(argc, argv); ``` +Or if you have all the arguments of `argv` in a `std::string`, separated by +white spaces, do: + +```cpp +auto result = options.parse(argv_str); +``` + +Note the support for string parsing is limited. No bash variable replacement and +wild card expansion. + To retrieve an option use `result.count("option")` to get the number of times it appeared, and diff --git a/src/example.cpp b/src/example.cpp index 420220e..caacd54 100644 --- a/src/example.cpp +++ b/src/example.cpp @@ -27,7 +27,7 @@ THE SOFTWARE. #include "cxxopts.hpp" void -parse(int argc, const char* argv[]) +parse(int argc, const char* argv[], bool parse_as_string = false) { try { @@ -77,7 +77,20 @@ parse(int argc, const char* argv[]) options.parse_positional({"input", "output", "positional"}); - auto result = options.parse(argc, argv); + cxxopts::ParseResult result; + if (parse_as_string) + { + std::string argv_str; + for (int i = 0; i < argc; ++i) + { + argv_str += std::string(argv[i]) + std::string(" "); + } + result = options.parse(argv_str); + } + else + { + result = options.parse(argc, argv); + } if (result.count("help")) { @@ -185,5 +198,8 @@ int main(int argc, const char* argv[]) { parse(argc, argv); + // Parse argv as string + parse(argc, argv, true); + return 0; }