diff options
author | Taylor C. Richberger <taywee@gmx.com> | 2017-11-19 09:49:53 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-11-19 09:49:53 -0700 |
commit | 431997da39a678b1aad0b85a5aacabd70120d513 (patch) | |
tree | 452b897a311e5d6f190ad33979ecdb00092fba55 /args.hxx | |
parent | fix ValueReader for types assignable to std::string (diff) | |
parent | Merge pull request #45 from pavel-belikov/readme (diff) | |
download | args.hxx-431997da39a678b1aad0b85a5aacabd70120d513.tar.xz |
Merge branch 'master' into better-value-parsing
Diffstat (limited to 'args.hxx')
-rw-r--r-- | args.hxx | 36 |
1 files changed, 36 insertions, 0 deletions
@@ -2577,6 +2577,42 @@ namespace args } }; + /** A flag class that calls a function when it's matched + */ + class ActionFlag : public FlagBase + { + private: + std::function<void(const std::vector<std::string> &)> action; + Nargs nargs; + + public: + ActionFlag(Group &group_, const std::string &name_, const std::string &help_, Matcher &&matcher_, Nargs nargs_, std::function<void(const std::vector<std::string> &)> action_, Options options_ = {}): + FlagBase(name_, help_, std::move(matcher_), options_), action(std::move(action_)), nargs(nargs_) + { + group_.Add(*this); + } + + ActionFlag(Group &group_, const std::string &name_, const std::string &help_, Matcher &&matcher_, std::function<void(const std::string &)> action_, Options options_ = {}): + FlagBase(name_, help_, std::move(matcher_), options_), nargs(1) + { + group_.Add(*this); + action = [action_](const std::vector<std::string> &a) { return action_(a.at(0)); }; + } + + ActionFlag(Group &group_, const std::string &name_, const std::string &help_, Matcher &&matcher_, std::function<void()> action_, Options options_ = {}): + FlagBase(name_, help_, std::move(matcher_), options_), nargs(0) + { + group_.Add(*this); + action = [action_](const std::vector<std::string> &) { return action_(); }; + } + + virtual Nargs NumberOfArguments() const noexcept override + { return nargs; } + + virtual void ParseValue(const std::vector<std::string> &value) override + { action(value); } + }; + /** A default Reader class for argument classes * * If destination type is assignable to std::string it uses an assignment to std::string. |