Auto Menu is an open-source library to automatically generate a text based interface for your programs and libraries.
Clone the repository and copy the src/automenu.hpp
and src/automenu.cpp
files to your working directory.
To import automenu into your project, run add the following to the top of the program.
#include<path_to_dir/automenu.hpp>
The library is inside a namespace am
. To access the contents of the library, either use scope resolution operator am::
, or add the following to your code.
using namespace am;
-
Create an object of Menu class
am::Menu nameOfMenu;
-
Set title (optional)
nameOfMenu.setTitle("Title");
-
Create menu entries Each enty is of type
std::par<void(*)(),std::string>
. For ease of use, a typedefam::MENU_ITEM
exists.void(*)()
is the pointer to a function with no arguments and return type void. The second parameter is astd::string
that storers the description for the entry. To pass the function, you may also use lambda functions.Example:
am::MENU_ITEM item1= {[](){std::cout<<"Hello World";}, "sayHello"}; am::MENU_ITEM item2 = {functionPointer, "Using Function Pointer"} std::pair<void(*)(),std::string> item3 = {[](){ std::cout<<"Using lambda function"; },"Lamda Function"}
-
Add menu entries
There are two ways to add menu items:- Individually, using
am::Menu::addItem
function.am::newMenu.addItem(nameOfEntry);
- Using
vector<am::MENU_ITEM>
vectoram::MENU_ITEM menuVector = {item1, {functionPointer, description}, {{std::cout<<"Lamda";},"Lamda"}};
am::newMenu.bulkAdd(menuVector);
- Individually, using
-
Set Clear (optional):
If you want the output to be erased after execution of an option, you need to add the following to your code.am::newMenu.setClear(true);
-
Run the menu once:
am::newMenu.once();
-
Run menu loop:
am::newMenu.run();
View examples folder.