This commit is contained in:
Timofey 2022-08-10 12:02:25 +03:00
parent b9d951ec9a
commit c2d0033f78
4 changed files with 87 additions and 12 deletions

View file

@ -1,5 +1,7 @@
#include <clippy/clippy.hpp> #include <clippy/clippy.hpp>
#include <utils/parametres.hpp>
#include <iostream> #include <iostream>
#include <vector> #include <vector>
@ -8,7 +10,9 @@ void Clippy::Run(const std::vector<std::string>& args) {
} }
bool Clippy::TryExecuteClippyCommand(const std::vector<std::string>& args) { bool Clippy::TryExecuteClippyCommand(const std::vector<std::string>& args) {
if (args.size() >= 2 && (args[1] == "help" || args[1] == "hello")) { using namespace utils::parametres;
if (CheckPatternParametres(args, Parameter::Skip, "help",
Parameter::Anything)) {
std::cout << "Hello I'm clippy" << std::endl; std::cout << "Hello I'm clippy" << std::endl;
std::cout << "Parametres: { "; std::cout << "Parametres: { ";
for (size_t i = 0; i < args.size(); ++i) { for (size_t i = 0; i < args.size(); ++i) {

View file

@ -1,20 +1,11 @@
#include <clippy/clippy.hpp> #include <clippy/clippy.hpp>
#include <utils/parametres.hpp>
#include <vector> #include <vector>
#include <string> #include <string>
std::vector<std::string> ParseInputParameters(int argc, char* argv[]) {
std::vector<std::string> result;
result.reserve(argc);
for (size_t i = 0; i < argc; ++i) {
result.emplace_back(argv[i]);
}
return result;
}
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
using namespace utils::parametres;
auto params = ParseInputParameters(argc, argv); auto params = ParseInputParameters(argc, argv);
Clippy clippy; Clippy clippy;

14
src/utils/parametres.cpp Normal file
View file

@ -0,0 +1,14 @@
#include <utils/parametres.hpp>
namespace utils::parametres {
std::vector<std::string> ParseInputParameters(int argc, char* argv[]) {
std::vector<std::string> result;
result.reserve(argc);
for (size_t i = 0; i < argc; ++i) {
result.emplace_back(argv[i]);
}
return result;
}
} // namespace utils

66
src/utils/parametres.hpp Normal file
View file

@ -0,0 +1,66 @@
#pragma once
#include <vector>
#include <string>
#include <stdexcept>
namespace utils::parametres {
enum class Parameter { Skip, Nothing, Anything };
namespace detail {
template <size_t index, typename... Args>
bool CheckPatternParametresImpl(const std::vector<std::string>& args,
Parameter t) {
if (t == Parameter::Nothing) {
return index == args.size();
} else if (t == Parameter::Anything) {
return true;
}
return false;
}
template <size_t index, typename T, typename... Args,
std::enable_if_t<!std::is_same_v<T, Parameter>, bool>>
bool CheckPatternParametresImpl(const std::vector<std::string>& args, T&& head,
Args&&... pattern);
template <size_t index, typename T, typename... Args,
std::enable_if_t<std::is_same_v<T, Parameter>, bool> = true>
bool CheckPatternParametresImpl(const std::vector<std::string>& args, T&& p,
Args&&... pattern) {
if (index >= args.size()) {
return false;
}
if (p != Parameter::Skip) {
throw std::logic_error("Unsupported parameter");
}
return CheckPatternParametresImpl<index + 1, Args...>(
args, std::forward<Args>(pattern)...);
}
template <size_t index, typename T, typename... Args,
std::enable_if_t<!std::is_same_v<T, Parameter>, bool> = true>
bool CheckPatternParametresImpl(const std::vector<std::string>& args, T&& head,
Args&&... pattern) {
if (index >= args.size()) {
return false;
}
if (args[index] != head) {
return false;
}
return CheckPatternParametresImpl<index + 1, Args...>(
args, std::forward<Args>(pattern)...);
}
} // namespace detail
std::vector<std::string> ParseInputParameters(int argc, char* argv[]);
template <typename... Args>
bool CheckPatternParametres(const std::vector<std::string>& args,
Args&&... pattern) {
return detail::CheckPatternParametresImpl<0, Args...>(
args, std::forward<Args>(pattern)...);
}
} // namespace utils