文章目录
外部依赖包
commander - 命令行参数解析
chalk - 文字颜色修改
log-symbols - 命令行图标
inquirer 命令行交互
- 安装
npm i -S inquirer
- 使用
var inquirer = require('inquirer'); inquirer .prompt([ /* 问题对象数组 */ ]) .then(answers => { /* 用户的反馈 */ }) .catch(error => { if(error.isTtyError) { /* 无法在当前环境中呈现的提示 */ } else { /* 其它一些错误 */ } });
问题(Questions)
属性 | 说明 |
---|---|
type: (String) | 提问的类型。默认: input; 值:input, number, confirm, list, rawlist, expand, checkbox, password, editor |
name: (String) | 在最后获取到的answers回答对象中,作为当前这个问题的键. |
message: (String|Function) | 打印出的问题。 如果定义为函数, 第一个参数将是当前查询程序会话的答案。默认为name值(后跟冒号). |
default: (String|Number|Boolean|Array|Function) | 默认值, 或使用函数 return 个值. 如果定义为函数, 第一个参数将是当前查询程序会话的答案。 |
choices: (Array|Function) | 给出一个选择的列表. 如果定义为函数, 第一个参数将是当前查询程序会话的答案。为数组时,数组的每个元素可以为基本类型中的值。 |
validate: (Function) | 接受用户输入,并且当值合法时,函数返回true。当函数返回false时,一个默认的错误信息会被提供给用户。 |
filter: (Function) | 接受用户输入并且将值转化后返回填充入最后的answers对象内。 |
transformer: (Function) | 接收用户输入,回答和选项标志,并返回一个转换后的值以显示给用户。转换只影响编辑时显示的内容。它不修改答案。 |
when: (Function|Boolean) | 接受当前用户输入的answers对象,并且通过返回true或者false来决定是否当前的问题应该去问。也可以是简单类型的值。 |
pageSize: (Number) | 改变渲染list、rawlist、expand 或者 checkbox 时的行数的长度。 |
prefix: (String) | 改变默认前缀消息 |
suffix: (String) | 改变默认后缀消息. |
askAnswered: (Boolean) | 如果答案已经存在,则强制提示该问题。 |
loop: (Boolean) | 启用列表循环。默认值:true |
注意:default, choices, validate, filter, when 等函数都可以异步调用。并返回 promise 或用this.async() 获取 callback 来调用最终值。
{
/* 首选使用 promise */
filter: function () {
return new Promise(/* etc... */);
},
/* 也可以使用 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);
}
}
答案(Answers)
Answers是一个包含有用户客户端输入的每一个问题的答案的对象
键:问题对象的name属性
值:取决于问题的类型,confirm类型为Boolean,Input类型为用户输入的字符串,rawlist和list类型为选中的值,也为字符串类型。
分隔符(Separator)
可以为任意的choices数组选项添加分隔,方便在多选项时划分选项类别
问题类型(Prompt types)
- List - {type: ‘list’}
问题对象中有 type, name, message, choices[, default, filter, loop] 等属性(default 必须为在choices 数组中的位置索引 或 选择值)
- Raw List - {type: ‘rawlist’}
问题对象中有 type, name, message, choices[, default, filter, loop] 等属性(default 必须为在choices 数组中的位置索引)
- Expand - {type: ‘expand’}
问题对象中有 type, name, message, choices[, default] 等属性(default 必须为在choices 数组中的位置索引,如果为定义key,则使用 default )
在choices 属性中需要增加一个属性:key,这个属性用于快速选择问题的答案。同时这个属性值必须为一个小写字母,h 是系统添加的,用户不要定义。
- Checkbox - {type: ‘checkbox’}
问题对象中有 type, name, message, choices[, filter, validate, default, loop] 等属性。默认值为数组中有 checked: true 的值
- Confirm - {type: ‘confirm’}
问题对象中有 type, name, message [, default] 等属性。默认值为布尔型
- Input - {type: ‘input’}、
问题对象中有 type, name, message[, default, filter, validate, transformer] 等属性。
- Input - {type: ‘number’}
问题对象中有 type, name, message[, default, filter, validate, transformer] 等属性。 - Password - {type: ‘password’}
问题对象中有 type, name, message, mask,[, default, filter, validate] 等属性。
- Editor - {type: ‘editor’}
问题对象中有 type, name, message[, default, filter, validate] 等属性。
终端打开用户默认编辑器,如vim,notepad。并将用户输入的文本传回