add basic commands

This commit is contained in:
lucaspalomodevelop 2023-10-03 13:56:00 +02:00
parent 988f9bdec3
commit ac9831542f
5 changed files with 114 additions and 10 deletions

7
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,7 @@
{
"files.associations": {
"*.html": "html",
"*.jsste": "plaintext",
"sstream": "cpp"
}
}

View File

@ -4,13 +4,61 @@
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <list>
#include <functional>
#include <algorithm>
#include <iterator>
struct commandInfo
{
std::string name;
std::string description;
void (*func)(int argc, char *argv[]);
};
class Command
{
private:
std::list<commandInfo> commands = {};
public:
Command(/* args */)
{
}
~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);
}
void execute(int argc, char *argv[])
{
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.func(argc, argv);
return;
}
}
std::cout << "Command not found" << std::endl;
}
};
#endif // COMMAND_H

45
src/commands.h Normal file
View File

@ -0,0 +1,45 @@
#ifndef COMMANDS_H
#define COMMANDS_H
#include <iostream>
namespace commands
{
void add(int argc, char *argv[])
{
std::cout << "add" << std::endl;
}
void list(int argc, char *argv[])
{
std::cout << "list" << std::endl;
}
void view(int argc, char *argv[])
{
std::cout << "view" << std::endl;
}
void edit(int argc, char *argv[])
{
std::cout << "edit" << std::endl;
}
void remove(int argc, char *argv[])
{
std::cout << "remove" << std::endl;
}
void search(int argc, char *argv[])
{
std::cout << "search" << std::endl;
}
void help(int argc, char *argv[])
{
std::cout << "help" << std::endl;
}
} // namespace commands
#endif

View File

@ -2,15 +2,17 @@
int main(int argc, char *argv[])
{
// command.addCommand("add", "add a note", add);
// command.addCommand("list", "list all notes", list);
// command.addCommand("view", "remove a note", view);
// command.addCommand("edit", "edit a note", edit);
// command.addCommand("remove", "remove a note", remove);
// command.addCommand("search", "search quicknote", search);
// command.addCommand("help", "show help", help);
command.addCommand("add", "add a note", commands::add);
command.addCommand("list", "list all notes", commands::list);
command.addCommand("view", "remove a note", commands::view);
command.addCommand("edit", "edit a note", commands::edit);
command.addCommand("remove", "remove a note", commands::remove);
command.addCommand("search", "search quicknote", commands::search);
command.addCommand("help", "show help", commands::help);
command.execute(argc, argv);
setup.runSetup();
std::cout << "quicknote!" << std::endl;
// std::cout << "quicknote!" << std::endl;
return 0;
}

View File

@ -1,5 +1,7 @@
#include <iostream>
#include "./setup.h"
#include "./command.h"
#include "./commands.h"
Setup setup;
// Command command;
Command command;