Inquirer. Js tries to make a beautiful command line interface for NodeJs that can be embedded. The diagram below:

It is very easy to do the following things:

  • Provides error callbacks
  • Ask the operator questions
  • Gets and parses user input
  • Check whether the user’s answer is valid
  • Manage multiple levels of prompts

Note: Inquirer. Js just gives users a nice interface and a way to ask questions. If you are looking for a full-fledged command line debugger, I recommend commander, Vorpal, and args.

The installation

npm install inquirer
Copy the code

Write a small demo — index.js

var inquirer = require('inquirer') inquirer.prompt([ { type: 'confirm', name: 'test', message: 'Are you handsome?', default: true}]). Then ((answers) => {console.log(' answers :') console.log(answers)}).Copy the code

Run a wave

node index.js
Copy the code

The output

Result: {test: false}Copy the code

As you can see from this small demo, Inquirer’s prompt method makes it possible to ask the user questions from the command line.

The Questions — the question

Both the title of the question and the default result value can be preset. When the answer is complete, a Promise object is returned, and all the answers entered by the user are retrieved in its then method. The argument passed to the Prompt method is an array of question questions, each element of which is a question object. It contains the following attributes:

{type: String, // Indicates the type of question, as explained separately below. Name: String, // In the answers object that gets the last answer, as the key message for the current question: String | Function, the problem of / / print title, if for Function default: String | Number | Array | Function, / / the user does not enter to answer it, the default value of the problem. Or use a function to return a default value. If is a function, the first argument of the function is the input answer to the current question. Choices: Array | Function, / / given a selection list, if is a Function, enter the answer to the question of the first parameter to the current. When is an array, each element of the array can be a value in the base type. Validate: Function, // Accepts user input, and returns true if the value is valid. When the function returns false, a default error message is provided to the user. Filter: Function, // takes user input and converts the value back to populate the last answers object. When: the Function | Boolean, / / accept the answers for the current user input object, and returns true or false to decide whether the current questions should go to ask. It can also be a simple type of value. PageSize: Number, // Change the length of lines rendered to list, rawList,expand, or checkbox. }Copy the code

Functions such as default, choices, validate, filter, and when can be called asynchronously. Return either a promise or a callback to get the final value using this.async().

Function () {return new promise (/* etc... * /); }, /* You can also use this.async */ validate: function (input) { // Declare function as asynchronous, and save the done callback var done = this.async(); SetTimeout (function () {if (typeof input! == 'number') { // Pass the return value in the done callback done('You need to provide a number'); return; } // Pass the return value in the done callback done(null, true); }, 3000); }}Copy the code

Answers – answer

Answers is an object key containing the answer to each question entered by the user client: the value of the name property of the question object: Depending on the type of question, confirm is a Boolean, Input is a string entered by the user, and rawList and list are selected values, which are also strings.

The Separator – separated

You can delimit any of the choices array options to make it easier to categorize options when there are multiple choices

Prompt types — Problem types

  • List

    {type: 'list'}

Problems in the object must have a type, name, message, choices and other properties, at the same time, the default option must be the default position in the array of choices index (Boolean)

  • Raw list {type: ‘rawList ‘} is similar to list except that list is printed as an unordered list while rawList is printed as an ordered list

  • Expand {type: ‘Expand ‘} also generates the list, but you need to add a key to the Choices property, which is used to quickly select the answer to the question. Something like alias or shorthand. The property value must also be a lowercase letter

  • Checkbox {type: ‘Checkbox ‘} The rest of the items are similar to the list except that the selection is a Checkbox. Also in the Choices array, the options with the checked: True attribute are the default values.

  • Confirm {type: ‘Confirm ‘} The answer to the question is Y/N. If there is a default attribute, the attribute value should be Boolean

  • Input {type: ‘Input ‘} gets the user Input string

  • Password {type: ‘Password ‘} is similar to input, except that user input appears XXXX on the command line

  • Editor {type: ‘Editor ‘} Terminal opens the user’s default Editor, such as vim, notepad. And returns the text entered by the user

support

All of the following CLIS have very good support.

  • Mac OS
    • Terminal.app
    • iTerm
  • Windows:
    • ConEmu
    • cmd.exe
    • Powershell
    • Cygwin
  • Linux (Ubuntu, openSUSE, Arch Linux, etc):
    • gnome-terminal (Terminal GNOME)
    • konsole