inquirer user and command line interaction tool

Weekly Downloads: 20,787,079

1, Installation

npm i -S inquirer

2, All type usage examples

var inquirer = require('inquirer');

const questions = [
  {
    type: 'confirm',
    name: 'order',
    message: 'Hello, may I take your order?',
    default: true,
  },
  {
    type: 'number',
    name: 'amount',
    message: 'How many of you?',
    default: 1,
  },
  {
    type: 'list',
    name: 'mainFood',
    message: 'What do you need for staple food?',
    choices: ['Rice', 'Noodle', 'Pizza'],
    // Convert the user's answer and return the converted result
    filter(val) {
      return val.toLowerCase();
    },
    default: 'Pizza', // Note: the default value is the value before conversion
  },
  {
    type: 'rawlist',
    message: 'Pizza What size do you want?',
    name: 'size',
    choices: ['5 inch', '6 inch', '7 inch'],
    when(answers) {
      return answers.mainFood === 'pizza';
    },
    default: 1, // default is the index of the option in the choices array
  },
  {
    type: 'checkbox',
    name: 'menu',
    message: 'What would you like to eat?',
    choices: [
      {
        name: 'Dongpo's braised pork',
        checked: true,
      },
      {
        name: 'Steamed Fish Head with Diced Hot Red Peppers',
      },
      {
        name: 'French foie gras',
        disabled: 'out of stock',
      },
      {
        name: 'Scrambled egg with tomato',
      },
    ],
  },
  {
    type: 'expand',
    name: 'drinks',
    message: 'What would you like to drink?',
    choices: [
      {
        key: 'a', // key must be a single lowercase character
        name: 'Wheat tea',
        value: 'XiaoMaiCha',
      },
      {
        key: 'b',
        name: 'Sprite',
        value: 'XueBi',
      },
      {
        key: 'c',
        name: 'minutemaid',
        value: 'GuoLiCheng',
      },
    ],
    default: 0, // The default value must be an option   Index in the choices array
  },
  {
    type: 'input',
    name: 'vipCard',
    message: "Please enter your membership card number",
    validate(value) {
      const pass = /^\d{8}$/.test(value);
      if (pass) {
        return true;
      }

      return 'Membership card number is 8 digits!';
    },
  },
  {
    type: 'password',
    name: 'pwd',
    message: 'Please enter your membership card password',
    mask: '*',
    validate(value) {
      let valid = value.length === 6;

      return valid || 'Password must be 6 digits!';
    },
  },
  {
    type: 'editor',
    name: 'suggest',
    message: 'Do you have any suggestions for our store?',
  },
];

inquirer
  .prompt(questions)
  .then((answers) => {
    console.log(answers)
  })
  .catch((error) => {
    if (error.isTtyError) {
      // Prompt couldn't be rendered in the current environment
    } else {
      // Something else went wrong
    }
  });

answers print results:  

{
  order: true,
  amount: 1,
  mainFood: 'pizza',
  size: '6 inch',
  menu: [ 'Dongpo's braised pork', 'Steamed Fish Head with Diced Hot Red Peppers', 'Scrambled egg with tomato' ],
  drinks: 'XiaoMaiCha',
  vipCard: '12345678',
  pwd: '123456',
  suggest: 'The service was very friendly and considerate!\n'
}

3, inquirer.prompt() syntax analysis

The array in inquirer.prompt() can define problems. Each array element is an object, and an array element represents a problem. The attributes that can be defined for each problem object are:

  • Type: (String) the type of prompt. The default is input. The optional values are: input, number, confirm, list, rawlist, expand, checkbox, password and editor
  • message: (String|Function) description of the problem
  • name: (String) key in answers object
  • choices: (Array|Function) option
  • default: (String|Number|Boolean|Array|Function)   Default value when the user does not answer
  • filter: (Function) converts the user's answer and returns the converted result to answers
  • when: (Function, Boolean) judge whether to display the current question according to the answer answered by the previous user
  • validate: (Function) verifies the user's answer
  • transformer: process the display effect of the user's answer (for example, modify the font or background color of the answer), but it will not affect the content of the final answer
  • pageSize: modifies the number of render lines under certain type types
  • Prefix: modify the default prefix of message
  • Suffix: modify the default suffix of message

type: 'confirm'

When the type is confirm, you need to set the type, name, message[, default] attributes. The default must be Boolean. The default value is displayed in uppercase, such as "Y" in the following figure.

type: 'number'

When the type is checkbox, you need to set the type, name, message[, default, filter, validate, transformer] attributes.

 type: 'list'

When the type is list, you need to set the type, name, message, choices[, default, filter, loop] attributes. Default can be   The index or value in the choices array.

 

 type: 'rawlist'

When the type is rawList, you need to set the type, name, message, choices[, default, filter, loop] attribute, default   Only   Index in the choices array.

type: 'checkbox'

When the type is checkbox, you need to set the type, name, message, choices[, filter, validate, default, loop] attribute. The default must be   An array of value s in choices.

The choices array element is an object that must have a name attribute. The checked: true option is selected by default. The option to convert the disabled property value to Boolean true will be disabled when   When the value of disabled is String, this value will be displayed after the option.  

type: 'expand'

When the type is expand, you need to set the type, name, message, choices[, default] attributes. The choices array element is an object, and each object must contain   key, name, value attributes. The key of the default option will be capitalized, such as A below.  

The H option will be added by default. If the default value is not set, press enter or enter h to list help. The help list is as follows:

 

type: 'input'

When type is input, you need to set type, name, message[, default, filter, validate, transformer]   Properties.

type: 'password' 

When the type is password, it needs to be set   type, name, message, mask[, default, filter, validate] attributes. Mask is the character that replaces the password.

type: 'editor'

When the type is editor, it needs to be set   type, name, message[, default, filter, validate, postfix] attributes.

Enter vim mode for editing when entering.

validate verify answer

If the verification fails, a red prompt will appear on the command line. If the process does not exit, you can continue to enter the answer

Separator option separator

Delimiters can be added to any   In the choices array.

choices: [ 'apple', new inquirer.Separator('------'), 'orange' ]

Tags: node.js

Posted on Sun, 28 Nov 2021 10:19:39 -0500 by Loki