#ifndef ARGPARSER_ARGUMENT_H #define ARGPARSER_ARGUMENT_H #include "errors.h" #include "parse-result.h" #include "type.h" #include #include #include #include namespace argparser { class arg { public: explicit arg(std::string name) : name(std::move(name)) {} virtual ~arg() = default; void parse(const std::string &input, parse_result &pr) const { do_parse(input, pr); } [[nodiscard]] bool has_parsed_enough(parse_result &pr) const { return this->get_has_parsed_enough(pr); } [[nodiscard]] bool can_parse_more(parse_result &pr) const { return this->get_can_parse_more(pr); } [[nodiscard]] std::string get_name() const { return name; } protected: const std::string name; template static T parse_single_value(std::string input, const type_handle_impl &type) { const char *parse_end; auto begin = &*input.begin(); auto end = &*input.end(); auto val = type->parse(begin, end, parse_end, internal::parser_allow_undelimited::Any); if (parse_end != end) { throw errors::type_parsing_error(type->get_name(), std::string(begin, end), parse_end - begin, "unexpected input"); } return val; } private: virtual void do_parse(std::string input, parse_result &pr) const = 0; [[nodiscard]] virtual bool get_has_parsed_enough(parse_result &pr) const = 0; [[nodiscard]] virtual bool get_can_parse_more(parse_result &pr) const = 0; }; using arg_handle = std::shared_ptr; }// namespace argparser #endif//ARGPARSER_ARGUMENT_H