init project
This commit is contained in:
commit
821ea7eed0
16 changed files with 950 additions and 0 deletions
54
include/utils/command_parser.hpp
Normal file
54
include/utils/command_parser.hpp
Normal file
|
@ -0,0 +1,54 @@
|
|||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <optional>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <functional>
|
||||
|
||||
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);
|
Loading…
Add table
Add a link
Reference in a new issue