Update example code and readme for string parsing
This commit is contained in:
parent
95c77095e3
commit
4f7ab890fa
10
README.md
10
README.md
@ -75,6 +75,16 @@ To parse the command line do:
|
|||||||
auto result = options.parse(argc, argv);
|
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
|
To retrieve an option use `result.count("option")` to get the number of times
|
||||||
it appeared, and
|
it appeared, and
|
||||||
|
|
||||||
|
|||||||
@ -27,7 +27,7 @@ THE SOFTWARE.
|
|||||||
#include "cxxopts.hpp"
|
#include "cxxopts.hpp"
|
||||||
|
|
||||||
void
|
void
|
||||||
parse(int argc, const char* argv[])
|
parse(int argc, const char* argv[], bool parse_as_string = false)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@ -77,7 +77,20 @@ parse(int argc, const char* argv[])
|
|||||||
|
|
||||||
options.parse_positional({"input", "output", "positional"});
|
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"))
|
if (result.count("help"))
|
||||||
{
|
{
|
||||||
@ -185,5 +198,8 @@ int main(int argc, const char* argv[])
|
|||||||
{
|
{
|
||||||
parse(argc, argv);
|
parse(argc, argv);
|
||||||
|
|
||||||
|
// Parse argv as string
|
||||||
|
parse(argc, argv, true);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user