some changes && add command class

This commit is contained in:
lucaspalomodevelop 2023-10-29 21:19:37 +01:00
parent a62992c7dd
commit 179f7b4107
4 changed files with 191 additions and 9 deletions

View File

@ -2,10 +2,15 @@
#ifndef __LPSTD_HPP__ #ifndef __LPSTD_HPP__
#define __LPSTD_HPP__ #define __LPSTD_HPP__
#ifdef _WIN32
#include <cstdio>
#include <windows.h>
#endif
#include "src/patterns/singleton.hpp" #include "src/patterns/singleton.hpp"
#include "src/testing/testing.hpp" #include "src/testing/testing.hpp"
// #include "src/faker/faker.hpp" // #include "src/faker/faker.hpp"
#include "src/exceptions/exceptions.hpp" #include "src/exceptions/exceptions.hpp"
#include "src/classes/command.hpp"
#endif // __LPSTD_HPP__ #endif // __LPSTD_HPP__

175
src/classes/command.hpp Normal file
View 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__

View File

@ -1,4 +1,6 @@
#ifndef __LPSTD_SINGLETON_HPP__
#define __LPSTD_SINGLETON_HPP__
namespace lpstd namespace lpstd
{ {
template <typename T> template <typename T>
@ -20,3 +22,5 @@ namespace lpstd
Singleton &operator=(Singleton const &); Singleton &operator=(Singleton const &);
}; };
} }
#endif // __LPSTD_SINGLETON_HPP__

View File

@ -3,8 +3,9 @@
#include <functional> #include <functional>
#ifdef _WIN32 #ifdef _WIN32
#include <cstdio> #define needUTF8() (SetConsoleOutputCP(65001) && setvbuf(stdout, nullptr, _IOFBF, 1000))
#include <windows.h> #else
#define needUTF8()
#endif #endif
namespace lpstd namespace lpstd
@ -22,6 +23,9 @@ namespace lpstd
void drawResults() void drawResults()
{ {
needUTF8();
std::cout << std::endl; std::cout << std::endl;
if (Results.failed == 0) if (Results.failed == 0)
{ {
@ -40,13 +44,7 @@ namespace lpstd
Expect(T value) Expect(T value)
{ {
#ifdef _WIN32 needUTF8();
// 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
this->value = value; this->value = value;
} }