You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
635 lines
21 KiB
635 lines
21 KiB
#include <string> |
|
#include <vector> |
|
|
|
#include "macro.h" |
|
#include "testcase.h" |
|
#include "testsuite.h" |
|
#include "util/argparser.h" |
|
|
|
bool runParser(std::vector<const char*> arguments, std::function<void(Util::ArgParser&)> initializer = {}) |
|
{ |
|
stdout = Test::TestSuite::the().outputNull(); |
|
|
|
Util::ArgParser parser; |
|
if (initializer) { |
|
initializer(parser); |
|
} |
|
|
|
arguments.insert(arguments.begin(), "app"); |
|
auto result = parser.parse(arguments.size(), arguments.data()); |
|
|
|
stdout = Test::TestSuite::the().outputStd(); |
|
return result; |
|
} |
|
|
|
// ----------------------------------------- |
|
|
|
TEST_CASE(NoArguments) |
|
{ |
|
auto result = runParser({}); |
|
EXPECT_EQ(result, true); |
|
} |
|
|
|
// ----------------------------------------- |
|
|
|
TEST_CASE(NonExistentArguments) |
|
{ |
|
auto result = runParser({ "-n", "-e" }); |
|
EXPECT_EQ(result, false); |
|
|
|
result = runParser({ "--non", "--existent" }); |
|
EXPECT_EQ(result, false); |
|
|
|
result = runParser({ "-n", "-e", "--non", "--existent" }); |
|
EXPECT_EQ(result, false); |
|
|
|
result = runParser({ "no", "handling" }); |
|
EXPECT_EQ(result, false); |
|
} |
|
|
|
// ----------------------------------------- |
|
|
|
TEST_CASE(BoolOptions) |
|
{ |
|
// Short option |
|
bool boolOpt1 = false; |
|
auto result = runParser({ "-b" }, [&](auto& parser) { |
|
parser.addOption(boolOpt1, 'b', nullptr, nullptr, nullptr); |
|
}); |
|
EXPECT_EQ(result, true); |
|
EXPECT_EQ(boolOpt1, true); |
|
|
|
// Short option, not given |
|
boolOpt1 = false; |
|
result = runParser({}, [&](auto& parser) { |
|
parser.addOption(boolOpt1, 'b', nullptr, nullptr, nullptr); |
|
}); |
|
EXPECT_EQ(result, true); |
|
EXPECT_EQ(boolOpt1, false); |
|
|
|
// Long option |
|
boolOpt1 = false; |
|
result = runParser({ "--bool" }, [&](auto& parser) { |
|
parser.addOption(boolOpt1, '\0', "bool", nullptr, nullptr); |
|
}); |
|
EXPECT_EQ(result, true); |
|
EXPECT_EQ(boolOpt1, true); |
|
|
|
// Long option, not given |
|
boolOpt1 = false; |
|
result = runParser({}, [&](auto& parser) { |
|
parser.addOption(boolOpt1, '\0', "bool", nullptr, nullptr); |
|
}); |
|
EXPECT_EQ(result, true); |
|
EXPECT_EQ(boolOpt1, false); |
|
|
|
// Allow both short and long option, provide short |
|
boolOpt1 = false; |
|
result = runParser({ "-b" }, [&](auto& parser) { |
|
parser.addOption(boolOpt1, 'b', "bool", nullptr, nullptr); |
|
}); |
|
EXPECT_EQ(result, true); |
|
EXPECT_EQ(boolOpt1, true); |
|
|
|
// Allow both short and long option, provide long |
|
boolOpt1 = false; |
|
result = runParser({ "--bool" }, [&](auto& parser) { |
|
parser.addOption(boolOpt1, 'b', "bool", nullptr, nullptr); |
|
}); |
|
EXPECT_EQ(result, true); |
|
EXPECT_EQ(boolOpt1, true); |
|
|
|
// Allow both short and long option, provide both |
|
boolOpt1 = false; |
|
result = runParser({ "-b", "--bool" }, [&](auto& parser) { |
|
parser.addOption(boolOpt1, 'b', "bool", nullptr, nullptr); |
|
}); |
|
EXPECT_EQ(result, true); |
|
EXPECT_EQ(boolOpt1, true); |
|
} |
|
|
|
// ----------------------------------------- |
|
|
|
TEST_CASE(SingleRequiredStringOptions) |
|
{ |
|
// Single required string short option |
|
std::string stringOpt1 = ""; |
|
auto result = runParser({ "-s", "my-required-argument" }, [&](auto& parser) { |
|
parser.addOption(stringOpt1, 's', nullptr, nullptr, nullptr, nullptr, Util::ArgParser::Required::Yes); |
|
}); |
|
EXPECT_EQ(result, true); |
|
EXPECT_EQ(stringOpt1, "my-required-argument"); |
|
|
|
// Single required string short option, given directly after |
|
stringOpt1 = ""; |
|
result = runParser({ "-smy-required-argument" }, [&](auto& parser) { |
|
parser.addOption(stringOpt1, 's', nullptr, nullptr, nullptr, nullptr, Util::ArgParser::Required::Yes); |
|
}); |
|
EXPECT_EQ(result, true); |
|
EXPECT_EQ(stringOpt1, "my-required-argument"); |
|
|
|
// Single required string short option, empty given |
|
stringOpt1 = ""; |
|
result = runParser({ "-s" }, [&](auto& parser) { |
|
parser.addOption(stringOpt1, 's', nullptr, nullptr, nullptr, nullptr, Util::ArgParser::Required::Yes); |
|
}); |
|
EXPECT_EQ(result, false); |
|
EXPECT_EQ(stringOpt1, ""); |
|
|
|
// Single required string short option, not given |
|
stringOpt1 = ""; |
|
result = runParser({}, [&](auto& parser) { |
|
parser.addOption(stringOpt1, 's', nullptr, nullptr, nullptr, nullptr, Util::ArgParser::Required::Yes); |
|
}); |
|
EXPECT_EQ(result, true); |
|
EXPECT_EQ(stringOpt1, ""); |
|
|
|
// Single required string long option |
|
stringOpt1 = ""; |
|
result = runParser({ "--string", "my-required-argument" }, [&](auto& parser) { |
|
parser.addOption(stringOpt1, '\0', "string", nullptr, nullptr, nullptr, Util::ArgParser::Required::Yes); |
|
}); |
|
EXPECT_EQ(result, true); |
|
EXPECT_EQ(stringOpt1, "my-required-argument"); |
|
|
|
// Single required string long option, given directly after |
|
stringOpt1 = ""; |
|
result = runParser({ "--string=my-required-argument" }, [&](auto& parser) { |
|
parser.addOption(stringOpt1, '\0', "string", nullptr, nullptr, nullptr, Util::ArgParser::Required::Yes); |
|
}); |
|
EXPECT_EQ(result, true); |
|
EXPECT_EQ(stringOpt1, "my-required-argument"); |
|
|
|
// Single required string long option, empty given |
|
stringOpt1 = ""; |
|
result = runParser({ "--string" }, [&](auto& parser) { |
|
parser.addOption(stringOpt1, '\0', "string", nullptr, nullptr, nullptr, Util::ArgParser::Required::Yes); |
|
}); |
|
EXPECT_EQ(result, false); |
|
EXPECT_EQ(stringOpt1, ""); |
|
|
|
// Single required string long option, not given |
|
stringOpt1 = ""; |
|
result = runParser({}, [&](auto& parser) { |
|
parser.addOption(stringOpt1, '\0', "string", nullptr, nullptr, nullptr, Util::ArgParser::Required::Yes); |
|
}); |
|
EXPECT_EQ(result, true); |
|
EXPECT_EQ(stringOpt1, ""); |
|
} |
|
|
|
// ----------------------------------------- |
|
|
|
TEST_CASE(SingleOptionalStringOptions) |
|
{ |
|
// Single optional string short option |
|
std::string stringOpt1 = ""; |
|
auto result = runParser({ "-s", "my-optional-argument" }, [&](auto& parser) { |
|
parser.addOption(stringOpt1, 's', nullptr, nullptr, nullptr, nullptr, Util::ArgParser::Required::Optional); |
|
}); |
|
EXPECT_EQ(result, true); |
|
EXPECT_EQ(stringOpt1, ""); |
|
|
|
// Single optional string short option, given directly after |
|
stringOpt1 = ""; |
|
result = runParser({ "-smy-optional-argument" }, [&](auto& parser) { |
|
parser.addOption(stringOpt1, 's', nullptr, nullptr, nullptr, nullptr, Util::ArgParser::Required::Optional); |
|
}); |
|
EXPECT_EQ(result, true); |
|
EXPECT_EQ(stringOpt1, "my-optional-argument"); |
|
|
|
// Single optional string short option, empty given |
|
stringOpt1 = ""; |
|
result = runParser({ "-s" }, [&](auto& parser) { |
|
parser.addOption(stringOpt1, 's', nullptr, nullptr, nullptr, nullptr, Util::ArgParser::Required::Optional); |
|
}); |
|
EXPECT_EQ(result, true); |
|
EXPECT_EQ(stringOpt1, ""); |
|
|
|
// Single optional string short option, not given |
|
stringOpt1 = ""; |
|
result = runParser({}, [&](auto& parser) { |
|
parser.addOption(stringOpt1, 's', nullptr, nullptr, nullptr, nullptr, Util::ArgParser::Required::Optional); |
|
}); |
|
EXPECT_EQ(result, true); |
|
EXPECT_EQ(stringOpt1, ""); |
|
|
|
// Single optional string long option |
|
stringOpt1 = ""; |
|
result = runParser({ "--string", "my-optional-argument" }, [&](auto& parser) { |
|
parser.addOption(stringOpt1, '\0', "string", nullptr, nullptr, nullptr, Util::ArgParser::Required::Optional); |
|
}); |
|
EXPECT_EQ(result, true); |
|
EXPECT_EQ(stringOpt1, ""); |
|
|
|
// Single optional string long option, given directly after |
|
stringOpt1 = ""; |
|
result = runParser({ "--string=my-optional-argument" }, [&](auto& parser) { |
|
parser.addOption(stringOpt1, '\0', "string", nullptr, nullptr, nullptr, Util::ArgParser::Required::Optional); |
|
}); |
|
EXPECT_EQ(result, true); |
|
EXPECT_EQ(stringOpt1, "my-optional-argument"); |
|
|
|
// Single optional string long option, empty given |
|
stringOpt1 = ""; |
|
result = runParser({ "--string" }, [&](auto& parser) { |
|
parser.addOption(stringOpt1, '\0', "string", nullptr, nullptr, nullptr, Util::ArgParser::Required::Optional); |
|
}); |
|
EXPECT_EQ(result, true); |
|
EXPECT_EQ(stringOpt1, ""); |
|
|
|
// Single optional string long option, not given |
|
stringOpt1 = ""; |
|
result = runParser({}, [&](auto& parser) { |
|
parser.addOption(stringOpt1, '\0', "string", nullptr, nullptr, nullptr, Util::ArgParser::Required::Optional); |
|
}); |
|
EXPECT_EQ(result, true); |
|
EXPECT_EQ(stringOpt1, ""); |
|
} |
|
|
|
// ----------------------------------------- |
|
|
|
TEST_CASE(SingleNonRequiredStringOptions) |
|
{ |
|
// Single non-required string short option |
|
std::string stringOpt1 = ""; |
|
auto result = runParser({ "-s", "my-non-required-argument" }, [&](auto& parser) { |
|
parser.addOption(stringOpt1, 's', nullptr, nullptr, nullptr, nullptr, Util::ArgParser::Required::No); |
|
}); |
|
EXPECT_EQ(result, true); |
|
EXPECT_EQ(stringOpt1, ""); |
|
|
|
// Single non-required string short option, given directly after |
|
stringOpt1 = ""; |
|
result = runParser({ "-smy-non-required-argument" }, [&](auto& parser) { |
|
parser.addOption(stringOpt1, 's', nullptr, nullptr, nullptr, nullptr, Util::ArgParser::Required::No); |
|
}); |
|
EXPECT_EQ(result, false); |
|
EXPECT_EQ(stringOpt1, ""); |
|
|
|
// Single non-required string short option, empty given |
|
stringOpt1 = ""; |
|
result = runParser({ "-s" }, [&](auto& parser) { |
|
parser.addOption(stringOpt1, 's', nullptr, nullptr, nullptr, nullptr, Util::ArgParser::Required::No); |
|
}); |
|
EXPECT_EQ(result, true); |
|
EXPECT_EQ(stringOpt1, ""); |
|
|
|
// Single non-required string short option, not given |
|
stringOpt1 = ""; |
|
result = runParser({}, [&](auto& parser) { |
|
parser.addOption(stringOpt1, 's', nullptr, nullptr, nullptr, nullptr, Util::ArgParser::Required::No); |
|
}); |
|
EXPECT_EQ(result, true); |
|
EXPECT_EQ(stringOpt1, ""); |
|
|
|
// Single non-required string long option |
|
stringOpt1 = ""; |
|
result = runParser({ "--string", "my-non-required-argument" }, [&](auto& parser) { |
|
parser.addOption(stringOpt1, '\0', "string", nullptr, nullptr, nullptr, Util::ArgParser::Required::No); |
|
}); |
|
EXPECT_EQ(result, true); |
|
EXPECT_EQ(stringOpt1, ""); |
|
|
|
// Single non-required string long option, given directly after |
|
stringOpt1 = ""; |
|
result = runParser({ "--string=my-non-required-argument" }, [&](auto& parser) { |
|
parser.addOption(stringOpt1, '\0', "string", nullptr, nullptr, nullptr, Util::ArgParser::Required::No); |
|
}); |
|
EXPECT_EQ(result, false); |
|
EXPECT_EQ(stringOpt1, ""); |
|
|
|
// Single non-required string long option, empty given |
|
stringOpt1 = ""; |
|
result = runParser({ "--string" }, [&](auto& parser) { |
|
parser.addOption(stringOpt1, '\0', "string", nullptr, nullptr, nullptr, Util::ArgParser::Required::No); |
|
}); |
|
EXPECT_EQ(result, true); |
|
EXPECT_EQ(stringOpt1, ""); |
|
|
|
// Single non-required string long option, not given |
|
stringOpt1 = ""; |
|
result = runParser({}, [&](auto& parser) { |
|
parser.addOption(stringOpt1, '\0', "string", nullptr, nullptr, nullptr, Util::ArgParser::Required::No); |
|
}); |
|
EXPECT_EQ(result, true); |
|
EXPECT_EQ(stringOpt1, ""); |
|
} |
|
|
|
// ----------------------------------------- |
|
|
|
TEST_CASE(NumberOptions) |
|
{ |
|
// Required int short option |
|
int intOpt1 = 0; |
|
auto result = runParser({ "-i", "2147483647" }, [&](auto& parser) { |
|
parser.addOption(intOpt1, 'i', nullptr, nullptr, nullptr, nullptr, Util::ArgParser::Required::Yes); |
|
}); |
|
EXPECT_EQ(result, true); |
|
EXPECT_EQ(intOpt1, 2147483647); |
|
|
|
// Required int short option, overflown value given |
|
intOpt1 = 0; |
|
result = runParser({ "-i", "2147483648" }, [&](auto& parser) { |
|
parser.addOption(intOpt1, 'i', nullptr, nullptr, nullptr, nullptr, Util::ArgParser::Required::Yes); |
|
}); |
|
EXPECT_EQ(result, false); |
|
EXPECT_EQ(intOpt1, 0); |
|
|
|
// Required int short option, empty given |
|
intOpt1 = 0; |
|
result = runParser({ "-i" }, [&](auto& parser) { |
|
parser.addOption(intOpt1, 'i', nullptr, nullptr, nullptr, nullptr, Util::ArgParser::Required::Yes); |
|
}); |
|
EXPECT_EQ(result, false); |
|
EXPECT_EQ(intOpt1, 0); |
|
|
|
// Required unsigned int short option |
|
unsigned int unsignedIntOpt1 = 0; |
|
result = runParser({ "-u", "4294967295" }, [&](auto& parser) { |
|
parser.addOption(unsignedIntOpt1, 'u', nullptr, nullptr, nullptr, nullptr, Util::ArgParser::Required::Yes); |
|
}); |
|
EXPECT_EQ(result, true); |
|
EXPECT_EQ(unsignedIntOpt1, 4294967295); |
|
|
|
// Required unsigned int short option, overflown value given |
|
unsignedIntOpt1 = 0; |
|
result = runParser({ "-u", "4294967296" }, [&](auto& parser) { |
|
parser.addOption(unsignedIntOpt1, 'u', nullptr, nullptr, nullptr, nullptr, Util::ArgParser::Required::Yes); |
|
}); |
|
EXPECT_EQ(result, false); |
|
EXPECT_EQ(unsignedIntOpt1, 0); |
|
|
|
// Required unsigned int short option, empty given |
|
unsignedIntOpt1 = 0; |
|
result = runParser({ "-u" }, [&](auto& parser) { |
|
parser.addOption(unsignedIntOpt1, 'u', nullptr, nullptr, nullptr, nullptr, Util::ArgParser::Required::Yes); |
|
}); |
|
EXPECT_EQ(result, false); |
|
EXPECT_EQ(unsignedIntOpt1, 0); |
|
|
|
// Required double short option |
|
double doubleOpt1 = 0; |
|
result = runParser({ "-d", "999.999" }, [&](auto& parser) { |
|
parser.addOption(doubleOpt1, 'd', nullptr, nullptr, nullptr, nullptr, Util::ArgParser::Required::Yes); |
|
}); |
|
EXPECT_EQ(result, true); |
|
EXPECT_EQ(doubleOpt1, 999.999); |
|
|
|
// Required double short option, empty given |
|
doubleOpt1 = 0; |
|
result = runParser({ "-d" }, [&](auto& parser) { |
|
parser.addOption(doubleOpt1, 'd', nullptr, nullptr, nullptr, nullptr, Util::ArgParser::Required::Yes); |
|
}); |
|
EXPECT_EQ(result, false); |
|
EXPECT_EQ(doubleOpt1, 0); |
|
} |
|
|
|
// ----------------------------------------- |
|
|
|
TEST_CASE(VectorStringOptions) |
|
{ |
|
// Required vector string short option, not given |
|
std::vector<std::string> vectorOpt1 = {}; |
|
auto result = runParser({}, [&](auto& parser) { |
|
parser.addOption(vectorOpt1, 'v', nullptr, nullptr, nullptr, nullptr, Util::ArgParser::Required::Yes); |
|
}); |
|
EXPECT_EQ(result, true); |
|
EXPECT_EQ(vectorOpt1.size(), 0); |
|
|
|
// Required vector string short option, empty given |
|
vectorOpt1 = {}; |
|
result = runParser({ "-v" }, [&](auto& parser) { |
|
parser.addOption(vectorOpt1, 'v', nullptr, nullptr, nullptr, nullptr, Util::ArgParser::Required::Yes); |
|
}); |
|
EXPECT_EQ(result, false); |
|
EXPECT_EQ(vectorOpt1.size(), 0); |
|
|
|
// Required vector string short option, one given |
|
vectorOpt1 = {}; |
|
result = runParser({ "-v", "a vector argument!" }, [&](auto& parser) { |
|
parser.addOption(vectorOpt1, 'v', nullptr, nullptr, nullptr, nullptr, Util::ArgParser::Required::Yes); |
|
}); |
|
EXPECT_EQ(result, true); |
|
EXPECT_EQ(vectorOpt1.size(), 1); |
|
if (vectorOpt1.size() == 1) { |
|
EXPECT_EQ(vectorOpt1[0], "a vector argument!"); |
|
} |
|
|
|
// Required vector string short option, two given |
|
vectorOpt1 = {}; |
|
result = runParser({ "-v", "hello", "-v", "world" }, [&](auto& parser) { |
|
parser.addOption(vectorOpt1, 'v', nullptr, nullptr, nullptr, nullptr, Util::ArgParser::Required::Yes); |
|
}); |
|
EXPECT_EQ(result, true); |
|
EXPECT_EQ(vectorOpt1.size(), 2); |
|
if (vectorOpt1.size() == 2) { |
|
EXPECT_EQ(vectorOpt1[0], "hello"); |
|
EXPECT_EQ(vectorOpt1[1], "world"); |
|
} |
|
|
|
// Required vector string short option, two given directly after |
|
vectorOpt1 = {}; |
|
result = runParser({ "-vhello", "-vworld" }, [&](auto& parser) { |
|
parser.addOption(vectorOpt1, 'v', nullptr, nullptr, nullptr, nullptr, Util::ArgParser::Required::Yes); |
|
}); |
|
EXPECT_EQ(result, true); |
|
EXPECT_EQ(vectorOpt1.size(), 2); |
|
if (vectorOpt1.size() == 2) { |
|
EXPECT_EQ(vectorOpt1[0], "hello"); |
|
EXPECT_EQ(vectorOpt1[1], "world"); |
|
} |
|
|
|
// Required vector string long option, empty given |
|
vectorOpt1 = {}; |
|
result = runParser({ "--vector" }, [&](auto& parser) { |
|
parser.addOption(vectorOpt1, '\0', "vector", nullptr, nullptr, nullptr, Util::ArgParser::Required::Yes); |
|
}); |
|
EXPECT_EQ(result, false); |
|
EXPECT_EQ(vectorOpt1.size(), 0); |
|
|
|
// Required vector string long option, one given |
|
vectorOpt1 = {}; |
|
result = runParser({ "--vector", "a vector argument!" }, [&](auto& parser) { |
|
parser.addOption(vectorOpt1, '\0', "vector", nullptr, nullptr, nullptr, Util::ArgParser::Required::Yes); |
|
}); |
|
EXPECT_EQ(result, true); |
|
EXPECT_EQ(vectorOpt1.size(), 1); |
|
if (vectorOpt1.size() == 1) { |
|
EXPECT_EQ(vectorOpt1[0], "a vector argument!"); |
|
} |
|
|
|
// Required vector string long option, two given |
|
vectorOpt1 = {}; |
|
result = runParser({ "--vector", "hello", "--vector", "world" }, [&](auto& parser) { |
|
parser.addOption(vectorOpt1, '\0', "vector", nullptr, nullptr, nullptr, Util::ArgParser::Required::Yes); |
|
}); |
|
EXPECT_EQ(result, true); |
|
EXPECT_EQ(vectorOpt1.size(), 2); |
|
if (vectorOpt1.size() == 2) { |
|
EXPECT_EQ(vectorOpt1[0], "hello"); |
|
EXPECT_EQ(vectorOpt1[1], "world"); |
|
} |
|
|
|
// Required vector string long option, two given directly after |
|
vectorOpt1 = {}; |
|
result = runParser({ "--vector=hello", "--vector=world" }, [&](auto& parser) { |
|
parser.addOption(vectorOpt1, '\0', "vector", nullptr, nullptr, nullptr, Util::ArgParser::Required::Yes); |
|
}); |
|
EXPECT_EQ(result, true); |
|
EXPECT_EQ(vectorOpt1.size(), 2); |
|
if (vectorOpt1.size() == 2) { |
|
EXPECT_EQ(vectorOpt1.at(0), "hello"); |
|
EXPECT_EQ(vectorOpt1.at(1), "world"); |
|
} |
|
} |
|
|
|
// ----------------------------------------- |
|
|
|
TEST_CASE(MultipleOptions) |
|
{ |
|
// Both short options, second is required, with a non-option argument in-between |
|
bool boolOpt1 = false; |
|
std::string stringOpt1 = ""; |
|
auto result = runParser({ "-b", "something", "-s", "a-string-value" }, [&](auto& parser) { |
|
parser.addOption(boolOpt1, 'b', nullptr, nullptr, nullptr); |
|
parser.addOption(stringOpt1, 's', nullptr, nullptr, nullptr, nullptr, Util::ArgParser::Required::Yes); |
|
}); |
|
EXPECT_EQ(result, true); |
|
EXPECT_EQ(boolOpt1, true); |
|
EXPECT_EQ(stringOpt1, "a-string-value"); |
|
} |
|
|
|
// ----------------------------------------- |
|
|
|
TEST_CASE(StopOnDoubleDashOption) |
|
{ |
|
// Bool short options, missing |
|
// Expected: The bool options are interpreted as non-option parameters |
|
bool boolOpt1 = false; |
|
bool boolOpt2 = false; |
|
auto result = runParser({ "--", "-b", "-c" }, [&](auto& parser) { |
|
parser.addOption(boolOpt1, 'b', nullptr, nullptr, nullptr); |
|
parser.addOption(boolOpt2, 'c', nullptr, nullptr, nullptr); |
|
}); |
|
EXPECT_EQ(result, true); |
|
EXPECT_EQ(boolOpt1, false); |
|
EXPECT_EQ(boolOpt2, false); |
|
|
|
// Bool short options, one given |
|
// Expected: boolOpt1 is set, one non-option parameter |
|
boolOpt1 = false; |
|
boolOpt2 = false; |
|
result = runParser({ "-b", "--", "-c" }, [&](auto& parser) { |
|
parser.addOption(boolOpt1, 'b', nullptr, nullptr, nullptr); |
|
parser.addOption(boolOpt2, 'c', nullptr, nullptr, nullptr); |
|
}); |
|
EXPECT_EQ(result, true); |
|
EXPECT_EQ(boolOpt1, true); |
|
EXPECT_EQ(boolOpt2, false); |
|
|
|
// Bool long options, missing |
|
// Expected: The bool options are interpreted as non-option parameters |
|
boolOpt1 = false; |
|
boolOpt2 = false; |
|
result = runParser({ "--", "--bool", "--cool" }, [&](auto& parser) { |
|
parser.addOption(boolOpt1, '\0', "bool", nullptr, nullptr); |
|
parser.addOption(boolOpt2, '\0', "cool", nullptr, nullptr); |
|
}); |
|
EXPECT_EQ(result, true); |
|
EXPECT_EQ(boolOpt1, false); |
|
EXPECT_EQ(boolOpt2, false); |
|
|
|
// Bool long options, one given |
|
// Expected: boolOpt1 is set, one non-option parameter |
|
boolOpt1 = false; |
|
boolOpt2 = false; |
|
result = runParser({ "--bool", "--", "--cool" }, [&](auto& parser) { |
|
parser.addOption(boolOpt1, '\0', "bool", nullptr, nullptr); |
|
parser.addOption(boolOpt2, '\0', "cool", nullptr, nullptr); |
|
}); |
|
EXPECT_EQ(result, true); |
|
EXPECT_EQ(boolOpt1, true); |
|
EXPECT_EQ(boolOpt2, false); |
|
} |
|
|
|
// ----------------------------------------- |
|
|
|
TEST_CASE(StopOnFirstNonOption) |
|
{ |
|
// Do not stop on first non-option; arguments are in correct order |
|
// Expected: The bool options are set and one non-option parameter |
|
bool boolOpt1 = false; |
|
bool boolOpt2 = false; |
|
auto result = runParser({ "-b", "-c", "stopping" }, [&](auto& parser) { |
|
parser.setStopParsingOnFirstNonOption(false); |
|
parser.addOption(boolOpt1, 'b', nullptr, nullptr, nullptr); |
|
parser.addOption(boolOpt2, 'c', nullptr, nullptr, nullptr); |
|
}); |
|
EXPECT_EQ(result, true); |
|
EXPECT_EQ(boolOpt1, true); |
|
EXPECT_EQ(boolOpt2, true); |
|
|
|
// Do not stop on first non-option; arguments are in wrong order |
|
// Expected: The bool options are set and one non-option parameter |
|
boolOpt1 = false; |
|
boolOpt2 = false; |
|
result = runParser({ "-b", "stopping", "-c" }, [&](auto& parser) { |
|
parser.setStopParsingOnFirstNonOption(false); |
|
parser.addOption(boolOpt1, 'b', nullptr, nullptr, nullptr); |
|
parser.addOption(boolOpt2, 'c', nullptr, nullptr, nullptr); |
|
}); |
|
EXPECT_EQ(result, true); |
|
EXPECT_EQ(boolOpt1, true); |
|
EXPECT_EQ(boolOpt2, true); |
|
|
|
// Stop on first non option, arguments are in correct order |
|
// Expected: The bool options are set and one non-option parameter |
|
boolOpt1 = false; |
|
boolOpt2 = false; |
|
result = runParser({ "-b", "-c", "stopping" }, [&](auto& parser) { |
|
parser.setStopParsingOnFirstNonOption(true); |
|
parser.addOption(boolOpt1, 'b', nullptr, nullptr, nullptr); |
|
parser.addOption(boolOpt2, 'c', nullptr, nullptr, nullptr); |
|
}); |
|
EXPECT_EQ(result, true); |
|
EXPECT_EQ(boolOpt1, true); |
|
EXPECT_EQ(boolOpt2, true); |
|
|
|
// Stop on first non option, arguments are in wrong order |
|
// Expected: boolOpt1 is set and the rest are non-option parameters |
|
boolOpt1 = false; |
|
boolOpt2 = false; |
|
result = runParser({ "-b", "stopping", "-c" }, [&](auto& parser) { |
|
parser.setStopParsingOnFirstNonOption(true); |
|
parser.addOption(boolOpt1, 'b', nullptr, nullptr, nullptr); |
|
parser.addOption(boolOpt2, 'c', nullptr, nullptr, nullptr); |
|
}); |
|
EXPECT_EQ(result, true); |
|
EXPECT_EQ(boolOpt1, true); |
|
EXPECT_EQ(boolOpt2, false); |
|
|
|
// ----------------------------------------- |
|
|
|
TEST_CASE(ExitOnFirstError) |
|
{ |
|
// Do not stop on first error, one non-existing given |
|
// Expected: parsing fails, boolOpt1 is set |
|
bool boolOpt1 = false; |
|
auto result = runParser({ "--this-doesnt-exist", "--this-exist" }, [&](auto& parser) { |
|
parser.setExitOnFirstError(false); |
|
parser.addOption(boolOpt1, '\0', "this-exist", nullptr, nullptr); |
|
}); |
|
EXPECT_EQ(result, false); |
|
EXPECT_EQ(boolOpt1, true); |
|
|
|
// Stop on first error, one non-existing given |
|
// Expected: parsing fails, boolOpt1 is not set |
|
boolOpt1 = false; |
|
result = runParser({ "--this-doesnt-exist", "--this-exist" }, [&](auto& parser) { |
|
parser.setExitOnFirstError(true); |
|
parser.addOption(boolOpt1, '\0', "this-exist", nullptr, nullptr); |
|
}); |
|
EXPECT_EQ(result, false); |
|
EXPECT_EQ(boolOpt1, false); |
|
} |
|
}
|
|
|