inquirerjs 是什么
Inquirerjs
是一个用来实现命令行交互式界面的工具集合。它帮助我们实现与用户的交互式交流,比如给用户提一个问题,用户给我们一个答案,我们根据用户的答案来做一些事情,典型应用如 plop
等生成器工具。
它有以下特色:
-
提供错误回调
-
可以提出问题
-
解析输入的答案
-
验证答案
-
管理分层提示
文档
安装
$ npm install inquirer
使用
var inquirer = require('inquirer');
inquirer
.prompt([
/* 在这里配置您的问题(可以设置多个,它们将按顺序向用户提出) */
])
.then(answers => {
// 回调,对用户输入的答案就行处理
});
方法
inquirer
有以下可用方法:
inquirer.prompt(questions) -> promise
启动命令行提问界面
questions
: 数组类型,每一个数组项是一个问题,这些问题将按照在数组中的顺序依次向用户提问- 返回一个
Promise
对象
inquirer.registerPrompt(name,prompt)
注册一个提问类型插件
-
naem
: 字符串,提问类型名字,在question
的type
中使用 -
prompt
: 对象,它自身的提问器。
inquirer.createPromptModule() -> prompt function
创建一个包含自己的提问器模块。当你覆盖一个已有的提问类型或添加一个新的提问类型时,如果不想影响其他依赖于inquirer
的库,则可以使用此方法 。
var prompt = inquirer.createPromptModule();
prompt(questions).then(/* ... */);
对象
问题对象
问题对象是包含与问题相关的值的散列
属性 | 类型 | 可选值 | 默认值 | 说明 |
---|---|---|---|---|
type | String | input … | input | 问题类型 |
name | String | 当把用户答案加入散列时使用的名称,如果名称包含".",将定义一个路径 | ||
message | String|Funcion | name | 要打印的问题信息。如果是函数,第一个参数是上一个问题的答案 | |
default | All | 如果没有任何键入,将此值作为答案的默认值,如果是函数,第一个参数是上一个问题的答案 | ||
choices | Array|Function | 选择题的选项,一个数组,或返回一个数组的函数,如果是函数,第一个参数是上一个问题的答案 | ||
validate | Function | 一个函数,用来验证用户的输入值 | ||
filter | Function | 过滤器函数,返回过滤后的答案 | ||
transformer | Function | 转换函数,对答案或选项进行转换 | ||
when | Function,Boolean | 接收当前用户的答案哈希,并应根据是否要询问此问题返回true或false。 该值也可以是一个简单的布尔值 | ||
pageSize | Number | 更改使用list、rawList、expand或checkbox时要呈现的行数 | ||
prefix | String | 更改默认前缀消息 | ||
suffix | String | 更改默认后缀消息。 |
default
,choices
,validate
,filter
以及when
的值为函数时,可以异步调用,也可以返回一个Promise
或者使用this.async()
方法来获得一个回调,然后使用最终值来调用它。
{
/* Preferred way: with promise */
filter() {
return new Promise(/* etc... */);
},
/* Legacy way: with this.async */
validate: function (input) {
// Declare function as asynchronous, and save the done callback
var done = this.async();
// Do async stuff
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);
}
}
答案
一个键值对散列,包含了每一个提问从客户端获得的答案
- Key
question
对象的name
属性 - Value (取决于提问类型)
confirm
(Boolean)input
用户输入的字符串number
:用户输入的数字rawlist
,list
: 用户选择的选项
分隔符
可以在choices
数组的任意位置加入分隔符:
// In the question object
choices: [ "Choice A", new inquirer.Separator(), "choice B" ]
// Which'll be displayed this way
[?] What do you want to do?
> Order a pizza
Make a reservation
--------
Ask opening hours
Talk to the receptionist
提问器类型
注: 方括号([])内的选项是可选的。其他的为必须
List-{type:'list'}
接受type
,name
,message
,choices
[,default
,filter
]等属性
~/Documents/oss/Inquirer.js/examples master*
❯node list.js
?What do you want to do? Order a pizza
?What size do you need?
Jumbo
Large
❯ Standard
Medium
Small
Micro
#### Raw List-`{type:"rawlist"}`
接受type
,name
,message
,choices
[,default
,filter
]等属性
~/Documents/oss/Inquirer.js/examples master*
❯node rawlist.js
?What do you want to do? Order a pizza
?What size do you need?
1) Jumbo
2) Large
❯ 3) Standard
4) Medium
5) Small
6) Micro
#### Expand - `{type:'expand'}`
接受type
,name
,message
,choices
[,default
,filter
]等属性.default
必须为choise
数组的某个索引,如果没有提供默认值,则help
会作为默认值。
expand
类型的提问器的choices
属性可以有一个额外的参数key
.这个参数的值必须是单个字母。h
选项是提问器自己添加到,使用者无法将选项定义为h
。
~/Documents/oss/Inquirer.js master*
❯ node examples/expand.js
? Conflict on `file.js`: (yadxH) y
>> Overwrite
~/Documents/oss/Inquirer.js master*
❯ node examples/expand.js
? Conflict on `file.js`: (yadxH)
y)Overwrite
a)Overwrite this one and all next
d) Show diff
──────────────
x) Abort
h) Help, list all options
Answer: d
#### Checkbox - `{type:'checkbox'}`
接受type
,name
,message
,choices
[,default
,validate
,filter
]等属性. default
应该是choices
数组的其中一个值。
所有选项中,默认值将被默认选中。
当disabled
属性为真值时将无法选择。 如果disabled是一个字符串,那么这个字符串将在disabled选项旁边输出,否则它将默认为" disabled " 。disabled
属性也可以是同步函数,以参数的形式接收当前的答案并返回布尔值或字符串
❯ node examples/checkbox.js
? Select toppings
◉ Cheddar
◯ Parmesan
= The usual =
❯ ◉ Mushroom
◯ Tomato
= The extras =
◯ Pineapple
(Move up and down to reveal more choices)
#### Confirm - `{type:'confirm'}`
接受type
,name
,message
,choices
[,default
]等属性. default
应该是一个布尔值。
~/Documents/oss/Inquirer.js master*
❯ node examples/pizza.js
Hi,welcome to Node Pizza
? Is this for delivery? (y/N)
#### Input - `{type:'input'}`
接受type
,name
,message
[,default
,filter
,validate
,transformer
]等属性. default
应该是一个布尔值。
~/Documents/oss/Inquirer.js master*
❯ node examples/input.js
? What's your first name Simon
? What's your last name Doe
? What's your phone number
>> Please enter a valid phone number
#### Input - `{type:'number'}`
接受type
,name
,message
[,default
,filter
,validate
,transformer
]等属性
#### Password - {type:'password'}
接受type
,name
,message
,mask
[,default
,filter
,validate
]等属性
~/Documents/oss/Inquirer.js master*
❯ node examples/password.js
? Enter a password [hidden]
? Enter a masked password **********
mask
选项被用来隐藏用户的输入。
#### Editor - `{type: 'editor'}`
接受type
,name
,message
[,default
,filter
,validate
]等属性
在临时文件上启动用户首选编辑器的实例 。 一旦用户退出编辑器,临时文件的内容将作为结果被读入。要使用的编辑器是通过读取 V I S U A L 或 VISUAL或 VISUAL或 editor环境变量来确定的 。 如果两者都不存在,则使用notepad(在Windows上)或vim(在Linux或Mac上)
用户界面与布局
除了提问器,Inquirer
还提供了一些基本的文本UI界面
Bottom Bar - inquirer.ui.BottomBar
这个UI在一个自由文本区域的底部显示一个固定的文本。这对于将消息保持在屏幕底部非常有用,同时将命令输出放在较高的部分 .
var ui = new inquirer.ui.BottomBar();
// pipe a Stream to the log zone
outputStream.pipe(ui.log);
// Or simply write output
ui.log.write('something just happened.');
ui.log.write('Almost over, standby!');
// During processing, update the bottom bar content to display a loader
// or output a progress bar, etc
ui.updateBottomBar('new bottom bar content');
实时接口
在内部,Inquirer使用JS reactive扩展来处理事件和异步流
这意味着您可以利用这个特性来提供更高级的流。例如,您可以动态地添加要询问的问题
var prompts = new Rx.Subject();
inquirer.prompt(prompts);
// At some point in the future, push new questions
prompts.next({
/* question... */
});
prompts.next({
/* question... */
});
// When you're done
prompts.complete();
使用返回值的process
属性,您可以访问更细粒度的回调
inquirer.prompt(prompts).ui.process.subscribe(onEachAnswer, onError, onComplete);
插件
autocomplete
显示用户类型的选项列表,与其他包(如fuzzy)兼容(用于搜索)
checkbox-plus
带有自动完成和其他附加功能的复选框列表
datetime
使用数字键盘和箭头键可定制的日期/时间选择器
inquirer-select-line
提示在数组中选择添加新元素的索引
command
简单的提示与命令历史和动态自动完成
inquirer-fuzzy-path
提示选择模糊的文件/目录
其它插件略去,如有兴趣,可查阅官方文档