Fixes issue #9 where error message shows the small form of the argument even though the user enters the long version

This commit is contained in:
Pranav Srinivas Kumar 2019-05-01 20:06:13 -04:00
parent 4d98282e2f
commit 7c9f83c7e1

View File

@ -89,6 +89,7 @@ class Argument {
public:
Argument() :
mNames({}),
mUsedName(""),
mHelp(""),
mAction([](const std::string& aValue) { return aValue; }),
mValues({}),
@ -267,6 +268,7 @@ public:
}
std::vector<std::string> mNames;
std::string mUsedName;
std::string mHelp;
std::any mDefaultValue;
std::any mImplicitValue;
@ -520,6 +522,7 @@ class ArgumentParser {
if (tIterator != mArgumentMap.end()) {
// Start parsing optional argument
auto tArgument = tIterator->second;
tArgument->mUsedName = tCurrentArgument;
tArgument->mIsUsed = true;
auto tCount = tArgument->mNumArgs;
@ -536,6 +539,7 @@ class ArgumentParser {
while (tCount > 0) {
i = i + 1;
if (i < argc) {
tArgument->mUsedName = tCurrentArgument;
tArgument->mRawValues.push_back(argv[i]);
if (tArgument->mAction != nullptr)
tArgument->mValues.push_back(tArgument->mAction(argv[i]));
@ -597,6 +601,7 @@ class ArgumentParser {
break;
}
if (i < argc) {
tArgument->mUsedName = tCurrentArgument;
tArgument->mRawValues.push_back(argv[i]);
if (tArgument->mAction != nullptr)
tArgument->mValues.push_back(tArgument->mAction(argv[i]));
@ -622,7 +627,7 @@ class ArgumentParser {
for (size_t i = 0; i < mPositionalArguments.size(); i++) {
auto tArgument = mPositionalArguments[i];
if (tArgument->mValues.size() != tArgument->mNumArgs) {
std::cout << "error: " << tArgument->mNames[0] << ": expected "
std::cout << "error: " << tArgument->mUsedName << ": expected "
<< tArgument->mNumArgs << (tArgument->mNumArgs == 1 ? " argument. " : " arguments. ")
<< tArgument->mValues.size() << " provided.\n" << std::endl;
print_help();
@ -638,7 +643,7 @@ class ArgumentParser {
// All cool if there's a default value to return
// If no default value, then there's a problem
if (!tArgument->mDefaultValue.has_value()) {
std::cout << "error: " << tArgument->mNames[0] << ": expected "
std::cout << "error: " << tArgument->mUsedName << ": expected "
<< tArgument->mNumArgs << (tArgument->mNumArgs == 1 ? " argument. " : " arguments. ")
<< tArgument->mValues.size() << " provided.\n" << std::endl;
print_help();