概念

inquirer.js 是一个用来实现命令行交互式界面的工具集合。
它包含以下功能:

  • 可以向用户提出问题
  • 解析用户输入的答案
  • 对用户的输入进行验证
  • 提供错误回调

    基本使用与常见属性

    ```javascript

    !/usr/bin/env node

var inquirer = require(‘inquirer’) inquirer .prompt([ { type: ‘editor’, //打开文本编辑器,input输入简单的形式,editor输入复杂的形式 name: ‘yourEditor’, message: ‘your editor:’, default: ‘haha’ //默认值 }, { type: ‘password’, //输入密码,看不见 name: ‘yourPassword’, message: ‘your password:’, default: ‘haha’ //默认值 }, { type: ‘checkbox’, name: ‘checkbox’, message: ‘yourCheckbox’, default: 0, choices: [ { value: 1, name: ‘apple’ }, { value: 2, name: ‘banana’ }, { value: 3, name: ‘pear’ } ] }, { type: ‘expand’, name: ‘expand’, message: ‘yourExpand’, default: ‘R’, choices: [ { key: ‘R’, value: ‘red’ }, { key: ‘B’, value: ‘blue’ }, { key: ‘W’, value: ‘white’ } ] }, { //rawlist与list相似 只是交互形式不同 type: ‘rawlist’, name: ‘rawlist’, message: ‘yourRawlist’, default: 0, //index下标 choices: [ { value: 1, name: ‘apple’ }, { value: 2, name: ‘banana’ }, { value: 3, name: ‘pear’ } ] }, { type: ‘list’, name: ‘list’, message: ‘yourList’, default: 0, //index下标 choices: [ { value: 1, name: ‘apple’ }, { value: 2, name: ‘banana’ }, { value: 3, name: ‘pear’ } ] }, { //confirm 二选一 type: ‘confirm’, name: ‘choice’, message: ‘yourChoice’, default: false }, { type: ‘input’, name: ‘yourName’, message: ‘your name:’, default: ‘haha’, //默认值 validate: val => { //校验 // return val === ‘hahah’ return typeof val === ‘string’ }, transformer: val => { //补充说明 return ‘name:’ + val }, filter: val => { //过滤 return ‘name[‘ + val + ‘]’ } }, { //会继续输入,不是数字会转换为数字 type: ‘number’, name: ‘num’, message: ‘yourNum’ } ]) .then(answers => { // console.log(answers.yourName) console.log(answers) }) .catch(error => { if (error.isTtyError) { // Prompt couldn’t be rendered in the current environment } else { // Something else went wrong } })

```