mirror of
https://github.com/KeqingMoe/argparse.git
synced 2025-07-04 15:14:39 +00:00
Update README.md
This commit is contained in:
parent
07cad7ac9e
commit
4245c1a850
59
README.md
59
README.md
@ -3,61 +3,70 @@
|
||||
## Positional Arguments
|
||||
|
||||
```cpp
|
||||
#include <argparse.hpp>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
argparse::ArgumentParser program("main");
|
||||
|
||||
program.add_argument("input");
|
||||
program.add_argument("output");
|
||||
|
||||
program.parse(argc, argv);
|
||||
std::string input = program.get("input");
|
||||
std::string output = program.get("output");
|
||||
program.parse_args({"./main", "rocket.msh", "thrust_profile.csv"});
|
||||
|
||||
return 0;
|
||||
}
|
||||
std::string input = program.get("input"); // "rocket.msh"
|
||||
std::string output = program.get("output"); // "thrust_profile.csv"
|
||||
```
|
||||
|
||||
## Optional Arguments
|
||||
|
||||
```cpp
|
||||
#include <argparse.hpp>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
argparse::ArgumentParser program("main");
|
||||
|
||||
program.add_argument("--config")
|
||||
.help("configuration file")
|
||||
.default_value(std::string("config.yml"));
|
||||
.default_value(std::string("config.json"));
|
||||
|
||||
program.add_argument("-n", "--num_iterations")
|
||||
.help("The list of input files")
|
||||
.action([](const std::string& value) { return std::stoi(value); });
|
||||
|
||||
program.parse(argc, argv);
|
||||
std::string config = program.get("--config");
|
||||
int num_iterations = program.get<int>("-n");
|
||||
program.parse_args({"./main", "-n", "36"});
|
||||
|
||||
return 0;
|
||||
}
|
||||
std::string config = program.get("--config"); // config.json
|
||||
int num_iterations = program.get<int>("-n"); // 36
|
||||
```
|
||||
|
||||
## List of Arguments
|
||||
## Vector of Arguments
|
||||
|
||||
```cpp
|
||||
#include <argparse.hpp>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
argparse::ArgumentParser program("main");
|
||||
|
||||
program.add_argument("--input_files")
|
||||
.help("The list of input files")
|
||||
.nargs(3);
|
||||
|
||||
program.parse(argc, argv);
|
||||
std::vector<std::string> files = program.get<std::vector<std::string>>("--input_files");
|
||||
program.parse_args({"./main", "--input_files", "config.yml", "System.xml"});
|
||||
|
||||
return 0;
|
||||
}
|
||||
auto files = program.get<std::vector<std::string>>("--input_files"); // {"config.yml", "System.xml"}
|
||||
```
|
||||
|
||||
## Compound Toggle Arguments
|
||||
|
||||
```cpp
|
||||
argparse::ArgumentParser program("test");
|
||||
|
||||
program.add_argument("-a")
|
||||
.default_value(false)
|
||||
.implicit_value(true);
|
||||
|
||||
program.add_argument("-b")
|
||||
.default_value(false)
|
||||
.implicit_value(true);
|
||||
|
||||
program.add_argument("-c")
|
||||
.nargs(2)
|
||||
.action([](const std::string& value) { return std::stof(value); });
|
||||
|
||||
program.parse_args({ "./main", "-abc", "3.14", "2.718" });
|
||||
|
||||
auto a = program.get<bool>("-a"); // true
|
||||
auto b = program.get<bool>("-b"); // true
|
||||
auto c = program.get<std::vector<float>>("-c"); // {3.14f, 2.718f}
|
||||
```
|
||||
|
Loading…
Reference in New Issue
Block a user