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