#ifndef ARGPARSER_SINGLE_ARG_H #define ARGPARSER_SINGLE_ARG_H #include #include #include #include "argument.h" #include "errors.h" #include "parse-result.h" #include "type.h" namespace argparser { template class single_arg : public arg, public std::enable_shared_from_this> { public: single_arg(std::string name, type_handle_impl type) : arg(std::move(name)), type(type) {} [[nodiscard]] T get(const parse_result &pr) const { auto v = pr.get_arg(name); if (!v.has_value()) { throw errors::missing_argument_error(name); } return std::any_cast(v); } [[nodiscard]] bool has(const parse_result &pr) const { return pr.has_arg(name); } private: type_handle_impl type; void do_parse(std::string input, parse_result &pr) const override { assert(!pr.get_arg(name).has_value());// a single arg can only be parsed once auto val = this->parse_single_value(input, type); pr.set_arg(name, std::make_any(val)); } bool get_can_parse_more(parse_result &pr) const override { return !pr.get_arg(name).has_value(); } bool get_has_parsed_enough(parse_result &pr) const override { return pr.get_arg(name).has_value(); } }; template using single_arg_handle = std::shared_ptr>; }// namespace argparser #endif//ARGPARSER_SINGLE_ARG_H