Skip to content

Instantly share code, notes, and snippets.

@TheEpicFace007
Created February 23, 2021 01:53
Show Gist options
  • Save TheEpicFace007/fb10cd475d3acc43214f5b64504c895c to your computer and use it in GitHub Desktop.
Save TheEpicFace007/fb10cd475d3acc43214f5b64504c895c to your computer and use it in GitHub Desktop.
Cli menu
#pragma once
#include <functional>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <list>
struct option
{
std::string name;
std::string desc;
std::function<void()> onCall;
};
class CliMenu
{
std::list<option> options = std::list<option>();
public:
void addOption(const char* name, const char *desc, std::function<void()> onCall);
void generateMenu();
void doOption();
void run();
~CliMenu()
{}
};
inline void CliMenu::addOption(const char *name, const char *desc, std::function<void()> onCall)
{
this->options.push_back({ name, desc, onCall });
}
inline void CliMenu::generateMenu()
{
std::cout << "Options:\n"
<< "------------------------------------\n";
std::cout << "quit" << " - " << "quit the app" << "\n";
for (auto const &i : this->options)
std::cout << i.name << " - " << i.desc << "\n";
}
inline void CliMenu::doOption()
{
std::string choosedOne;
std::cin >> choosedOne;
option o;
for (auto const &i : this->options)
{
if (i.name == choosedOne)
{
i.onCall();
break;
}
else
{
if (i.name == "quit")
this->~CliMenu();
}
}
std::cout << "\n";
this->generateMenu();
}
inline void CliMenu::run()
{
std::string choosedOne;
do
{
this->generateMenu();
std::cin >> choosedOne;
option o;
for (auto const &i : this->options)
{
if (i.name == choosedOne)
{
i.onCall();
break;
}
}
std::cout << "\n";
} while (choosedOne != "quit");
this->~CliMenu();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment