From 45669907b419e75f041a9b6d53331c0759819c0f Mon Sep 17 00:00:00 2001 From: "Taylor C. Richberger" Date: Wed, 27 Apr 2016 18:05:14 -0600 Subject: add validators, add more body to library --- args.hxx | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/args.hxx b/args.hxx index c364f39..442b2a9 100644 --- a/args.hxx +++ b/args.hxx @@ -4,9 +4,99 @@ #include #include +#include +#include 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 children; + std::function validator; + + public: + + Group(const std::function &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, -- cgit v1.2.1