#ifndef ARGPARSER_SINGLE_FLAG_H #define ARGPARSER_SINGLE_FLAG_H #include #include #include #include #include "option.h" #include "parse-result.h" #include "type.h" namespace argparser { class flag_impl : public option, public std::enable_shared_from_this { public: explicit flag_impl(std::string name) : option(std::move(name)) {} [[nodiscard]] bool is_inverted() const { return is_inverted_; } std::shared_ptr invert() { this->is_inverted_ = true; return this->shared_from_this(); } [[nodiscard]] bool consumes_value() const override { return false; } void validate(const parse_result &) const override { } [[nodiscard]] bool get(const parse_result &pr) const { bool val = pr.has_opt(name); if (is_inverted_) { val = !val; } return val; } [[nodiscard]] bool has(const parse_result &pr) const { return pr.has_opt(name); } private: void do_parse(std::optional arg, std::any &val) override { assert(!arg.has_value()); if (!val.has_value()) { val = std::make_any(true); } } bool is_inverted_{}; }; using flag_handle_impl = std::shared_ptr; }// namespace argparser #endif//ARGPARSER_SINGLE_FLAG_H