mirror of
https://github.com/KeqingMoe/argparse.git
synced 2025-07-04 15:14:39 +00:00
Merge branch 'master' into feature/variable-length-nargs
This commit is contained in:
commit
08943f47ab
@ -110,7 +110,7 @@ SpacesInContainerLiterals: true
|
|||||||
SpacesInCStyleCastParentheses: false
|
SpacesInCStyleCastParentheses: false
|
||||||
SpacesInParentheses: false
|
SpacesInParentheses: false
|
||||||
SpacesInSquareBrackets: false
|
SpacesInSquareBrackets: false
|
||||||
Standard: Cpp11
|
Standard: c++17
|
||||||
TabWidth: 8
|
TabWidth: 8
|
||||||
UseTab: Never
|
UseTab: Never
|
||||||
...
|
...
|
||||||
|
21
.clang-tidy
Normal file
21
.clang-tidy
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
Checks:
|
||||||
|
-*,
|
||||||
|
clang-analyzer-*,
|
||||||
|
cppcoreguidelines-avoid-c-arrays,
|
||||||
|
cppcoreguidelines-special-member-functions,
|
||||||
|
readability-*,
|
||||||
|
|
||||||
|
CheckOptions:
|
||||||
|
- { key: readability-identifier-naming.ClassCase, value: CamelCase }
|
||||||
|
- { key: readability-identifier-naming.ConstexprVariableCase, value: lower_case }
|
||||||
|
- { key: readability-identifier-naming.ConstexprVariableIgnoredRegexp, value: "^Is.+" }
|
||||||
|
- { key: readability-identifier-naming.FunctionCase, value: lower_case }
|
||||||
|
- { key: readability-identifier-naming.NamespaceCase, value: lower_case }
|
||||||
|
- { key: readability-identifier-naming.ParameterCase, value: lower_case }
|
||||||
|
- { key: readability-identifier-naming.PrivateMemberCase, value: lower_case }
|
||||||
|
- { key: readability-identifier-naming.PrivateMemberPrefix, value: m_ }
|
||||||
|
- { key: readability-identifier-naming.StructCase, value: CamelCase }
|
||||||
|
- { key: readability-identifier-naming.StructIgnoredRegexp, value: "parse_number" }
|
||||||
|
- { key: readability-identifier-naming.VariableCase, value: lower_case }
|
||||||
|
|
||||||
|
HeaderFilterRegex: '.*'
|
6
.github/workflows/ci.yml
vendored
6
.github/workflows/ci.yml
vendored
@ -18,7 +18,6 @@ jobs:
|
|||||||
- macos-latest-clang
|
- macos-latest-clang
|
||||||
- ubuntu-latest-clang
|
- ubuntu-latest-clang
|
||||||
- ubuntu-latest-gcc
|
- ubuntu-latest-gcc
|
||||||
- windows-2016-msvc
|
|
||||||
- windows-latest-msvc
|
- windows-latest-msvc
|
||||||
|
|
||||||
include:
|
include:
|
||||||
@ -42,11 +41,6 @@ jobs:
|
|||||||
c_compiler: msvc
|
c_compiler: msvc
|
||||||
cxx_compiler: msvc
|
cxx_compiler: msvc
|
||||||
|
|
||||||
- toolchain: windows-2016-msvc
|
|
||||||
os: windows-2016
|
|
||||||
c_compiler: msvc
|
|
||||||
cxx_compiler: msvc
|
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
|
|
||||||
- name: Checkout Code
|
- name: Checkout Code
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
cmake_minimum_required(VERSION 3.8)
|
cmake_minimum_required(VERSION 3.12.4)
|
||||||
|
|
||||||
project(argparse VERSION 1.0.0 LANGUAGES CXX)
|
project(argparse
|
||||||
set("PROJECT_DESCRIPTION" "A single header argument parser for C++17")
|
VERSION 2.5.0
|
||||||
set("PROJECT_HOMEPAGE_URL" "https://github.com/p-ranav/argparse")
|
DESCRIPTION "A single header argument parser for C++17"
|
||||||
|
HOMEPAGE_URL "https://github.com/p-ranav/argparse"
|
||||||
|
LANGUAGES CXX
|
||||||
|
)
|
||||||
|
|
||||||
option(ARGPARSE_BUILD_TESTS OFF)
|
option(ARGPARSE_BUILD_TESTS OFF)
|
||||||
option(ARGPARSE_LONG_VERSION_ARG_ONLY OFF)
|
option(ARGPARSE_LONG_VERSION_ARG_ONLY OFF)
|
||||||
|
194
README.md
194
README.md
@ -7,7 +7,7 @@
|
|||||||
<a href="https://github.com/p-ranav/argparse/blob/master/LICENSE">
|
<a href="https://github.com/p-ranav/argparse/blob/master/LICENSE">
|
||||||
<img src="https://img.shields.io/badge/License-MIT-yellow.svg" alt="license"/>
|
<img src="https://img.shields.io/badge/License-MIT-yellow.svg" alt="license"/>
|
||||||
</a>
|
</a>
|
||||||
<img src="https://img.shields.io/badge/version-2.2-blue.svg?cacheSeconds=2592000" alt="version"/>
|
<img src="https://img.shields.io/badge/version-2.5-blue.svg?cacheSeconds=2592000" alt="version"/>
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
## Highlights
|
## Highlights
|
||||||
@ -27,7 +27,7 @@ Simply include argparse.hpp and you're good to go.
|
|||||||
To start parsing command-line arguments, create an ```ArgumentParser```.
|
To start parsing command-line arguments, create an ```ArgumentParser```.
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
argparse::ArgumentParser program("program name");
|
argparse::ArgumentParser program("program_name");
|
||||||
```
|
```
|
||||||
|
|
||||||
**NOTE:** There is an optional second argument to the `ArgumentParser` which is the program version. Example: `argparse::ArgumentParser program("libfoo", "1.9.0");`
|
**NOTE:** There is an optional second argument to the `ArgumentParser` which is the program version. Example: `argparse::ArgumentParser program("libfoo", "1.9.0");`
|
||||||
@ -49,7 +49,7 @@ Here's an example of a ***positional argument***:
|
|||||||
#include <argparse/argparse.hpp>
|
#include <argparse/argparse.hpp>
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
argparse::ArgumentParser program("program name");
|
argparse::ArgumentParser program("program_name");
|
||||||
|
|
||||||
program.add_argument("square")
|
program.add_argument("square")
|
||||||
.help("display the square of a given integer")
|
.help("display the square of a given integer")
|
||||||
@ -59,9 +59,9 @@ int main(int argc, char *argv[]) {
|
|||||||
program.parse_args(argc, argv);
|
program.parse_args(argc, argv);
|
||||||
}
|
}
|
||||||
catch (const std::runtime_error& err) {
|
catch (const std::runtime_error& err) {
|
||||||
std::cout << err.what() << std::endl;
|
std::cerr << err.what() << std::endl;
|
||||||
std::cout << program;
|
std::cerr << program;
|
||||||
exit(0);
|
std::exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto input = program.get<int>("square");
|
auto input = program.get<int>("square");
|
||||||
@ -101,9 +101,9 @@ try {
|
|||||||
program.parse_args(argc, argv);
|
program.parse_args(argc, argv);
|
||||||
}
|
}
|
||||||
catch (const std::runtime_error& err) {
|
catch (const std::runtime_error& err) {
|
||||||
std::cout << err.what() << std::endl;
|
std::cerr << err.what() << std::endl;
|
||||||
std::cout << program;
|
std::cerr << program;
|
||||||
exit(0);
|
std::exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (program["--verbose"] == true) {
|
if (program["--verbose"] == true) {
|
||||||
@ -160,16 +160,16 @@ If you want to know whether the user supplied a value for an argument that has a
|
|||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
program.add_argument("--color")
|
program.add_argument("--color")
|
||||||
.default_value("orange")
|
.default_value(std::string{"orange"}) // might otherwise be type const char* leading to an error when trying program.get<std::string>
|
||||||
.help("specify the cat's fur color");
|
.help("specify the cat's fur color");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
program.parse_args(argc, argv); // Example: ./main --color orange
|
program.parse_args(argc, argv); // Example: ./main --color orange
|
||||||
}
|
}
|
||||||
catch (const std::runtime_error& err) {
|
catch (const std::runtime_error& err) {
|
||||||
std::cout << err.what() << std::endl;
|
std::cerr << err.what() << std::endl;
|
||||||
std::cout << program;
|
std::cerr << program;
|
||||||
exit(0);
|
std::exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto color = program.get<std::string>("--color"); // "orange"
|
auto color = program.get<std::string>("--color"); // "orange"
|
||||||
@ -190,9 +190,9 @@ try {
|
|||||||
program.parse_args(argc, argv); // Example: ./main --color red --color green --color blue
|
program.parse_args(argc, argv); // Example: ./main --color red --color green --color blue
|
||||||
}
|
}
|
||||||
catch (const std::runtime_error& err) {
|
catch (const std::runtime_error& err) {
|
||||||
std::cout << err.what() << std::endl;
|
std::cerr << err.what() << std::endl;
|
||||||
std::cout << program;
|
std::cerr << program;
|
||||||
exit(0);
|
std::exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto colors = program.get<std::vector<std::string>>("--color"); // {"red", "green", "blue"}
|
auto colors = program.get<std::vector<std::string>>("--color"); // {"red", "green", "blue"}
|
||||||
@ -200,6 +200,24 @@ auto colors = program.get<std::vector<std::string>>("--color"); // {"red", "gre
|
|||||||
|
|
||||||
Notice that ```.default_value``` is given an explicit template parameter to match the type you want to ```.get```.
|
Notice that ```.default_value``` is given an explicit template parameter to match the type you want to ```.get```.
|
||||||
|
|
||||||
|
#### Repeating an argument to increase a value
|
||||||
|
|
||||||
|
A common pattern is to repeat an argument to indicate a greater value.
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
int verbosity = 0;
|
||||||
|
program.add_argument("-V", "--verbose")
|
||||||
|
.action([&](const auto &) { ++verbosity; })
|
||||||
|
.append()
|
||||||
|
.default_value(false)
|
||||||
|
.implicit_value(true)
|
||||||
|
.nargs(0);
|
||||||
|
|
||||||
|
program.parse_args(argc, argv); // Example: ./main -VVVV
|
||||||
|
|
||||||
|
std::cout << "verbose level: " << verbosity << std::endl; // verbose level: 4
|
||||||
|
```
|
||||||
|
|
||||||
### Negative Numbers
|
### Negative Numbers
|
||||||
|
|
||||||
Optional arguments start with ```-```. Can ```argparse``` handle negative numbers? The answer is yes!
|
Optional arguments start with ```-```. Can ```argparse``` handle negative numbers? The answer is yes!
|
||||||
@ -220,9 +238,9 @@ try {
|
|||||||
program.parse_args(argc, argv);
|
program.parse_args(argc, argv);
|
||||||
}
|
}
|
||||||
catch (const std::runtime_error& err) {
|
catch (const std::runtime_error& err) {
|
||||||
std::cout << err.what() << std::endl;
|
std::cerr << err.what() << std::endl;
|
||||||
std::cout << program;
|
std::cerr << program;
|
||||||
exit(0);
|
std::exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Some code to print arguments
|
// Some code to print arguments
|
||||||
@ -239,7 +257,7 @@ As you can see here, ```argparse``` supports negative integers, negative floats
|
|||||||
### Combining Positional and Optional Arguments
|
### Combining Positional and Optional Arguments
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
argparse::ArgumentParser program("test");
|
argparse::ArgumentParser program("main");
|
||||||
|
|
||||||
program.add_argument("square")
|
program.add_argument("square")
|
||||||
.help("display the square of a given number")
|
.help("display the square of a given number")
|
||||||
@ -253,9 +271,9 @@ try {
|
|||||||
program.parse_args(argc, argv);
|
program.parse_args(argc, argv);
|
||||||
}
|
}
|
||||||
catch (const std::runtime_error& err) {
|
catch (const std::runtime_error& err) {
|
||||||
std::cout << err.what() << std::endl;
|
std::cerr << err.what() << std::endl;
|
||||||
std::cout << program;
|
std::cerr << program;
|
||||||
exit(0);
|
std::exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
int input = program.get<int>("square");
|
int input = program.get<int>("square");
|
||||||
@ -285,18 +303,53 @@ The square of 4 is 16
|
|||||||
|
|
||||||
```
|
```
|
||||||
$ ./main --help
|
$ ./main --help
|
||||||
Usage: ./main [options] square
|
Usage: main [options] square
|
||||||
|
|
||||||
Positional arguments:
|
Positional arguments:
|
||||||
square display a square of a given number
|
square display the square of a given number
|
||||||
|
|
||||||
Optional arguments:
|
Optional arguments:
|
||||||
-h, --help show this help message and exit
|
-h --help shows help message and exits [default: false]
|
||||||
-v, --verbose enable verbose logging
|
-v --version prints version information and exits [default: false]
|
||||||
|
--verbose [default: false]
|
||||||
```
|
```
|
||||||
|
|
||||||
You may also get the help message in string via `program.help().str()`.
|
You may also get the help message in string via `program.help().str()`.
|
||||||
|
|
||||||
|
#### Adding a description and an epilog to help
|
||||||
|
|
||||||
|
`ArgumentParser::add_description` will add text before the detailed argument
|
||||||
|
information. `ArgumentParser::add_epilog` will add text after all other help output.
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
argparse::ArgumentParser program("main");
|
||||||
|
|
||||||
|
program.add_argument("thing")
|
||||||
|
.help("Thing to use.");
|
||||||
|
program.add_description("Forward a thing to the next member.");
|
||||||
|
program.add_epilog("Possible things include betingalw, chiz, and res.");
|
||||||
|
|
||||||
|
program.parse_args(argc, argv);
|
||||||
|
|
||||||
|
std::cout << program << std::endl;
|
||||||
|
```
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ ./main --help
|
||||||
|
Usage: main thing
|
||||||
|
|
||||||
|
Forward a thing to the next member.
|
||||||
|
|
||||||
|
Positional arguments:
|
||||||
|
thing Thing to use.
|
||||||
|
|
||||||
|
Optional arguments:
|
||||||
|
-h --help shows help message and exits [default: false]
|
||||||
|
-v --version prints version information and exits [default: false]
|
||||||
|
|
||||||
|
Possible things include betingalw, chiz, and res.
|
||||||
|
```
|
||||||
|
|
||||||
### List of Arguments
|
### List of Arguments
|
||||||
|
|
||||||
ArgumentParser objects usually associate a single command-line argument with a single action to be taken. The ```.nargs``` associates a different number of command-line arguments with a single action. When using ```nargs(N)```, N arguments from the command line will be gathered together into a list.
|
ArgumentParser objects usually associate a single command-line argument with a single action to be taken. The ```.nargs``` associates a different number of command-line arguments with a single action. When using ```nargs(N)```, N arguments from the command line will be gathered together into a list.
|
||||||
@ -312,9 +365,9 @@ try {
|
|||||||
program.parse_args(argc, argv); // Example: ./main --input_files config.yml System.xml
|
program.parse_args(argc, argv); // Example: ./main --input_files config.yml System.xml
|
||||||
}
|
}
|
||||||
catch (const std::runtime_error& err) {
|
catch (const std::runtime_error& err) {
|
||||||
std::cout << err.what() << std::endl;
|
std::cerr << err.what() << std::endl;
|
||||||
std::cout << program;
|
std::cerr << program;
|
||||||
exit(0);
|
std::exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto files = program.get<std::vector<std::string>>("--input_files"); // {"config.yml", "System.xml"}
|
auto files = program.get<std::vector<std::string>>("--input_files"); // {"config.yml", "System.xml"}
|
||||||
@ -341,9 +394,9 @@ try {
|
|||||||
program.parse_args(argc, argv); // Example: ./main --query_point 3.5 4.7 9.2
|
program.parse_args(argc, argv); // Example: ./main --query_point 3.5 4.7 9.2
|
||||||
}
|
}
|
||||||
catch (const std::runtime_error& err) {
|
catch (const std::runtime_error& err) {
|
||||||
std::cout << err.what() << std::endl;
|
std::cerr << err.what() << std::endl;
|
||||||
std::cout << program;
|
std::cerr << program;
|
||||||
exit(0);
|
std::exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto query_point = program.get<std::vector<double>>("--query_point"); // {3.5, 4.7, 9.2}
|
auto query_point = program.get<std::vector<double>>("--query_point"); // {3.5, 4.7, 9.2}
|
||||||
@ -373,9 +426,9 @@ try {
|
|||||||
program.parse_args(argc, argv); // Example: ./main -abc 1.95 2.47
|
program.parse_args(argc, argv); // Example: ./main -abc 1.95 2.47
|
||||||
}
|
}
|
||||||
catch (const std::runtime_error& err) {
|
catch (const std::runtime_error& err) {
|
||||||
std::cout << err.what() << std::endl;
|
std::cerr << err.what() << std::endl;
|
||||||
std::cout << program;
|
std::cerr << program;
|
||||||
exit(0);
|
std::exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto a = program.get<bool>("-a"); // true
|
auto a = program.get<bool>("-a"); // true
|
||||||
@ -437,6 +490,27 @@ The grammar follows `std::from_chars`, but does not exactly duplicate it. For ex
|
|||||||
| 'u' | decimal (unsigned) |
|
| 'u' | decimal (unsigned) |
|
||||||
| 'x' or 'X' | hexadecimal (unsigned) |
|
| 'x' or 'X' | hexadecimal (unsigned) |
|
||||||
|
|
||||||
|
### Default Arguments
|
||||||
|
|
||||||
|
`argparse` provides predefined arguments and actions for `-h`/`--help` and `-v`/`--version`. These default actions exit the program after displaying a help or version message, respectively. These defaults arguments can be disabled during `ArgumentParser` creation so that you can handle these arguments in your own way. (Note that a program name and version must be included when choosing default arguments.)
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
argparse::ArgumentParser program("test", "1.0", default_arguments::none);
|
||||||
|
|
||||||
|
program.add_argument("-h", "--help")
|
||||||
|
.action([=](const std::string& s) {
|
||||||
|
std::cout << help().str();
|
||||||
|
})
|
||||||
|
.default_value(false)
|
||||||
|
.help("shows help message")
|
||||||
|
.implicit_value(true)
|
||||||
|
.nargs(0);
|
||||||
|
```
|
||||||
|
|
||||||
|
The above code snippet outputs a help message and continues to run. It does not support a `--version` argument.
|
||||||
|
|
||||||
|
The default is `default_arguments::all` for included arguments. No default arguments will be added with `default_arguments::none`. `default_arguments::help` and `default_arguments::version` will individually add `--help` and `--version`.
|
||||||
|
|
||||||
### Gathering Remaining Arguments
|
### Gathering Remaining Arguments
|
||||||
|
|
||||||
`argparse` supports gathering "remaining" arguments at the end of the command, e.g., for use in a compiler:
|
`argparse` supports gathering "remaining" arguments at the end of the command, e.g., for use in a compiler:
|
||||||
@ -457,9 +531,9 @@ try {
|
|||||||
program.parse_args(argc, argv);
|
program.parse_args(argc, argv);
|
||||||
}
|
}
|
||||||
catch (const std::runtime_error& err) {
|
catch (const std::runtime_error& err) {
|
||||||
std::cout << err.what() << std::endl;
|
std::cerr << err.what() << std::endl;
|
||||||
std::cout << program;
|
std::cerr << program;
|
||||||
exit(0);
|
std::exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -504,9 +578,9 @@ try {
|
|||||||
program.parse_args(argc, argv);
|
program.parse_args(argc, argv);
|
||||||
}
|
}
|
||||||
catch (const std::runtime_error& err) {
|
catch (const std::runtime_error& err) {
|
||||||
std::cout << err.what() << std::endl;
|
std::cerr << err.what() << std::endl;
|
||||||
std::cout << program;
|
std::cerr << program;
|
||||||
exit(0);
|
std::exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto output_filename = program.get<std::string>("-o");
|
auto output_filename = program.get<std::string>("-o");
|
||||||
@ -586,9 +660,9 @@ try {
|
|||||||
program.parse_args({"./test", "config.json"});
|
program.parse_args({"./test", "config.json"});
|
||||||
}
|
}
|
||||||
catch (const std::runtime_error& err) {
|
catch (const std::runtime_error& err) {
|
||||||
std::cout << err.what() << std::endl;
|
std::cerr << err.what() << std::endl;
|
||||||
std::cout << program;
|
std::cerr << program;
|
||||||
exit(0);
|
std::exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
nlohmann::json config = program.get<nlohmann::json>("config");
|
nlohmann::json config = program.get<nlohmann::json>("config");
|
||||||
@ -622,9 +696,9 @@ try {
|
|||||||
program.parse_args(argc, argv);
|
program.parse_args(argc, argv);
|
||||||
}
|
}
|
||||||
catch (const std::runtime_error& err) {
|
catch (const std::runtime_error& err) {
|
||||||
std::cout << err.what() << std::endl;
|
std::cerr << err.what() << std::endl;
|
||||||
std::cout << program;
|
std::cerr << program;
|
||||||
exit(0);
|
std::exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto numbers = program.get<std::vector<int>>("numbers"); // {1, 2, 3}
|
auto numbers = program.get<std::vector<int>>("numbers"); // {1, 2, 3}
|
||||||
@ -664,9 +738,9 @@ try {
|
|||||||
program.parse_args(argc, argv);
|
program.parse_args(argc, argv);
|
||||||
}
|
}
|
||||||
catch (const std::runtime_error& err) {
|
catch (const std::runtime_error& err) {
|
||||||
std::cout << err.what() << std::endl;
|
std::cerr << err.what() << std::endl;
|
||||||
std::cout << program;
|
std::cerr << program;
|
||||||
exit(0);
|
std::exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto input = program.get("input");
|
auto input = program.get("input");
|
||||||
@ -707,6 +781,24 @@ Thanks goes to these wonderful people:
|
|||||||
<tr>
|
<tr>
|
||||||
<td align="center"><a href="https://github.com/MU001999"><img src="https://avatars3.githubusercontent.com/u/21022101?s=400&v=4" width="100px;" alt="mupp"/><br /><sub><b>mupp</b></sub></a></td>
|
<td align="center"><a href="https://github.com/MU001999"><img src="https://avatars3.githubusercontent.com/u/21022101?s=400&v=4" width="100px;" alt="mupp"/><br /><sub><b>mupp</b></sub></a></td>
|
||||||
<td align="center"><a href="https://github.com/CrustyAuklet"><img src="https://avatars2.githubusercontent.com/u/9755578?s=400&v=4" width="100px;" alt="Ethan Slattery"/><br /><sub><b>Ethan Slattery</b></sub></a></td>
|
<td align="center"><a href="https://github.com/CrustyAuklet"><img src="https://avatars2.githubusercontent.com/u/9755578?s=400&v=4" width="100px;" alt="Ethan Slattery"/><br /><sub><b>Ethan Slattery</b></sub></a></td>
|
||||||
|
<td align="center"><a href="https://github.com/skrobinson"><img src="https://avatars.githubusercontent.com/u/49722376?v=4" width="100px;" alt="skrobinson"/><br /><sub><b>skrobinson</b></sub></a></td>
|
||||||
|
<td align="center"><a href="https://github.com/cekc"><img src="https://avatars.githubusercontent.com/u/31835620?v=4" width="100px;" alt="Mike Zozu"/><br /><sub><b>Mike Zozu</b></sub></a></td>
|
||||||
|
<td align="center"><a href="https://github.com/Chuvi-w"><img src="https://avatars.githubusercontent.com/u/3652659?v=4" width="100px;" alt="Chuvi-w"/><br /><sub><b>Chuvi-w</b></sub></a></td>
|
||||||
|
<td align="center"><a href="https://github.com/KOLANICH"><img src="https://avatars.githubusercontent.com/u/240344?v=4" width="100px;" alt="KOLANICH"/><br /><sub><b>KOLANICH</b></sub></a></td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td align="center"><a href="https://github.com/Bedzior"><img src="https://avatars.githubusercontent.com/u/19894088?v=4" width="100px;" alt="Rafał Będźkowski"/><br /><sub><b>Rafał Będźkowski</b></sub></a></td>
|
||||||
|
<td align="center"><a href="https://github.com/hokacci"><img src="https://avatars.githubusercontent.com/u/47231909?v=4" width="100px;" alt="Yoshihiro Hokazono"/><br /><sub><b>Yoshihiro Hokazono</b></sub></a></td>
|
||||||
|
<td align="center"><a href="https://github.com/qoelet"><img src="https://avatars.githubusercontent.com/u/115877?v=4" width="100px;" alt="Kenny Shen"/><br /><sub><b>Kenny Shen</b></sub></a></td>
|
||||||
|
<td align="center"><a href="https://github.com/MU001999"><img src="https://avatars.githubusercontent.com/u/21022101?v=4" width="100px;" alt="The 42nd Mu00"/><br /><sub><b>The 42nd Mu00</b></sub></a></td>
|
||||||
|
<td align="center"><a href="https://github.com/Ubpa"><img src="https://avatars.githubusercontent.com/u/15104079?v=4" width="100px;" alt="Ubpa"/><br /><sub><b>Ubpa</b></sub></a></td>
|
||||||
|
<td align="center"><a href="https://github.com/kfsone"><img src="https://avatars.githubusercontent.com/u/323009?v=4" width="100px;" alt="Oliver Smith"/><br /><sub><b>Oliver Smith</b></sub></a></td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td align="center"><a href="https://github.com/JadeMatrix"><img src="https://avatars.githubusercontent.com/u/1753533?v=4" width="100px;" alt="Joseph Durel"/><br /><sub><b>Joseph Durel</b></sub></a></td>
|
||||||
|
<td align="center"><a href="https://github.com/rysson"><img src="https://avatars.githubusercontent.com/u/5898312?v=4" width="100px;" alt="rysson"/><br /><sub><b>rysson</b></sub></a></td>
|
||||||
|
<td align="center"><a href="https://github.com/aashwinr"><img src="https://avatars.githubusercontent.com/u/78666414?v=4" width="100px;" alt="aashwinr"/><br /><sub><b>aashwinr</b></sub></a></td>
|
||||||
|
<td align="center"><a href="https://github.com/bufferbase"><img src="https://avatars.githubusercontent.com/u/65209648?v=4" width="100px;" alt="bufferbase"/><br /><sub><b>bufferbase</b></sub></a></td>
|
||||||
</tr>
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
|
@ -2,7 +2,7 @@ from conans import ConanFile
|
|||||||
|
|
||||||
class ArgparseConan(ConanFile):
|
class ArgparseConan(ConanFile):
|
||||||
name = "argparse"
|
name = "argparse"
|
||||||
version = "1.0"
|
version = "2.5"
|
||||||
exports_sources = "include/argparse.hpp"
|
exports_sources = "include/argparse.hpp"
|
||||||
no_copy_source = True
|
no_copy_source = True
|
||||||
|
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -30,6 +30,7 @@ file(GLOB ARGPARSE_TEST_SOURCES
|
|||||||
test_compound_arguments.cpp
|
test_compound_arguments.cpp
|
||||||
test_container_arguments.cpp
|
test_container_arguments.cpp
|
||||||
test_const_correct.cpp
|
test_const_correct.cpp
|
||||||
|
test_default_args.cpp
|
||||||
test_get.cpp
|
test_get.cpp
|
||||||
test_help.cpp
|
test_help.cpp
|
||||||
test_invalid_arguments.cpp
|
test_invalid_arguments.cpp
|
||||||
|
2719
test/doctest.hpp
2719
test/doctest.hpp
File diff suppressed because it is too large
Load Diff
@ -146,3 +146,27 @@ TEST_CASE("Users can use actions on remaining arguments" *
|
|||||||
program.parse_args({"concat", "a", "-b", "-c", "--d"});
|
program.parse_args({"concat", "a", "-b", "-c", "--d"});
|
||||||
REQUIRE(result == "a-b-c--d");
|
REQUIRE(result == "a-b-c--d");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Users can run actions on parameterless optional arguments" *
|
||||||
|
test_suite("actions")) {
|
||||||
|
argparse::ArgumentParser program("test");
|
||||||
|
|
||||||
|
GIVEN("a flag argument with a counting action") {
|
||||||
|
int count = 0;
|
||||||
|
program.add_argument("-V", "--verbose")
|
||||||
|
.action([&](const auto &) { ++count; })
|
||||||
|
.append()
|
||||||
|
.default_value(false)
|
||||||
|
.implicit_value(true)
|
||||||
|
.nargs(0);
|
||||||
|
|
||||||
|
WHEN("the flag is repeated") {
|
||||||
|
program.parse_args({"test", "-VVVV"});
|
||||||
|
|
||||||
|
THEN("the count increments once per use") {
|
||||||
|
REQUIRE(program.get<bool>("-V"));
|
||||||
|
REQUIRE(count == 4);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
19
test/test_default_args.cpp
Normal file
19
test/test_default_args.cpp
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
#include <argparse/argparse.hpp>
|
||||||
|
#include <doctest.hpp>
|
||||||
|
|
||||||
|
using doctest::test_suite;
|
||||||
|
|
||||||
|
TEST_CASE("Include all default arguments" * test_suite("default_args")) {
|
||||||
|
argparse::ArgumentParser parser("test");
|
||||||
|
auto help_msg { parser.help().str() };
|
||||||
|
REQUIRE(help_msg.find("shows help message") != std::string::npos);
|
||||||
|
REQUIRE(help_msg.find("prints version information") != std::string::npos);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Do not include default arguments" * test_suite("default_args")) {
|
||||||
|
argparse::ArgumentParser parser("test", "1.0",
|
||||||
|
argparse::default_arguments::none);
|
||||||
|
parser.parse_args({"test"});
|
||||||
|
REQUIRE_THROWS_AS(parser.get("--help"), std::logic_error);
|
||||||
|
REQUIRE_THROWS_AS(parser.get("--version"), std::logic_error);
|
||||||
|
}
|
@ -1,5 +1,6 @@
|
|||||||
#include <argparse/argparse.hpp>
|
#include <argparse/argparse.hpp>
|
||||||
#include <doctest.hpp>
|
#include <doctest.hpp>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
using doctest::test_suite;
|
using doctest::test_suite;
|
||||||
|
|
||||||
@ -51,3 +52,26 @@ TEST_CASE("Users can override the help options" * test_suite("help")) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Users can disable default -h/--help" * test_suite("help")) {
|
||||||
|
argparse::ArgumentParser program("test", "1.0",
|
||||||
|
argparse::default_arguments::version);
|
||||||
|
REQUIRE_THROWS_AS(program.parse_args({"test", "-h"}), std::runtime_error);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Users can replace default -h/--help" * test_suite("help")) {
|
||||||
|
argparse::ArgumentParser program("test", "1.0",
|
||||||
|
argparse::default_arguments::version);
|
||||||
|
std::stringstream buffer;
|
||||||
|
program.add_argument("-h", "--help")
|
||||||
|
.action([&](const auto &) {
|
||||||
|
buffer << program;
|
||||||
|
})
|
||||||
|
.default_value(false)
|
||||||
|
.implicit_value(true)
|
||||||
|
.nargs(0);
|
||||||
|
|
||||||
|
REQUIRE(buffer.str().empty());
|
||||||
|
program.parse_args({"test", "--help"});
|
||||||
|
REQUIRE_FALSE(buffer.str().empty());
|
||||||
|
}
|
||||||
|
@ -36,5 +36,6 @@ TEST_CASE("Parse unknown optional argument" *
|
|||||||
.scan<'u', unsigned long long>()
|
.scan<'u', unsigned long long>()
|
||||||
.help("memory in MB to give the VMM when loading");
|
.help("memory in MB to give the VMM when loading");
|
||||||
|
|
||||||
REQUIRE_THROWS(bfm.parse_args({ "./test.exe", "-om" }));
|
REQUIRE_THROWS_WITH_AS(bfm.parse_args({"./test.exe", "-om"}),
|
||||||
|
"Unknown argument: -om", std::runtime_error);
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,39 @@
|
|||||||
#include <doctest.hpp>
|
#include <doctest.hpp>
|
||||||
#include <argparse/argparse.hpp>
|
#include <argparse/argparse.hpp>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
using doctest::test_suite;
|
using doctest::test_suite;
|
||||||
|
|
||||||
TEST_CASE("Users can print version and exit" * test_suite("version")) {
|
TEST_CASE("Users can print version and exit" * test_suite("version")
|
||||||
|
* doctest::skip()) {
|
||||||
argparse::ArgumentParser program("cli-test", "1.9.0");
|
argparse::ArgumentParser program("cli-test", "1.9.0");
|
||||||
program.add_argument("-d", "--dir")
|
program.add_argument("-d", "--dir")
|
||||||
.required();
|
.required();
|
||||||
program.parse_args( { "test", "--version" });
|
program.parse_args( { "test", "--version" });
|
||||||
REQUIRE(program.get("--version") == "1.9.0");
|
REQUIRE(program.get("--version") == "1.9.0");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Users can disable default -v/--version" * test_suite("version")) {
|
||||||
|
argparse::ArgumentParser program("test", "1.0",
|
||||||
|
argparse::default_arguments::help);
|
||||||
|
REQUIRE_THROWS_WITH_AS(program.parse_args({"test", "--version"}),
|
||||||
|
"Unknown argument: --version", std::runtime_error);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Users can replace default -v/--version" * test_suite("version")) {
|
||||||
|
std::string version { "3.1415" };
|
||||||
|
argparse::ArgumentParser program("test", version,
|
||||||
|
argparse::default_arguments::help);
|
||||||
|
std::stringstream buffer;
|
||||||
|
program.add_argument("-v", "--version")
|
||||||
|
.action([&](const auto &) {
|
||||||
|
buffer << version;
|
||||||
|
})
|
||||||
|
.default_value(true)
|
||||||
|
.implicit_value(false)
|
||||||
|
.nargs(0);
|
||||||
|
|
||||||
|
REQUIRE(buffer.str().empty());
|
||||||
|
program.parse_args({"test", "--version"});
|
||||||
|
REQUIRE_FALSE(buffer.str().empty());
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user