mirror of
https://git.lynn.is/Gwen/argparser.git
synced 2024-05-14 21:31:09 +02:00
60 lines
1.7 KiB
C
60 lines
1.7 KiB
C
|
#ifndef ARGPARSER_ARGUMENT_H
|
||
|
#define ARGPARSER_ARGUMENT_H
|
||
|
|
||
|
#include "errors.h"
|
||
|
#include "parse-result.h"
|
||
|
#include "type.h"
|
||
|
#include <any>
|
||
|
#include <memory>
|
||
|
#include <span>
|
||
|
#include <string>
|
||
|
|
||
|
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<typename T>
|
||
|
static T parse_single_value(std::string input, const type_handle_impl<T> &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<arg>;
|
||
|
}// namespace argparser
|
||
|
|
||
|
#endif//ARGPARSER_ARGUMENT_H
|