mirror of
https://github.com/KeqingMoe/argparse.git
synced 2025-07-04 15:14:39 +00:00
Added support for parent parsers. Some basic unit tests
This commit is contained in:
parent
79fb2e8598
commit
9037b08454
@ -274,6 +274,30 @@ class ArgumentParser {
|
|||||||
return *tArgument;
|
return *tArgument;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void add_parents() {
|
||||||
|
for (size_t i = 0; i < mParentParsers.size(); i++) {
|
||||||
|
auto tParentParser = mParentParsers[i];
|
||||||
|
auto tPositionalArguments = tParentParser.mPositionalArguments;
|
||||||
|
for (auto& tArgument : tPositionalArguments) {
|
||||||
|
mPositionalArguments.push_back(tArgument);
|
||||||
|
}
|
||||||
|
auto tOptionalArguments = tParentParser.mOptionalArguments;
|
||||||
|
for (auto& tArgument : tOptionalArguments) {
|
||||||
|
mOptionalArguments.push_back(tArgument);
|
||||||
|
}
|
||||||
|
auto tArgumentMap = tParentParser.mArgumentMap;
|
||||||
|
for (auto&[tKey, tValue] : tArgumentMap) {
|
||||||
|
upsert(mArgumentMap, tKey, tValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T, typename... Targs>
|
||||||
|
void add_parents(T aArgumentParser, Targs... Fargs) {
|
||||||
|
mParentParsers.push_back(aArgumentParser);
|
||||||
|
add_parents(Fargs...);
|
||||||
|
}
|
||||||
|
|
||||||
void parse_args(const std::vector<std::string>& aArguments) {
|
void parse_args(const std::vector<std::string>& aArguments) {
|
||||||
parse_args_internal(aArguments);
|
parse_args_internal(aArguments);
|
||||||
parse_args_validate();
|
parse_args_validate();
|
||||||
@ -563,6 +587,7 @@ class ArgumentParser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::string mProgramName;
|
std::string mProgramName;
|
||||||
|
std::vector<ArgumentParser> mParentParsers;
|
||||||
std::vector<std::shared_ptr<Argument>> mPositionalArguments;
|
std::vector<std::shared_ptr<Argument>> mPositionalArguments;
|
||||||
std::vector<std::shared_ptr<Argument>> mOptionalArguments;
|
std::vector<std::shared_ptr<Argument>> mOptionalArguments;
|
||||||
size_t mNextPositionalArgument;
|
size_t mNextPositionalArgument;
|
||||||
|
@ -6,4 +6,5 @@
|
|||||||
#include <test_optional_arguments.hpp>
|
#include <test_optional_arguments.hpp>
|
||||||
#include <test_compound_arguments.hpp>
|
#include <test_compound_arguments.hpp>
|
||||||
#include <test_actions.hpp>
|
#include <test_actions.hpp>
|
||||||
#include <test_container_arguments.hpp>
|
#include <test_container_arguments.hpp>
|
||||||
|
#include <test_parent_parsers.hpp>
|
34
tests/test_parent_parsers.hpp
Normal file
34
tests/test_parent_parsers.hpp
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <catch.hpp>
|
||||||
|
#include <argparse.hpp>
|
||||||
|
|
||||||
|
TEST_CASE("Add parent parsers", "[parent_parsers]") {
|
||||||
|
argparse::ArgumentParser parent_parser("main");
|
||||||
|
parent_parser.add_argument("--verbose")
|
||||||
|
.default_value(false)
|
||||||
|
.implicit_value(true);
|
||||||
|
|
||||||
|
argparse::ArgumentParser child_parser("foo");
|
||||||
|
child_parser.add_parents(parent_parser);
|
||||||
|
child_parser.parse_args({ "./main", "--verbose"});
|
||||||
|
REQUIRE(child_parser["--verbose"] == true);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Add parent to multiple parent parsers", "[parent_parsers]") {
|
||||||
|
argparse::ArgumentParser parent_parser("main");
|
||||||
|
parent_parser.add_argument("--parent")
|
||||||
|
.default_value(0)
|
||||||
|
.action([](const std::string& value) { return std::stoi(value); });
|
||||||
|
|
||||||
|
argparse::ArgumentParser foo_parser("foo");
|
||||||
|
foo_parser.add_argument("foo");
|
||||||
|
foo_parser.add_parents(parent_parser);
|
||||||
|
foo_parser.parse_args({ "./main", "--parent", "2", "XXX" });
|
||||||
|
REQUIRE(foo_parser["--parent"] == 2);
|
||||||
|
REQUIRE(foo_parser["foo"] == std::string("XXX"));
|
||||||
|
|
||||||
|
argparse::ArgumentParser bar_parser("bar");
|
||||||
|
bar_parser.add_argument("--bar");
|
||||||
|
bar_parser.parse_args({ "./main", "--bar", "YYY" });
|
||||||
|
REQUIRE(bar_parser["--bar"] == std::string("YYY"));
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user