56 lines
1.8 KiB
C++
56 lines
1.8 KiB
C++
#pragma once
|
|
|
|
#include <iostream>
|
|
#include <optional>
|
|
#include <vector>
|
|
#include <string>
|
|
#include <functional>
|
|
#include <variant>
|
|
|
|
enum class PatternType { FixedWord, AnyWord, AnyWords };
|
|
|
|
struct Pattern {
|
|
PatternType type;
|
|
std::string fixed_word;
|
|
};
|
|
|
|
using Callback = std::variant<std::function<void(const std::vector<std::string>&)>,
|
|
std::function<bool(const std::vector<std::string>&)>>;
|
|
|
|
class Parser {
|
|
public:
|
|
void AddRule(const std::vector<Pattern>& pattern, const auto& callback,
|
|
bool find_in_suggestion = true) {
|
|
if constexpr (std::is_same_v<
|
|
decltype(callback(std::declval<const std::vector<std::string>&>())), void>) {
|
|
AddRuleImpl(pattern, std::function<void(const std::vector<std::string>&)>(callback),
|
|
find_in_suggestion);
|
|
} else {
|
|
AddRuleImpl(pattern, std::function<bool(const std::vector<std::string>&)>(callback),
|
|
find_in_suggestion);
|
|
}
|
|
}
|
|
void Parse(int argc, char* argv[]) const;
|
|
std::vector<std::string> GetPossibleAdditions(int argc, char* argv[]) const;
|
|
std::vector<std::string> GetPossibleAdditions(const std::vector<std::string>&) const;
|
|
|
|
private:
|
|
void AddRuleImpl(const std::vector<Pattern>& pattern, const Callback& callback,
|
|
bool find_in_suggestion);
|
|
bool Match(const std::vector<Pattern>& pattern, const std::vector<std::string>& args) const;
|
|
|
|
private:
|
|
struct Rule {
|
|
Rule(const std::vector<Pattern>& pattern, Callback callback, bool find_in_suggestion)
|
|
: pattern(pattern), callback(callback), find_in_suggestion(find_in_suggestion) {}
|
|
std::vector<Pattern> pattern;
|
|
Callback callback;
|
|
bool find_in_suggestion;
|
|
};
|
|
|
|
std::vector<Rule> rules_;
|
|
};
|
|
|
|
std::vector<Pattern> MakePattern(
|
|
std::initializer_list<std::variant<const char*, PatternType>> elements);
|