mirror of
https://github.com/KeqingMoe/argparse.git
synced 2025-07-04 15:14:39 +00:00
Implemented API and tests for programming implicit values in toggle arguments
This commit is contained in:
parent
996f7882ca
commit
f6f9b0302d
@ -42,6 +42,7 @@ struct Argument {
|
||||
std::vector<std::string> mNames;
|
||||
std::string mHelp;
|
||||
std::any mDefaultValue;
|
||||
std::any mImplicitValue;
|
||||
std::function<std::any(const std::string&)> mAction;
|
||||
std::vector<std::any> mValues;
|
||||
std::vector<std::string> mRawValues;
|
||||
@ -67,6 +68,11 @@ struct Argument {
|
||||
return *this;
|
||||
}
|
||||
|
||||
Argument& implicit_value(std::any aImplicitValue) {
|
||||
mImplicitValue = aImplicitValue;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Argument& action(std::function<std::any(const std::string&)> aAction) {
|
||||
mAction = aAction;
|
||||
return *this;
|
||||
@ -176,8 +182,20 @@ class ArgumentParser {
|
||||
auto tCurrentArgument = argv[i];
|
||||
std::map<std::string, std::shared_ptr<Argument>>::iterator tIterator = mArgumentMap.find(argv[i]);
|
||||
if (tIterator != mArgumentMap.end()) {
|
||||
// Start parsing optional argument
|
||||
auto tArgument = tIterator->second;
|
||||
auto tCount = tArgument->mNumArgs;
|
||||
|
||||
// Check to see if implicit value should be used
|
||||
// Two cases to handle here:
|
||||
// (1) User has explicitly programmed nargs to be 0
|
||||
// (2) User has left nargs to be default, i.e., 1 and provided an implicit value
|
||||
if (tCount == 0 || (tArgument->mImplicitValue.has_value() && tCount == 1)) {
|
||||
// Use implicit value for this optional argument
|
||||
tArgument->mValues.push_back(tArgument->mImplicitValue);
|
||||
tArgument->mRawValues.push_back("");
|
||||
tCount = 0;
|
||||
}
|
||||
while (tCount > 0) {
|
||||
i = i + 1;
|
||||
if (i < argc) {
|
||||
|
@ -4,3 +4,4 @@
|
||||
#include <test_add_argument.hpp>
|
||||
#include <test_parse_args.hpp>
|
||||
#include <test_positional_arguments.hpp>
|
||||
#include <test_implicit_values.hpp>
|
27
tests/test_implicit_values.hpp
Normal file
27
tests/test_implicit_values.hpp
Normal file
@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
#include <catch.hpp>
|
||||
#include <argparse.hpp>
|
||||
|
||||
TEST_CASE("Parse toggle arguments with default value", "[parse_args]") {
|
||||
argparse::ArgumentParser program("test");
|
||||
program.add_argument("--verbose", "-v")
|
||||
.default_value(false)
|
||||
.implicit_value(true);
|
||||
|
||||
program.parse_args({ "./test.exe" });
|
||||
auto arguments = program.get_arguments();
|
||||
REQUIRE(arguments.size() == 2);
|
||||
REQUIRE(program.get<bool>("--verbose") == false);
|
||||
}
|
||||
|
||||
TEST_CASE("Parse toggle arguments with implicit value", "[parse_args]") {
|
||||
argparse::ArgumentParser program("test");
|
||||
program.add_argument("--verbose")
|
||||
.default_value(false)
|
||||
.implicit_value(true);
|
||||
|
||||
program.parse_args({ "./test.exe", "--verbose" });
|
||||
auto arguments = program.get_arguments();
|
||||
REQUIRE(arguments.size() == 1);
|
||||
REQUIRE(program.get<bool>("--verbose") == true);
|
||||
}
|
Loading…
Reference in New Issue
Block a user