inquirer.js一個用戶與命令行交互的工具詳解
寫在前面:
開始通過npm init 創(chuàng)建package.json的時候就有大量與用戶的交互(當然也可以通過參數(shù)來忽略輸入);而現(xiàn)在大多數(shù)工程都是通過腳手架來創(chuàng)建的,使用腳手架的時候最明顯的就是與命令行的交互,如果想自己做一個腳手架或者在某些時候要與用戶進行交互,這個時候就不得不提到inquirer.js了。
零. 介紹
由于交互的問題種類不同,inquirer為每個問題提供很多參數(shù):
- type:表示提問的類型,包括:input, confirm, list, rawlist, expand, checkbox, password, editor;
- name: 存儲當前問題回答的變量;
- message:問題的描述;
- default:默認值;
- choices:列表選項,在某些type下可用,并且包含一個分隔符(separator);
- validate:對用戶的回答進行校驗;
- filter:對用戶的回答進行過濾處理,返回處理后的值;
- transformer:對用戶回答的顯示效果進行處理(如:修改回答的字體或背景顏色),但不會影響最終的答案的內(nèi)容;
- when:根據(jù)前面問題的回答,判斷當前問題是否需要被回答;
- pageSize:修改某些type類型下的渲染行數(shù);
- prefix:修改message默認前綴;
- suffix:修改message默認后綴。
上面的屬性(除
transformer外)在下面都有對應(yīng)使用。
一. 使用
0. 語法結(jié)構(gòu)
const inquirer = require('inquirer');
const promptList = [
// 具體交互內(nèi)容
];
inquirer.prompt(promptList).then(answers => {
console.log(answers); // 返回的結(jié)果
})
1. input
const promptList = [{
type: 'input',
message: '設(shè)置一個用戶名:',
name: 'name',
default: "test_user" // 默認值
},{
type: 'input',
message: '請輸入手機號:',
name: 'phone',
validate: function(val) {
if(val.match(/\d{11}/g)) { // 校驗位數(shù)
return val;
}
return "請輸入11位數(shù)字";
}
}];
效果:

2. confirm
const promptList = [{
type: "confirm",
message: "是否使用監(jiān)聽?",
name: "watch",
prefix: "前綴"
},{
type: "confirm",
message: "是否進行文件過濾?",
name: "filter",
suffix: "后綴",
when: function(answers) { // 當watch為true的時候才會提問當前問題
return answers.watch
}
}];
效果:

3. list
const promptList = [{
type: 'list',
message: '請選擇一種水果:',
name: 'fruit',
choices: [
"Apple",
"Pear",
"Banana"
],
filter: function (val) { // 使用filter將回答變?yōu)樾?
return val.toLowerCase();
}
}];
效果:

4. rawlist
const promptList = [{
type: 'rawlist',
message: '請選擇一種水果:',
name: 'fruit',
choices: [
"Apple",
"Pear",
"Banana"
]
}];
效果:

5. expand
const promptList = [{
type: "expand",
message: "請選擇一種水果:",
name: "fruit",
choices: [
{
key: "a",
name: "Apple",
value: "apple"
},
{
key: "O",
name: "Orange",
value: "orange"
},
{
key: "p",
name: "Pear",
value: "pear"
}
]
}];
效果:

6. checkbox
const promptList = [{
type: "checkbox",
message: "選擇顏色:",
name: "color",
choices: [
{
name: "red"
},
new inquirer.Separator(), // 添加分隔符
{
name: "blur",
checked: true // 默認選中
},
{
name: "green"
},
new inquirer.Separator("--- 分隔符 ---"), // 自定義分隔符
{
name: "yellow"
}
]
}];
// 或者下面這樣
const promptList = [{
type: "checkbox",
message: "選擇顏色:",
name: "color",
choices: [
"red",
"blur",
"green",
"yellow"
],
pageSize: 2 // 設(shè)置行數(shù)
}];
效果:

7. password
const promptList = [{
type: "password", // 密碼為密文輸入
message: "請輸入密碼:",
name: "pwd"
}];
效果:

8. editor
const promptList = [{
type: "editor",
message: "請輸入備注:",
name: "editor"
}];
效果:

寫在后面:
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
localStorage設(shè)置有效期和過期時間的簡單方法
眾所周知前端三大緩存,cookie,sessionStorage,localStorage,下面這篇文章主要給大家介紹了關(guān)于localStorage設(shè)置有效期和過期時間的相關(guān)資料,需要的朋友可以參考下2022-02-02
JS中的算法與數(shù)據(jù)結(jié)構(gòu)之二叉查找樹(Binary Sort Tree)實例詳解
這篇文章主要介紹了JS中的算法與數(shù)據(jù)結(jié)構(gòu)之二叉查找樹(Binary Sort Tree),結(jié)合實例形式詳細分析了二叉查找樹(Binary Sort Tree)的原理、定義、遍歷、查找、插入、刪除等常見操作技巧,需要的朋友可以參考下2019-08-08
js window.onload 加載多個函數(shù)和追加函數(shù)詳解
本篇文章主要是對js window.onload 加載多個函數(shù)和追加函數(shù)進行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-01-01
使用jsonp實現(xiàn)跨域獲取數(shù)據(jù)實例講解
這篇文章主要介紹了使用jsonp實現(xiàn)跨域獲取數(shù)據(jù)實例講解,需要的朋友可以參考下2016-12-12
Auntion-TableSort國人寫的一個javascript表格排序的東西
Auntion-TableSort國人寫的一個javascript表格排序的東西...2007-11-11
瀏覽器環(huán)境下JavaScript腳本加載與執(zhí)行探析之defer與async特性
defer和async特性相信是很多JavaScript開發(fā)者"熟悉而又不熟悉"的兩個特性,從字面上來看,二者的功能很好理解,分別是"延遲腳本"和"異步腳本"的作用2016-01-01

