1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
|
#ifdef WITH_MODULE
import argparse;
#else
#include <argparse/argparse.hpp>
#endif
#include <doctest.hpp>
using doctest::test_suite;
TEST_CASE("Parse toggle arguments with default value" *
test_suite("optional_arguments")) {
argparse::ArgumentParser program("test");
program.add_argument("--verbose", "-v")
.default_value(false)
.implicit_value(true);
program.parse_args({"./test.exe"});
REQUIRE(program.get<bool>("--verbose") == false);
REQUIRE(program["--verbose"] == false);
}
TEST_CASE("Argument '-' is not an optional argument" *
test_suite("optional_arguments")) {
argparse::ArgumentParser program("test");
program.add_argument("input");
program.parse_args({"./test.exe", "-"});
REQUIRE(program.get<std::string>("input") == "-");
}
TEST_CASE("Argument '-' is not an optional argument but '-l' is" *
test_suite("optional_arguments")) {
argparse::ArgumentParser program("test");
program.add_argument("-l").flag();
program.add_argument("input");
program.parse_args({"./test.exe", "-l", "-"});
REQUIRE(program.get<bool>("-l") == true);
REQUIRE(program.get<std::string>("input") == "-");
}
TEST_CASE("Argument '-l' is an optional argument but '-' is not" *
test_suite("optional_arguments")) {
argparse::ArgumentParser program("test");
program.add_argument("-l").flag();
program.add_argument("input");
program.parse_args({"./test.exe", "-", "-l"});
REQUIRE(program.get<bool>("-l") == true);
REQUIRE(program.get<std::string>("input") == "-");
}
TEST_CASE("Parse toggle arguments with implicit value" *
test_suite("optional_arguments")) {
argparse::ArgumentParser program("test");
program.add_argument("--verbose").flag();
program.parse_args({"./test.exe", "--verbose"});
REQUIRE(program.get<bool>("--verbose") == true);
REQUIRE(program["--verbose"] == true);
REQUIRE(program["--verbose"] != false);
}
TEST_CASE("Parse multiple toggle arguments with implicit values" *
test_suite("optional_arguments")) {
argparse::ArgumentParser program("test");
program.add_argument("-a").flag();
program.add_argument("-u").flag();
program.add_argument("-x").flag();
program.parse_args({"./test.exe", "-a", "-x"});
REQUIRE(program.get<bool>("-a") == true);
REQUIRE(program.get<bool>("-u") == false);
REQUIRE(program.get<bool>("-x") == true);
}
TEST_CASE("Parse optional arguments of many values" *
test_suite("optional_arguments")) {
GIVEN("a program that accepts an optional argument of many values") {
argparse::ArgumentParser program("test");
program.add_argument("-i").remaining().scan<'i', int>();
WHEN("provided no argument") {
THEN("the program accepts it but gets nothing") {
REQUIRE_NOTHROW(program.parse_args({"test"}));
REQUIRE_THROWS_AS(program.get<std::vector<int>>("-i"),
std::logic_error);
}
}
WHEN("provided remaining arguments follow the option") {
program.parse_args({"test", "-i", "-42", "8", "100", "300"});
THEN("the optional parameter consumes all of them") {
auto inputs = program.get<std::vector<int>>("-i");
REQUIRE(inputs.size() == 4);
REQUIRE(inputs[0] == -42);
REQUIRE(inputs[1] == 8);
REQUIRE(inputs[2] == 100);
REQUIRE(inputs[3] == 300);
}
}
}
}
TEST_CASE("Parse 2 optional arguments of many values" *
test_suite("optional_arguments")) {
GIVEN("a program that accepts 2 optional arguments of many values") {
argparse::ArgumentParser program("test");
program.add_argument("-i")
.nargs(argparse::nargs_pattern::any)
.scan<'i', int>();
program.add_argument("-s").nargs(argparse::nargs_pattern::any);
WHEN("provided no argument") {
THEN("the program accepts it and gets empty container") {
REQUIRE_NOTHROW(program.parse_args({"test"}));
auto i = program.get<std::vector<int>>("-i");
REQUIRE(i.size() == 0);
auto s = program.get<std::vector<std::string>>("-s");
REQUIRE(s.size() == 0);
}
}
WHEN("provided 2 options with many arguments") {
program.parse_args({"test", "-i", "-42", "8", "100", "300", "-s", "ok",
"this", "works"});
THEN("the optional parameter consumes each arguments") {
auto i = program.get<std::vector<int>>("-i");
REQUIRE(i.size() == 4);
REQUIRE(i[0] == -42);
REQUIRE(i[1] == 8);
REQUIRE(i[2] == 100);
REQUIRE(i[3] == 300);
auto s = program.get<std::vector<std::string>>("-s");
REQUIRE(s.size() == 3);
REQUIRE(s[0] == "ok");
REQUIRE(s[1] == "this");
REQUIRE(s[2] == "works");
}
}
}
}
TEST_CASE("Parse an optional argument of many values"
" and a positional argument of many values" *
test_suite("optional_arguments")) {
GIVEN("a program that accepts an optional argument of many values"
" and a positional argument of many values") {
argparse::ArgumentParser program("test");
program.add_argument("-s").nargs(argparse::nargs_pattern::any);
program.add_argument("input").nargs(argparse::nargs_pattern::any);
WHEN("provided no argument") {
program.parse_args({"test"});
THEN("the program accepts it and gets empty containers") {
auto s = program.get<std::vector<std::string>>("-s");
REQUIRE(s.size() == 0);
auto input = program.get<std::vector<std::string>>("input");
REQUIRE(input.size() == 0);
}
}
WHEN("provided many arguments followed by an option with many arguments") {
program.parse_args({"test", "foo", "bar", "-s", "ok", "this", "works"});
THEN("the parameters consume each arguments") {
auto s = program.get<std::vector<std::string>>("-s");
REQUIRE(s.size() == 3);
REQUIRE(s[0] == "ok");
REQUIRE(s[1] == "this");
REQUIRE(s[2] == "works");
auto input = program.get<std::vector<std::string>>("input");
REQUIRE(input.size() == 2);
REQUIRE(input[0] == "foo");
REQUIRE(input[1] == "bar");
}
}
}
}
TEST_CASE("Parse arguments of different types" *
test_suite("optional_arguments")) {
using namespace std::literals;
argparse::ArgumentParser program("test");
program.add_argument("--this-argument-is-longer-than-any-sso-buffer-that-"
"makes-sense-unless-your-cache-line-is-this-long"s);
REQUIRE_NOTHROW(program.parse_args({"test"}));
program.add_argument("-string"s, "-string-view"sv, "-builtin")
.default_value(false)
.implicit_value(true);
program.parse_args({"test", "-string-view"});
REQUIRE(program["-string"sv] == true);
REQUIRE(program["-string-view"] == true);
REQUIRE(program["-builtin"s] == true);
}
|