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è)類型類型,其他的有興趣再去了解:
input
confirm
list
checkbox
password
方法用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 puppeteer(headless chrome)實(shí)現(xiàn)網(wǎng)站登錄
這篇文章主要介紹了node puppeteer(headless chrome)實(shí)現(xiàn)網(wǎng)站登錄,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05NodeJS使用formidable實(shí)現(xiàn)文件上傳
這篇文章主要為大家詳細(xì)介紹了NodeJS使用formidable實(shí)現(xiàn)文件上傳的相關(guān)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10Nodejs + Websocket 指定發(fā)送及群聊的實(shí)現(xiàn)
這篇文章主要介紹了Nodejs + Websocket 指定發(fā)送及群聊的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01Node.js中使用Buffer編碼、解碼二進(jìn)制數(shù)據(jù)詳解
這篇文章主要介紹了Node.js中使用Buffer編碼、解碼二進(jìn)制數(shù)據(jù)詳解,Buffer支持ascii、utf8、ucs2、base64等編碼格式,需要的朋友可以參考下2014-08-08詳解nodejs微信公眾號(hào)開發(fā)——4.自動(dòng)回復(fù)各種消息
這篇文章主要介紹了詳解nodejs微信公眾號(hào)開發(fā)——4.自動(dòng)回復(fù)各種消息,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-04-04node.js中事件觸發(fā)器events的使用方法實(shí)例分析
這篇文章主要介紹了node.js中事件觸發(fā)器events的使用方法,結(jié)合實(shí)例形式分析了node.js事件觸發(fā)器events的功能、原理及基本使用方法,需要的朋友可以參考下2019-11-11node.js實(shí)現(xiàn)多圖片上傳實(shí)例
這篇文章主要介紹了node.js實(shí)現(xiàn)多圖片上傳實(shí)例,包括路由、控制器和視圖的源碼,重點(diǎn)在圖片上傳處理程序,需要的朋友可以參考下2014-06-06docker中編譯nodejs并使用nginx啟動(dòng)
這篇文章主要介紹了docker中編譯nodejs并使用nginx啟動(dòng)的相關(guān)資料,需要的朋友可以參考下2017-06-06