mirror of
https://github.com/Yet-Another-DreamTeam/quicknote.git
synced 2026-03-13 08:09:41 +00:00
modify command class
This commit is contained in:
parent
3beb0b269e
commit
ab657b1e13
@ -14,7 +14,8 @@ struct commandInfo
|
||||
{
|
||||
std::string name;
|
||||
std::string description;
|
||||
void (*func)(int argc, char *argv[]);
|
||||
void (*func)();
|
||||
std::vector<std::string> args;
|
||||
};
|
||||
|
||||
class Command : public lpstd::Singleton<Command>
|
||||
@ -24,6 +25,10 @@ private:
|
||||
std::list<commandInfo> commands = {};
|
||||
std::string defaultCommandName = "help";
|
||||
|
||||
commandInfo currentCommand = {name : "",
|
||||
description : "",
|
||||
func : nullptr};
|
||||
|
||||
public:
|
||||
Command(/* args */)
|
||||
{
|
||||
@ -33,7 +38,51 @@ public:
|
||||
{
|
||||
}
|
||||
|
||||
void addCommand(std::string name, std::string description, void (*func)(int argc, char *argv[]))
|
||||
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;
|
||||
@ -42,14 +91,22 @@ public:
|
||||
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;
|
||||
});
|
||||
sortedCommands.sort([](const commandInfo &a, const commandInfo &b)
|
||||
{ return a.name < b.name; });
|
||||
|
||||
for (auto &commandInfo : sortedCommands)
|
||||
{
|
||||
@ -59,7 +116,7 @@ public:
|
||||
return help;
|
||||
}
|
||||
|
||||
void callCommand(std::string command,int argc, char *argv[] )
|
||||
void callCommand(std::string command, int argc, char *argv[])
|
||||
{
|
||||
std::vector<std::string> args;
|
||||
for (int i = 2; i < argc; i++)
|
||||
@ -71,7 +128,9 @@ public:
|
||||
{
|
||||
if (commandInfo.name == command)
|
||||
{
|
||||
commandInfo.func(argc, argv);
|
||||
commandInfo.args = args;
|
||||
currentCommand = commandInfo;
|
||||
commandInfo.func();
|
||||
return;
|
||||
}
|
||||
}
|
||||
@ -83,7 +142,8 @@ public:
|
||||
|
||||
if (argc < 2)
|
||||
{
|
||||
std::cout << "Command not found \n" << std::endl;
|
||||
std::cout << "Command not found \n"
|
||||
<< std::endl;
|
||||
// throw lpstd::exceptions::ParameterException("argc < 2");
|
||||
|
||||
this->callCommand(defaultCommandName, argc, argv);
|
||||
@ -102,7 +162,9 @@ public:
|
||||
{
|
||||
if (commandInfo.name == command)
|
||||
{
|
||||
commandInfo.func(argc, argv);
|
||||
commandInfo.args = args;
|
||||
currentCommand = commandInfo;
|
||||
commandInfo.func();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@ -11,52 +11,61 @@ namespace commands
|
||||
|
||||
Command &command = Command::Instance();
|
||||
|
||||
void add(int argc, char *argv[])
|
||||
void NotImplemented()
|
||||
{
|
||||
std::cout << "add" << std::endl;
|
||||
std::cout << "command: '" << command.getCurrentCommand().name << "' is not implemented" << std::endl;
|
||||
throw NotImplementedException();
|
||||
}
|
||||
|
||||
void list(int argc, char *argv[])
|
||||
void add()
|
||||
{
|
||||
std::cout << "list" << std::endl;
|
||||
throw NotImplementedException();
|
||||
std::cout << command.getCurrentCommand().name << std::endl;
|
||||
|
||||
if (command.existsArg("-n", "--namespace"))
|
||||
{
|
||||
std::cout << "namespace: " << command.getArg("-n", "--namespace") << std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "namespace: "
|
||||
<< "default" << std::endl;
|
||||
}
|
||||
|
||||
// throw NotImplementedException();
|
||||
}
|
||||
|
||||
void view(int argc, char *argv[])
|
||||
void list()
|
||||
{
|
||||
std::cout << "view" << std::endl;
|
||||
throw NotImplementedException();
|
||||
NotImplemented();
|
||||
}
|
||||
|
||||
void edit(int argc, char *argv[])
|
||||
void view()
|
||||
{
|
||||
std::cout << "edit" << std::endl;
|
||||
throw NotImplementedException();
|
||||
NotImplemented();
|
||||
}
|
||||
|
||||
void remove(int argc, char *argv[])
|
||||
void edit()
|
||||
{
|
||||
std::cout << "remove" << std::endl;
|
||||
throw NotImplementedException();
|
||||
NotImplemented();
|
||||
}
|
||||
|
||||
void search(int argc, char *argv[])
|
||||
void remove()
|
||||
{
|
||||
std::cout << "search" << std::endl;
|
||||
throw NotImplementedException();
|
||||
NotImplemented();
|
||||
}
|
||||
|
||||
// not implemented
|
||||
void collect(int argc, char *argv[])
|
||||
void search()
|
||||
{
|
||||
std::cout << "collect" << std::endl;
|
||||
throw NotImplementedException();
|
||||
NotImplemented();
|
||||
}
|
||||
|
||||
void help(int argc, char *argv[])
|
||||
void collect()
|
||||
{
|
||||
NotImplemented();
|
||||
}
|
||||
|
||||
void help()
|
||||
{
|
||||
// std::cout << "help" << std::endl;
|
||||
std::cout << command.getHelpAsString() << std::endl;
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user