diff options
author | Taylor C. Richberger <Taywee@gmx.com> | 2016-04-27 18:05:14 -0600 |
---|---|---|
committer | Taylor C. Richberger <Taywee@gmx.com> | 2016-04-27 18:05:14 -0600 |
commit | 45669907b419e75f041a9b6d53331c0759819c0f (patch) | |
tree | 9bc4743eab6a0b964365560f83846c3be622939a | |
parent | replace hello world with real start (diff) | |
download | args.hxx-45669907b419e75f041a9b6d53331c0759819c0f.tar.xz |
add validators, add more body to library
-rw-r--r-- | args.hxx | 92 |
1 files changed, 92 insertions, 0 deletions
@@ -4,9 +4,99 @@ #include <string> #include <vector> +#include <list> +#include <functional> namespace args { + class Base + { + protected: + bool matched; + + public: + Base() : matched(false) {} + virtual ~Base() {} + + virtual bool Matched() const + { + return matched; + } + }; + + class Group : public Base + { + private: + std::vector<Base*> children; + std::function<bool(int, int)> validator; + + public: + + Group(const std::function<bool(int, int)> &validator = Validators::DontCare) : validator(validator) {} + virtual ~Group() {} + + void Add(Base &child) + { + children.emplace_back(&child); + } + + int MatchedChildren() const + { + int sum = 0; + for (const Base * child: children) + { + if (child->Matched()) + { + ++sum; + } + } + return sum; + } + + virtual bool Matched() const override + { + return validator(children.size(), MatchedChildren()); + } + + struct Validators + { + static bool Xor(int children, int matched) + { + return matched == 1; + } + + static bool AtLeastOne(int children, int matched) + { + return matched >= 1; + } + + static bool AtMostOne(int children, int matched) + { + return matched <= 1; + } + + static bool All(int children, int matched) + { + return children == matched; + } + + static bool DontCare(int children, int matched) + { + return true; + } + + static bool CareTooMuch(int children, int matched) + { + return false; + } + + static bool None(int children, int matched) + { + return matched == 0; + } + }; + }; + class ArgumentParser { private: @@ -14,6 +104,8 @@ namespace args std::string description; std::string epilog; + Group args; + public: ArgumentParser( const std::string &prog, |