|
|
|
@ -1,3 +1,5 @@
|
|
|
|
|
#include <string> |
|
|
|
|
|
|
|
|
|
#include "macro.h" |
|
|
|
|
#include "testcase.h" |
|
|
|
|
#include "testsuite.h" |
|
|
|
@ -19,12 +21,16 @@ bool runParser(std::vector<const char*> arguments, std::function<void(Util::ArgP
|
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// -----------------------------------------
|
|
|
|
|
|
|
|
|
|
TEST_CASE(NoArguments) |
|
|
|
|
{ |
|
|
|
|
auto result = runParser({}); |
|
|
|
|
EXPECT_EQ(result, true); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// -----------------------------------------
|
|
|
|
|
|
|
|
|
|
TEST_CASE(BoolOptions) |
|
|
|
|
{ |
|
|
|
|
// Short option
|
|
|
|
@ -84,6 +90,215 @@ TEST_CASE(BoolOptions)
|
|
|
|
|
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(MultipleOptions) |
|
|
|
|
{ |
|
|
|
|
// Both short options, second is required, with a non-option argument in-between
|
|
|
|
|