Implemented API and tests for programming implicit values in toggle arguments

This commit is contained in:
Pranav Srinivas Kumar 2019-03-30 21:21:42 -04:00
parent 996f7882ca
commit f6f9b0302d
3 changed files with 46 additions and 0 deletions

View File

@ -42,6 +42,7 @@ struct Argument {
std::vector<std::string> mNames; std::vector<std::string> mNames;
std::string mHelp; std::string mHelp;
std::any mDefaultValue; std::any mDefaultValue;
std::any mImplicitValue;
std::function<std::any(const std::string&)> mAction; std::function<std::any(const std::string&)> mAction;
std::vector<std::any> mValues; std::vector<std::any> mValues;
std::vector<std::string> mRawValues; std::vector<std::string> mRawValues;
@ -67,6 +68,11 @@ struct Argument {
return *this; return *this;
} }
Argument& implicit_value(std::any aImplicitValue) {
mImplicitValue = aImplicitValue;
return *this;
}
Argument& action(std::function<std::any(const std::string&)> aAction) { Argument& action(std::function<std::any(const std::string&)> aAction) {
mAction = aAction; mAction = aAction;
return *this; return *this;
@ -176,8 +182,20 @@ class ArgumentParser {
auto tCurrentArgument = argv[i]; auto tCurrentArgument = argv[i];
std::map<std::string, std::shared_ptr<Argument>>::iterator tIterator = mArgumentMap.find(argv[i]); std::map<std::string, std::shared_ptr<Argument>>::iterator tIterator = mArgumentMap.find(argv[i]);
if (tIterator != mArgumentMap.end()) { if (tIterator != mArgumentMap.end()) {
// Start parsing optional argument
auto tArgument = tIterator->second; auto tArgument = tIterator->second;
auto tCount = tArgument->mNumArgs; 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) { while (tCount > 0) {
i = i + 1; i = i + 1;
if (i < argc) { if (i < argc) {

View File

@ -4,3 +4,4 @@
#include <test_add_argument.hpp> #include <test_add_argument.hpp>
#include <test_parse_args.hpp> #include <test_parse_args.hpp>
#include <test_positional_arguments.hpp> #include <test_positional_arguments.hpp>
#include <test_implicit_values.hpp>

View 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);
}