aboutsummaryrefslogtreecommitdiff
path: root/args.hxx
diff options
context:
space:
mode:
authorTaylor C. Richberger <taywee@gmx.com>2017-11-19 09:49:53 -0700
committerGitHub <noreply@github.com>2017-11-19 09:49:53 -0700
commit431997da39a678b1aad0b85a5aacabd70120d513 (patch)
tree452b897a311e5d6f190ad33979ecdb00092fba55 /args.hxx
parentfix ValueReader for types assignable to std::string (diff)
parentMerge pull request #45 from pavel-belikov/readme (diff)
downloadargs.hxx-431997da39a678b1aad0b85a5aacabd70120d513.tar.xz
Merge branch 'master' into better-value-parsing
Diffstat (limited to 'args.hxx')
-rw-r--r--args.hxx36
1 files changed, 36 insertions, 0 deletions
diff --git a/args.hxx b/args.hxx
index 5169d13..b45b912 100644
--- a/args.hxx
+++ b/args.hxx
@@ -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.