mirror of
https://github.com/KeqingMoe/argparse.git
synced 2025-07-04 07:04:39 +00:00
Merge branch 'master' of https://github.com/p-ranav/argparse
This commit is contained in:
commit
307181159e
38
README.md
38
README.md
@ -22,6 +22,25 @@ std::string input = program.get("input"); // "rocket.msh"
|
||||
std::string output = program.get("output"); // "thrust_profile.csv"
|
||||
```
|
||||
|
||||
### Construct Objects from Arguments with ```.action```
|
||||
|
||||
```cpp
|
||||
argparse::ArgumentParser program("json_test");
|
||||
|
||||
program.add_argument("config")
|
||||
.action([](const std::string& value) {
|
||||
// read a JSON file
|
||||
std::ifstream stream(value);
|
||||
nlohmann::json config_json;
|
||||
stream >> config_json;
|
||||
return config_json;
|
||||
});
|
||||
|
||||
program.parse_args({"./test", "config.json"});
|
||||
|
||||
nlohmann::json config = program.get<nlohmann::json>("config");
|
||||
```
|
||||
|
||||
### Optional Arguments
|
||||
|
||||
```cpp
|
||||
@ -126,22 +145,3 @@ auto b = program.get<bool>("-b"); // true
|
||||
auto c = program.get<std::vector<float>>("-c"); // {3.14f, 2.718f}
|
||||
auto files = program.get<std::vector<std::string>>("--files"); // {"a.txt", "b.txt", "c.txt"}
|
||||
```
|
||||
|
||||
### Construct class objects from arguments
|
||||
|
||||
```cpp
|
||||
argparse::ArgumentParser program("json_test");
|
||||
|
||||
program.add_argument("config")
|
||||
.action([](const std::string& value) {
|
||||
// read a JSON file
|
||||
std::ifstream stream(value);
|
||||
nlohmann::json config_json;
|
||||
stream >> config_json;
|
||||
return config_json;
|
||||
});
|
||||
|
||||
program.parse_args({"./test", "config.json"});
|
||||
|
||||
nlohmann::json config = program.get<nlohmann::json>("config");
|
||||
```
|
||||
|
Loading…
Reference in New Issue
Block a user