一个nodejs的交互控件:Inquirer

一个交互式的nodejs控件包:Inquirer
更新于: 2022-06-06 00:45:28

安装

npm install inquirer

常用控件列表

  • list/rawlist: 单选
  • input: 输入字符串
  • number: 输入int/float型
  • confirm: bool 型
名称使用代码使用截图
list
var inquirer = require('inquirer');

inquirer.prompt([
  {
    name: 'type',
    type: 'list',
    message: 'Select your IDE?',
    choices: [{ value: 'idea' }, { value: 'pycharm' }]
  }
]).then(res => {
  console.log(res);
});
input/number
var inquirer = require('inquirer');

inquirer.prompt([
  {
    name: 'ipt1',
    type: 'input',
    message: 'Input your keywords?',
  }
]).then(res => {
  console.log(res);
});
confirm
inquirer.prompt([
  {
    name: 'ipt1',
    type: 'confirm',
    message: 'Confirm?',
  }
]).then(res => {
  console.log(res);
});
rawlist
inquirer.prompt([
  {
    name: 'input1',
    type: 'rawlist',
    message: 'Select your IDE?',
    choices: [{ name: 'Jetbrain IDEA', value: 'idea' }, { name: 'Python pycharm', value: 'pycharm' }]
  }
]).then(res => {
  console.log(res);
});
password
inquirer.prompt([
  {
    name: 'pwd',
    type: 'password',
    message: 'Input your password?',
  }
]).then(res => {
  console.log(res);
});

参考