mirror of
https://github.com/KeqingMoe/argparse.git
synced 2025-07-03 22:54:39 +00:00
Added example to README for option=value support
This commit is contained in:
parent
20095a697a
commit
6dd2a3cf4b
36
README.md
36
README.md
@ -934,6 +934,42 @@ foo@bar:/home/dev/$ ./main fex
|
||||
baz
|
||||
```
|
||||
|
||||
## Using `option=value` syntax
|
||||
|
||||
```cpp
|
||||
#include "argparse.hpp"
|
||||
#include <cassert>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
argparse::ArgumentParser program("test");
|
||||
program.add_argument("--foo").implicit_value(true).default_value(false);
|
||||
program.add_argument("--bar");
|
||||
|
||||
try {
|
||||
program.parse_args(argc, argv);
|
||||
}
|
||||
catch (const std::runtime_error& err) {
|
||||
std::cerr << err.what() << std::endl;
|
||||
std::cerr << program;
|
||||
std::exit(1);
|
||||
}
|
||||
|
||||
if (program.is_used("--foo")) {
|
||||
std::cout << "--foo: " << std::boolalpha << program.get<bool>("--foo") << "\n";
|
||||
}
|
||||
|
||||
if (program.is_used("--bar")) {
|
||||
std::cout << "--bar: " << program.get("--bar") << "\n";
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
```console
|
||||
foo@bar:/home/dev/$ ./test --bar=BAR --foo
|
||||
--foo: true
|
||||
--bar: BAR
|
||||
```
|
||||
|
||||
## CMake Integration
|
||||
|
||||
Use the latest argparse in your CMake project without copying any content.
|
||||
|
@ -1,15 +0,0 @@
|
||||
#include "argparse.hpp"
|
||||
#include <cassert>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
argparse::ArgumentParser program("test");
|
||||
program.add_argument("--foo").implicit_value(true).default_value(false);
|
||||
program.add_argument("bar");
|
||||
|
||||
auto unknown_args =
|
||||
program.parse_known_args({"test", "--foo", "--badger", "BAR", "spam"});
|
||||
|
||||
assert(program.get<bool>("--foo") == true);
|
||||
assert(program.get<std::string>("bar") == std::string{"BAR"});
|
||||
assert((unknown_args == std::vector<std::string>{"--badger", "spam"}));
|
||||
}
|
@ -28,4 +28,12 @@ TEST_CASE("Duplicate =-named and standard" * test_suite("equals_form")) {
|
||||
REQUIRE(result == "value");
|
||||
std::string result2{parser.get("--long")};
|
||||
REQUIRE(result2 == "NO_VALUE");
|
||||
}
|
||||
|
||||
TEST_CASE("Basic --value=value with nargs(2)" * test_suite("equals_form")) {
|
||||
argparse::ArgumentParser parser("test");
|
||||
parser.add_argument("--long").nargs(2);
|
||||
parser.parse_args({"test", "--long=value1", "value2"});
|
||||
REQUIRE((parser.get<std::vector<std::string>>("--long") ==
|
||||
std::vector<std::string>{"value1", "value2"}));
|
||||
}
|
Loading…
Reference in New Issue
Block a user