add helpcommand if argc < 2

This commit is contained in:
lucaspalomodevelop 2023-10-07 15:03:27 +02:00
parent 6589f344e4
commit 37786bc860
2 changed files with 27 additions and 10 deletions

1
.gitignore vendored
View File

@ -28,6 +28,7 @@
# Executables
*.exe
quicknote
*.out
*.app

View File

@ -22,6 +22,7 @@ class Command : public lpstd::Singleton<Command>
private:
std::list<commandInfo> commands = {};
std::string defaultCommandName = "help";
public:
Command(/* args */)
@ -44,27 +45,42 @@ public:
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)
for (auto &commandInfo : this->commands)
{
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.func(argc, argv);
return;
}
}
std::cout << "Command not found" << std::endl;
}
void execute(int argc, char *argv[])
{
if (argc < 2)
{
std::cout << "Command not found" << std::endl;
throw lpstd::exceptions::ParameterException("argc < 2");
std::cout << "Command not found \n" << std::endl;
// throw lpstd::exceptions::ParameterException("argc < 2");
this->callCommand(defaultCommandName, argc, argv);
return;
}