Added option=value support using #185

This commit is contained in:
Pranav Srinivas Kumar 2022-09-21 06:54:34 -07:00
parent d512563c45
commit 20095a697a
3 changed files with 67 additions and 2 deletions

View File

@ -1226,10 +1226,42 @@ public:
} }
private: private:
/*
* Pre-process this argument list. Anything starting with "--", that
* contains an =, where the prefix before the = has an entry in the
* options table, should be split.
*/
std::vector<std::string>
preprocess_arguments(const std::vector<std::string> &raw_arguments) {
std::vector<std::string> arguments;
for (const auto &arg : raw_arguments) {
// Check that:
// - We don't have an argument named exactly this
// - The argument starts with "--"
// - The argument contains a "="
std::size_t eqpos = arg.find("=");
if (m_argument_map.find(arg) == m_argument_map.end() &&
arg.rfind("--", 0) == 0 && eqpos != std::string::npos) {
// Get the name of the potential option, and check it exists
std::string opt_name = arg.substr(0, eqpos);
if (m_argument_map.find(opt_name) != m_argument_map.end()) {
// This is the name of an option! Split it into two parts
arguments.push_back(std::move(opt_name));
arguments.push_back(arg.substr(eqpos + 1));
continue;
}
}
// If we've fallen through to here, then it's a standard argument
arguments.push_back(arg);
}
return arguments;
}
/* /*
* @throws std::runtime_error in case of any invalid argument * @throws std::runtime_error in case of any invalid argument
*/ */
void parse_args_internal(const std::vector<std::string> &arguments) { void parse_args_internal(const std::vector<std::string> &raw_arguments) {
auto arguments = preprocess_arguments(raw_arguments);
if (m_program_name.empty() && !arguments.empty()) { if (m_program_name.empty() && !arguments.empty()) {
m_program_name = arguments.front(); m_program_name = arguments.front();
} }
@ -1294,7 +1326,8 @@ private:
* Like parse_args_internal but collects unused args into a vector<string> * Like parse_args_internal but collects unused args into a vector<string>
*/ */
std::vector<std::string> std::vector<std::string>
parse_known_args_internal(const std::vector<std::string> &arguments) { parse_known_args_internal(const std::vector<std::string> &raw_arguments) {
auto arguments = preprocess_arguments(raw_arguments);
std::vector<std::string> unknown_arguments{}; std::vector<std::string> unknown_arguments{};

View File

@ -48,6 +48,7 @@ file(GLOB ARGPARSE_TEST_SOURCES
test_version.cpp test_version.cpp
test_subparsers.cpp test_subparsers.cpp
test_parse_known_args.cpp test_parse_known_args.cpp
test_equals_form.cpp
) )
set_source_files_properties(main.cpp set_source_files_properties(main.cpp
PROPERTIES PROPERTIES

31
test/test_equals_form.cpp Normal file
View File

@ -0,0 +1,31 @@
#include <argparse/argparse.hpp>
#include <doctest.hpp>
#include <iostream>
using doctest::test_suite;
TEST_CASE("Basic --value=value" * test_suite("equals_form")) {
argparse::ArgumentParser parser("test");
parser.add_argument("--long");
parser.parse_args({"test", "--long=value"});
std::string result{parser.get("--long")};
REQUIRE(result == "value");
}
TEST_CASE("Fallback = in regular option name" * test_suite("equals_form")) {
argparse::ArgumentParser parser("test");
parser.add_argument("--long=mislead");
parser.parse_args({"test", "--long=mislead", "value"});
std::string result{parser.get("--long=mislead")};
REQUIRE(result == "value");
}
TEST_CASE("Duplicate =-named and standard" * test_suite("equals_form")) {
argparse::ArgumentParser parser("test");
parser.add_argument("--long=mislead");
parser.add_argument("--long").default_value(std::string{"NO_VALUE"});
parser.parse_args({"test", "--long=mislead", "value"});
std::string result{parser.get("--long=mislead")};
REQUIRE(result == "value");
std::string result2{parser.get("--long")};
REQUIRE(result2 == "NO_VALUE");
}