Argument is now a class. Also it's friends with ArgumentParser. Moved a number of methods to private

This commit is contained in:
Pranav Srinivas Kumar 2019-04-01 21:04:50 -04:00
parent 20a67c21a9
commit 8fd51f982d

View File

@ -85,17 +85,9 @@ T get_from_list(const std::vector<T>& aList, size_t aIndex) {
return aList[aIndex];
}
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;
size_t mNumArgs;
bool mIsOptional;
class Argument {
friend class ArgumentParser;
public:
Argument() :
mNames({}),
mHelp(""),
@ -131,6 +123,52 @@ struct Argument {
return *this;
}
template <typename T>
bool operator!=(const T& aRhs) const {
return !(*this == aRhs);
}
template <typename T>
typename std::enable_if<is_specialization<T, std::vector>::value == false &&
is_specialization<T, std::list>::value == false, bool>::type
operator==(const T& aRhs) const {
return get<T>() == aRhs;
}
template <typename T>
typename std::enable_if<is_specialization<T, std::vector>::value, bool>::type
operator==(const T& aRhs) const {
T tLhs = get_vector<T>();
if (tLhs.size() != aRhs.size())
return false;
else {
for (size_t i = 0; i < tLhs.size(); i++) {
auto tValueAtIndex = std::any_cast<typename T::value_type>(tLhs[i]);
if (tValueAtIndex != aRhs[i])
return false;
}
return true;
}
}
template <typename T>
typename std::enable_if<is_specialization<T, std::list>::value, bool>::type
operator==(const T& aRhs) const {
T tLhs = get_list<T>();
if (tLhs.size() != aRhs.size())
return false;
else {
for (size_t i = 0; i < tLhs.size(); i++) {
auto tValueAtIndex = std::any_cast<typename T::value_type>(get_from_list(tLhs, i));
if (tValueAtIndex != get_from_list(aRhs, i))
return false;
}
return true;
}
}
private:
template <typename T>
T get() const {
if (mValues.size() == 0) {
@ -222,50 +260,15 @@ struct Argument {
}
}
template <typename T>
bool operator!=(const T& aRhs) const {
return !(*this == aRhs);
}
template <typename T>
typename std::enable_if<is_specialization<T, std::vector>::value == false &&
is_specialization<T, std::list>::value == false, bool>::type
operator==(const T& aRhs) const {
return get<T>() == aRhs;
}
template <typename T>
typename std::enable_if<is_specialization<T, std::vector>::value, bool>::type
operator==(const T& aRhs) const {
T tLhs = get_vector<T>();
if (tLhs.size() != aRhs.size())
return false;
else {
for (size_t i = 0; i < tLhs.size(); i++) {
auto tValueAtIndex = std::any_cast<typename T::value_type>(tLhs[i]);
if (tValueAtIndex != aRhs[i])
return false;
}
return true;
}
}
template <typename T>
typename std::enable_if<is_specialization<T, std::list>::value, bool>::type
operator==(const T& aRhs) const {
T tLhs = get_list<T>();
if (tLhs.size() != aRhs.size())
return false;
else {
for (size_t i = 0; i < tLhs.size(); i++) {
auto tValueAtIndex = std::any_cast<typename T::value_type>(get_from_list(tLhs, i));
if (tValueAtIndex != get_from_list(aRhs, i))
return false;
}
return true;
}
}
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;
size_t mNumArgs;
bool mIsOptional;
};
class ArgumentParser {