Git Product home page Git Product logo

cpp-inquirer's Introduction

C++ Inquirer

A single header C++11 library for interactive command line prompts.

Use

Simply include the header:

#include "inquirer.h"

And initialise the inquirer object (with optional title):

auto inquirer = alx::Inquirer("cpp-inquirer example");

There are two ways to get answers back:

// Adds a question.
inquirer.add_question({"query", "What do you want to do?" });
// Prompts the user with all questions at once.
inquirer.ask();
/* With the key of your question. You will get a std::string as a value, so you will need to convert
   it to an appropriate type yourself. */
inquirer.answer("query"); 

or

// Adds a question and immediately prompts the user for an answer.
std::string answer = inquirer.add_question({ "query", "What do you want to do?" }).ask();

If you mix both of these approaches the question already answered will not be asked again. If you wish to ask the same question more than once call:

inquirer.ask(true);

Alternatively, if you have unrelated question or prefer to manage them individually you can use the alx::Question class directly:

std::string answer = alx::Question{"key", "What do you want to do?"}.ask(); // Key is not used in this case, so any value will do.
// Or
alx::Question question{"key", "What do you want to do?"};
std::string answer = question.ask();

Prompts

Text

inquirer.add_question({ "query", "What do you want to do?" });

Yes/No

inquirer.add_question({ "birthday", "Is this for a birthday?", alx::Type::yesNo });

Integer or decimal

inquirer.add_question({ "candles", "How many candles do you want?", alx::Type::integer });
// Or
inquirer.add_question({ "candles", "How many candles do you want?", alx::Type::decimal });

Select

inquirer.add_question({ "type", "What kind of a cake would you like?",
		        std::vector<std::string>{ "Chocolate", "Ice-cream", "Cheesecake", "Red velvet" }});

Confirm

inquirer.add_question({ "delivery", "Is this for delivery?", alx::Type::confirm });

Regex validated input

inquirer.add_question({ "number", "Enter your contact details:", "\\d{9}" });

Password

Password questions hide user input.

inquirer.add_question({ "password", "Enter your password:", alx::Type::password });

You can keep asking for a password if it doesn't meet your criteria like this:

std::string pass;
alx::Question question{ "pass", "Password", alx::Type::password };
while (pass.empty()) // Your criteria here
    pass = question.ask(true);

This works for any other question type as well but would probably be most commonly used with passwords.

Contributing

I'm more than happy to accept pull requests with some minor requirements:

  • Prefix your commit messages with a relevant change type. If the commit relates to a GitHub issue, include the issue number in the message. Example:

Types: added a new question type 'yes/no'

This addresses issue #123

or

Answers: added a way to return an answer with the correct type

  • Please follow the naming convention already established in the code. There is no clangformat, but I'm sure you can deduce it yourself.

cpp-inquirer's People

Contributors

aelliixx avatar mrsimsure avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

mrsimsure

cpp-inquirer's Issues

Some type of question cause the asking process to stop silently

Hello, first of all, this library is really wonderful, i was looking exactly for something this simple and yet powerful to build my command line tools.
But i'm having some problems when using different type of questions, specifically if i use the 'text' type before every other question, everything works as excepted, but if use any of the other type before the 'text' one, the program stop silently without asking any question at all.
I'm pretty new to C++, so i don't know if this could be something wrong with my setup or the library itself.
Here an example of what i'm talking about, i pretty much just use the same code form your examples.

This way it works, and every question is asked one after another

auto inquirer = alx::Inquirer("");
		
inquirer.add_question({ "query", "What do you want to do?" });
inquirer.add_question({ "birthday", "Is this for a birthday?", alx::Type::yesNo });
inquirer.add_question({ "candles", "How many candles do you want?", alx::Type::integer });
inquirer.add_question({ "delivery", "Is this for delivery?", alx::Type::confirm });
inquirer.add_question({ "type", "What kind of a cake would you like?",
		    std::vector<std::string>{ "Chocolate", "Ice-cream", "Cheesecake", "Red velvet" }});

inquirer.ask();

But just doing this cause my problem, any other type of question does not work if a 'text' question is not asked before, and nothing come out of the terminal

auto inquirer = alx::Inquirer("");
		
//inquirer.add_question({ "query", "What do you want to do?" });
inquirer.add_question({ "birthday", "Is this for a birthday?", alx::Type::yesNo });
inquirer.add_question({ "candles", "How many candles do you want?", alx::Type::integer });
inquirer.add_question({ "delivery", "Is this for delivery?", alx::Type::confirm });
inquirer.add_question({ "type", "What kind of a cake would you like?",
		    std::vector<std::string>{ "Chocolate", "Ice-cream", "Cheesecake", "Red velvet" }});

inquirer.ask();

What could be the problem?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.