diff options
| -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, | 
