node如何實(shí)現(xiàn)cmd彈窗交互之inquirer
node實(shí)現(xiàn)cmd彈窗交互——inquirer
實(shí)現(xiàn)cmd彈窗交互
安裝inquirer包
npm i inquirer
引入inquirer包
var inquirer = require('inquirer');
// console.log('Hi, welcome to Node Pizza');
var questions = [
{
type: 'input',
name: 'toBeDelivered',//這個(gè)參數(shù)
message: '請(qǐng)選擇文件夾',
}
];
inquirer.prompt(questions).then(answers => {
console.log(answers);
});
questions為配置參數(shù)對(duì)象
type:(String)提示的類型。默認(rèn)值:input-可能的值:input,number,confirm, list,rawlist,expand,checkbox,password,editorname:(String)將答案存儲(chǔ)在答案哈希中時(shí)使用的名稱。如果名稱包含句點(diǎn),它將在答案哈希中定義路徑message:(String | Function)要打印的問題。如果定義為函數(shù),則第一個(gè)參數(shù)將是當(dāng)前查詢者會(huì)話答案。缺省值為name(后跟冒號(hào))。
node命令交互inquirer
用過vue或者react的用腳手架新建項(xiàng)目的應(yīng)該都進(jìn)行過命令交互,vue創(chuàng)建的時(shí)候會(huì)讓你選擇vue2還是vue3,也有多選要什么配置,也有輸入y或者n選擇是否用history路由等,這其實(shí)用inquire這個(gè)包都能實(shí)現(xiàn)。
環(huán)境跟之前commander使用是一樣的,初始化之后配置bin和npm link一下,這邊就不再說了。
安裝inquirer
npm install inquirer
引入
var inquirer = require(‘inquirer');
inquirer主要知道這幾個(gè)類型類型,其他的有興趣再去了解:
inputconfirmlistcheckboxpassword
方法用prompt就行,另外兩個(gè)registerPrompt和createPromptModule也可以自己去了解。
我們按照順序都展示出來,不管輸入還是選擇了什么,都繼續(xù)下一種類型展示,代碼:
typeInput();
function typeInput() {
inquirer.prompt([ {
name: 'input',
type: 'input',
message: 'input: year, month and day',
default: 'year'
}]).then((res) => {
console.log('Year: ' + res.input);
typeConfirm();
})
}
function typeConfirm(){
inquirer.prompt([ {
name: 'confirm',
type: 'confirm',
message: 'confirm',
default: true
}]).then((res) => {
console.log('confirm: ' + res.confirm);
typeList();
})
}
function typeList(){
inquirer.prompt([ {
name: 'list',
type: 'list',
message: 'list',
choices: ['red', 'blue', 'yellow'],
default: 1
}]).then((res) => {
console.log('list: ' + res.list);
typeCheckbox();
})
}
function typeCheckbox(){
inquirer.prompt([ {
name: 'checkbox',
type: 'checkbox',
message: 'checkbox',
choices: ['red', 'blue', 'yellow'],
default: ['blue']
}]).then((res) => {
console.log('checkbox: ' + res.checkbox);
typePassword();
})
}
function typePassword(){
inquirer.prompt([ {
name: 'password',
type: 'password',
message: 'password',
mask: false //是否出現(xiàn)*號(hào)
}]).then((res) => {
console.log('password: ' + res.password);
})
}
prompt方法返回的是Promise,用的時(shí)候也可以配合async和await,返回的字段就是name字段:
typeCheckbox();
async function typeCheckbox() {
let {checkbox} = await inquirer.prompt([
{
name: 'checkbox',
type: 'checkbox',
message:'checkbox',
choices: ['red', 'blue', 'yellow'],
default: ['blue']
}
]);
console.log('checkbox ' + checkbox);
}
效果:

commander和inquirer可以說是命令行交互最基本的兩個(gè)包,這兩個(gè)包的基本用法已經(jīng)足夠我們?nèi)ラ_發(fā)一個(gè)cli的命令行交互操作。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
node.js實(shí)現(xiàn)學(xué)生檔案管理
這篇文章主要為大家詳細(xì)介紹了node.js實(shí)現(xiàn)學(xué)生檔案管理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
解決node報(bào)錯(cuò)Error: Cannot find module http-e
這篇文章主要介紹了解決node報(bào)錯(cuò)Error: Cannot find module http-errors的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-05-05
autojs的nodejs打包成品app經(jīng)驗(yàn)分享
這篇文章主要為大家介紹了autojs的nodejs打包成品app經(jīng)驗(yàn)分享,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
使用NodeJS?5分鐘?連接?Redis?讀寫操作的詳細(xì)過程
這篇文章主要介紹了NodeJS?5分鐘?連接?Redis?讀寫操作,本文給大家介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07
WebSocket+node.js創(chuàng)建即時(shí)通信的Web聊天服務(wù)器
這篇文章主要為大家詳細(xì)介紹了WebSocket+node.js創(chuàng)建即時(shí)通信的Web聊天服務(wù)器的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-08-08
MQTT Client實(shí)現(xiàn)消息推送功能的方法詳解
這篇文章主要介紹了MQTT Client實(shí)現(xiàn)消息推送功能的方法,結(jié)合實(shí)例形式詳細(xì)分析了MQTT Client實(shí)現(xiàn)消息推送的基本原理、實(shí)現(xiàn)方法與相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2023-05-05
Nodejs 獲取時(shí)間加手機(jī)標(biāo)識(shí)的32位標(biāo)識(shí)實(shí)現(xiàn)代碼
本文給大家分享nodejs獲取時(shí)間加手機(jī)標(biāo)識(shí)的32位標(biāo)識(shí)實(shí)現(xiàn)代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2017-03-03
nodejs教程之制作一個(gè)簡單的文章發(fā)布系統(tǒng)
本文主要講述了使用nodejs制作一個(gè)簡單的文章發(fā)布系統(tǒng),使用mongodb數(shù)據(jù)庫,時(shí)間比較緊,功能做的也比較簡單,僅僅是增刪改查,外加附近上傳,有相同需求的小伙伴可以參考下2014-11-11

