A collection of function to make an interactive CLI. Inspired by Enquirer.js.
Full demo
Have use Terminalizer to record the demo.
For CMake based projects, you can simply use FetchContent
this way:
FetchContent_Declare(
enquirer
GIT_REPOSITORY https://github.com/Gashmob/Enquirer.git
GIT_TAG v1.0.2
)
FetchContent_GetProperties(enquirer)
if(NOT enquirer_POPULATED)
message(STATUS "Fetching enquirer...")
FetchContent_Populate(enquirer)
add_subdirectory(${enquirer_SOURCE_DIR} ${enquirer_BINARY_DIR})
endif()
target_link_libraries(my_target PUBLIC enquirer)
Then the include path is just #include <enquirer.h>
Please note :
- All functions are in the
enquirer
namespace. - All functions are synchronous (wait until user submit).
You can try the demo by building the demo
target.
Ask the user for username and password.
Prototypes
There is 2 prototypes :
- Returns the username and password as a
std::pair
- Takes a predicate as arguments and return its result.
std::pair<std::string, std::string> auth(const std::string &id_prompt = "Username",
const std::string &pw_prompt = "Password",
char mask = '*');
bool auth(const std::function<bool(const std::pair<std::string, std::string> &)> &predicate,
const std::string &id_prompt = "Username",
const std::string &pw_prompt = "Password",
char mask = '*');
Example
bool is_valid = enquirer::auth([](const std::pair<std::string, std::string> &credentials) {
return credentials.first == "admin"
&& credentials.second == "admin";
});
Result
Prompt the question and autocomplete the answer with a list of choices.
Prototype
std::string autocomplete(const std::string& question,
const std::string choices[] = {},
int limit = 10);
Example
std::string answer = enquirer::autocomplete("What is you favorite fruit", {
"Apple",
"Banana",
"Blueberry",
"Cherry",
"Orange",
"Pear",
"Raspberry",
"Strawberry"
});
Result
Ask the user to confirm.
Prototype
bool confirm(const std::string &question,
bool default_value = false);
Example
bool quit = false;
while (!quit) {
quit = enquirer::confirm("Do you want to quit?");
}
Result
Multi-prompt for user
Prototype
std::map<std::string, std::string> form(const std::string &question,
const std::vector<std::string> &inputs);
Example
auto answers = enquirer::form("Please provide some informations:", {
"Firstname",
"Lastname",
"Username"
});
Result
Prompt the question and return the answer.
Prototype
std::string input(const std::string &question,
const std::string &default_value = "");
Example
std::string answer = enquirer::input("What is your name?", "John Doe");
Result
Hides the user input
Prototype
std::string invisible(const std::string &question);
Example
std::string secret = enquirer::invisible("What is your secret?");
Result
Same as Input, but split the user input around ,
.
Prototype
std::vector<std::string> list(const std::string &question);
Example
auto keywords = enquirer::list("Type comma separated keywords");
Result
Allow user to select several items from a list
Prototype
std::vector<std::string> multi_select(const std::string &question,
const std::vector<std::string> &choices);
Example
auto choices = enquirer::multi_select("Choose some colors", {
"Red",
"Green",
"Blue",
"Yellow",
"Magenta",
"Cyan",
"White",
"Black"
});
Result
Ask the user for a number
Prototype
template<typename N,
typename = typename std::enable_if<std::is_arithmetic<N>::value>::type>
N number(const std::string &question);
Example
auto pi = enquirer::number<double>("What is the value of PI?");
Result
Mask the user input with *
.
Prototype
std::string password(const std::string &question,
char mask = '*');
Example
auto pwd = enquirer::password("What is your password?");
Result
Multi-choice quiz !
Prototype
bool quiz(const std::string &question,
const std::vector<std::string> &choices,
const std::string &correct);
Example
if (enquirer::quiz("Which is yellow?", {"Banana", "Coconut", "Strawberry"}, "Banana"))
std::cout << "Good answer!" << std::endl;
else
std::cout << "Bad answer!" << std::endl;
Result
Allow user to choose a value in a range.
Prototype
template<typename N,
typename = typename std::enable_if<std::is_arithmetic<N>::value>::type>
N slider(const std::string &question,
N min_value,
N max_value,
N step,
N initial_value);
Example
int value = enquirer::slider<int>("How much do you want?", 0, 10, 1, 1);
Result
Choose one item from a list.
Prototype
std::string select(const std::string &question,
const std::vector<std::string> &choices);
Example
auto language = enquirer::select("Which is the best one?", {
"c++",
"python",
"java"
});
Result
Choose between two values.
Prototype
bool toggle(const std::string &question,
const std::string &enable,
const std::string &disable,
bool default_value = false);
Example
bool light = enquirer::toggle("Light?", "On", "Off", true);
Result
All tests are run for each push via GitHub Actions on Ubuntu and macOS.
The tests sources are located in tests/test.cpp
and use
a simple c++ test framework. You can run the tests by building the test
target.