pregunta
Hassle free CLI input.
Slim chainable library, when you need something like npm init
, to interactively gather input.
installation
npm i pregunta --save
usage
Example: npm init
var pregunta = pregunta
Note: Use .var('variableName')
to specify the name of the variable to hold the answer, otherwise the question itself will be the name of the variable.
Yes or No questions
You can use the yn
method to ask yes/no questions. The second argument if set becomes the default value
var pregunta = pregunta
Choices
You can use the choose
to ask for a choice. The third argument is the default, it can be either a string or an index.
var pregunta = pregunta
Validation
The validate
method can be use to set validation for input. The value can be either a RegExp to test the answer or a Function that receives the answer as the first argument and must return either true or false. Note that the ask
method can accept validation as the third argument or you can simply append a validate
method to set the validation.
The invalid
method sets a message to print out when the validation fails. It can be a String, where if it contains a %s
character, it will be replaced with the erroneous answer given. Its value can also be a Function whose first argument will be the erroneous answer and must return a string to print out or a falsy value to not print out a message.
var pregunta = var REG_EMAIL = /^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$/ipregunta
Prompt
The say
method is used to print out messages. The message piggybacks the last question asked and appears beforehand, i.e before the prompt is shown for that question
var pregunta = var pokemon = 'pikachu' 'bulbasaur' 'charmander'var statement = 'choose a pokemon:\n\n\t' + pokemon + '\n\n' pregunta
pregunta API
.ask(question [, default, validation ])
.ask(String) .ask(String, String) .ask(String, String, RegExp) .ask(String, String, Function)
.yn(question [, default ])
.yn(String) .yn(String, Boolean) .yn(String, String)
.choose(question, choices [, default ])
.choose(String, Array) .choose(String, Array, Number) .choose(String, Array, String)
.var(variable_name)
.var(String)
.default(value)
.default(String)
.validate(validation)
.validate(RegExp) .validate(Function)
.invalid(message)
.invalid(String) .invalid(Function)
.say(statement)
.say(String)
.done(callback)
.done(Function)
More cats
If you're looking for something more robust check out inquirer, they have a sweet collection of CLI user interfaces.