Added member to store raw value in Argument

This commit is contained in:
Pranav Srinivas Kumar 2019-03-30 11:16:28 -04:00
parent a06308f673
commit bacc8dc39b

View File

@ -1,10 +1,29 @@
#include <string> #include <string>
#include <map>
#include <vector> #include <vector>
#include <functional> #include <functional>
#include <any> #include <any>
#include <memory>
namespace argparse { namespace argparse {
template <class KeyType, class ElementType>
bool upsert(std::map<KeyType, ElementType>& aMap, KeyType const& aKey, ElementType const& aNewValue) {
typedef typename std::map<KeyType, ElementType>::iterator Iterator;
typedef typename std::pair<Iterator, bool> Result;
Result tResult = aMap.insert(typename std::map<KeyType, ElementType>::value_type(aKey, aNewValue));
if (!tResult.second) {
if (!(tResult.first->second == aNewValue)) {
tResult.first->second = aNewValue;
return true;
}
else
return false; // it was the same
}
else
return true; // changed cause not existing
}
struct Argument { struct Argument {
std::vector<std::string> mNames; std::vector<std::string> mNames;
std::string mHelp; std::string mHelp;
@ -70,6 +89,9 @@ class ArgumentParser {
tArgument->mNames.push_back(value); tArgument->mNames.push_back(value);
add_argument_internal(tArgument, Fargs...); add_argument_internal(tArgument, Fargs...);
mArguments.push_back(tArgument); mArguments.push_back(tArgument);
for (auto& mName : tArgument->mNames) {
upsert(mArgumentMap, mName, tArgument);
}
return *tArgument; return *tArgument;
} }
@ -129,6 +151,7 @@ class ArgumentParser {
std::string mProgramName; std::string mProgramName;
std::vector<std::shared_ptr<Argument>> mArguments; std::vector<std::shared_ptr<Argument>> mArguments;
std::map<std::string, std::shared_ptr<Argument>> mArgumentMap;
}; };
} }