mirror of
https://github.com/lucaspalomodevelop/lpstd.git
synced 2026-03-12 23:27:22 +00:00
some changes && add command class
This commit is contained in:
parent
a62992c7dd
commit
179f7b4107
@ -2,10 +2,15 @@
|
||||
#ifndef __LPSTD_HPP__
|
||||
#define __LPSTD_HPP__
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <cstdio>
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
#include "src/patterns/singleton.hpp"
|
||||
#include "src/testing/testing.hpp"
|
||||
// #include "src/faker/faker.hpp"
|
||||
#include "src/exceptions/exceptions.hpp"
|
||||
#include "src/classes/command.hpp"
|
||||
|
||||
#endif // __LPSTD_HPP__
|
||||
|
||||
175
src/classes/command.hpp
Normal file
175
src/classes/command.hpp
Normal file
@ -0,0 +1,175 @@
|
||||
#ifndef __LPSTD_COMMAND_H__
|
||||
#define __LPSTD_COMMAND_H__
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <list>
|
||||
#include <functional>
|
||||
#include <algorithm>
|
||||
#include <iterator>
|
||||
#include "../patterns/singleton.hpp"
|
||||
|
||||
struct commandInfo
|
||||
{
|
||||
std::string name;
|
||||
std::string description;
|
||||
void (*func)();
|
||||
std::vector<std::string> args;
|
||||
};
|
||||
|
||||
class Command : public lpstd::Singleton<Command>
|
||||
{
|
||||
|
||||
private:
|
||||
std::list<commandInfo> commands = {};
|
||||
std::string defaultCommandName = "help";
|
||||
|
||||
commandInfo currentCommand = {name : "",
|
||||
description : "",
|
||||
func : nullptr};
|
||||
|
||||
public:
|
||||
Command(/* args */)
|
||||
{
|
||||
}
|
||||
|
||||
~Command()
|
||||
{
|
||||
}
|
||||
|
||||
commandInfo getCurrentCommand()
|
||||
{
|
||||
return this->currentCommand;
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
bool existsArg(Args &&...arg)
|
||||
{
|
||||
|
||||
for (auto &argToFind : {arg...})
|
||||
{
|
||||
std::vector<std::string>::iterator it = std::find(this->currentCommand.args.begin(), this->currentCommand.args.end(), argToFind);
|
||||
if (it != this->currentCommand.args.end())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
std::string getArg(Args &&...arg)
|
||||
{
|
||||
|
||||
if (this->currentCommand.args.size() == 0)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
std::vector<std::string> args = this->currentCommand.args;
|
||||
std::vector<std::string> argsToFind = {arg...};
|
||||
std::vector<std::string> values = {};
|
||||
for (auto &argToFind : argsToFind)
|
||||
{
|
||||
std::vector<std::string>::iterator it = std::find(args.begin(), args.end(), argToFind);
|
||||
if (it != args.end())
|
||||
{
|
||||
values.push_back(*(it + 1));
|
||||
}
|
||||
}
|
||||
return values.size() > 0 ? values[0] : "";
|
||||
}
|
||||
|
||||
void addCommand(std::string name, std::string description, void (*func)(void))
|
||||
{
|
||||
commandInfo command;
|
||||
command.name = name;
|
||||
command.description = description;
|
||||
command.func = func;
|
||||
this->commands.push_back(command);
|
||||
}
|
||||
|
||||
// void addCommand(std::string name, std::string description, void (*func)(int argc, char *argv[]))
|
||||
// {
|
||||
// commandInfo command;
|
||||
// command.name = name;
|
||||
// command.description = description;
|
||||
// command.func = func;
|
||||
// this->commands.push_back(command);
|
||||
// }
|
||||
|
||||
std::string getHelpAsString()
|
||||
{
|
||||
std::string help = "";
|
||||
std::list<commandInfo> sortedCommands = this->commands;
|
||||
|
||||
sortedCommands.sort([](const commandInfo &a, const commandInfo &b)
|
||||
{ return a.name < b.name; });
|
||||
|
||||
for (auto &commandInfo : sortedCommands)
|
||||
{
|
||||
help += commandInfo.name + " - " + commandInfo.description + "\n";
|
||||
}
|
||||
|
||||
return help;
|
||||
}
|
||||
|
||||
void callCommand(std::string command, int argc, char *argv[])
|
||||
{
|
||||
std::vector<std::string> args;
|
||||
for (int i = 2; i < argc; i++)
|
||||
{
|
||||
args.push_back(argv[i]);
|
||||
}
|
||||
|
||||
for (auto &commandInfo : this->commands)
|
||||
{
|
||||
if (commandInfo.name == command)
|
||||
{
|
||||
commandInfo.args = args;
|
||||
currentCommand = commandInfo;
|
||||
commandInfo.func();
|
||||
return;
|
||||
}
|
||||
}
|
||||
std::cout << "Command not found" << std::endl;
|
||||
}
|
||||
|
||||
void execute(int argc, char *argv[])
|
||||
{
|
||||
|
||||
if (argc < 2)
|
||||
{
|
||||
std::cout << "Command not found \n"
|
||||
<< std::endl;
|
||||
// throw lpstd::exceptions::ParameterException("argc < 2");
|
||||
|
||||
this->callCommand(defaultCommandName, argc, argv);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
std::string command = argv[1];
|
||||
std::vector<std::string> args;
|
||||
for (int i = 2; i < argc; i++)
|
||||
{
|
||||
args.push_back(argv[i]);
|
||||
}
|
||||
|
||||
for (auto &commandInfo : this->commands)
|
||||
{
|
||||
if (commandInfo.name == command)
|
||||
{
|
||||
commandInfo.args = args;
|
||||
currentCommand = commandInfo;
|
||||
commandInfo.func();
|
||||
return;
|
||||
}
|
||||
}
|
||||
std::cout << "Command not found" << std::endl;
|
||||
}
|
||||
};
|
||||
|
||||
#endif // __LPSTD_COMMAND_H__
|
||||
@ -1,4 +1,6 @@
|
||||
|
||||
#ifndef __LPSTD_SINGLETON_HPP__
|
||||
#define __LPSTD_SINGLETON_HPP__
|
||||
namespace lpstd
|
||||
{
|
||||
template <typename T>
|
||||
@ -20,3 +22,5 @@ namespace lpstd
|
||||
Singleton &operator=(Singleton const &);
|
||||
};
|
||||
}
|
||||
|
||||
#endif // __LPSTD_SINGLETON_HPP__
|
||||
@ -3,8 +3,9 @@
|
||||
#include <functional>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <cstdio>
|
||||
#include <windows.h>
|
||||
#define needUTF8() (SetConsoleOutputCP(65001) && setvbuf(stdout, nullptr, _IOFBF, 1000))
|
||||
#else
|
||||
#define needUTF8()
|
||||
#endif
|
||||
|
||||
namespace lpstd
|
||||
@ -22,6 +23,9 @@ namespace lpstd
|
||||
|
||||
void drawResults()
|
||||
{
|
||||
|
||||
needUTF8();
|
||||
|
||||
std::cout << std::endl;
|
||||
if (Results.failed == 0)
|
||||
{
|
||||
@ -40,13 +44,7 @@ namespace lpstd
|
||||
Expect(T value)
|
||||
{
|
||||
|
||||
#ifdef _WIN32
|
||||
// Set console code page to UTF-8 so console known how to interpret string data
|
||||
SetConsoleOutputCP(CP_UTF8);
|
||||
|
||||
// Enable buffering to prevent VS from chopping up UTF-8 byte sequences
|
||||
setvbuf(stdout, nullptr, _IOFBF, 1000);
|
||||
#endif
|
||||
needUTF8();
|
||||
|
||||
this->value = value;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user