node如何實現(xiàn)cmd彈窗交互之inquirer
node實現(xiàn)cmd彈窗交互——inquirer
實現(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',//這個參數(shù)
message: '請選擇文件夾',
}
];
inquirer.prompt(questions).then(answers => {
console.log(answers);
});
questions為配置參數(shù)對象
type:(String)提示的類型。默認值:input-可能的值:input,number,confirm, list,rawlist,expand,checkbox,password,editorname:(String)將答案存儲在答案哈希中時使用的名稱。如果名稱包含句點,它將在答案哈希中定義路徑message:(String | Function)要打印的問題。如果定義為函數(shù),則第一個參數(shù)將是當前查詢者會話答案。缺省值為name(后跟冒號)。
node命令交互inquirer
用過vue或者react的用腳手架新建項目的應(yīng)該都進行過命令交互,vue創(chuàng)建的時候會讓你選擇vue2還是vue3,也有多選要什么配置,也有輸入y或者n選擇是否用history路由等,這其實用inquire這個包都能實現(xiàn)。
環(huán)境跟之前commander使用是一樣的,初始化之后配置bin和npm link一下,這邊就不再說了。
安裝inquirer
npm install inquirer
引入
var inquirer = require(‘inquirer');
inquirer主要知道這幾個類型類型,其他的有興趣再去了解:
inputconfirmlistcheckboxpassword
方法用prompt就行,另外兩個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)*號
}]).then((res) => {
console.log('password: ' + res.password);
})
}
prompt方法返回的是Promise,用的時候也可以配合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可以說是命令行交互最基本的兩個包,這兩個包的基本用法已經(jīng)足夠我們?nèi)ラ_發(fā)一個cli的命令行交互操作。
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
在Node.js下運用MQTT協(xié)議實現(xiàn)即時通訊及離線推送的方法
這篇文章主要介紹了在Node.js下運用MQTT協(xié)議實現(xiàn)即時通訊及離線推送的方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01
nodejs個人博客開發(fā)第六步 數(shù)據(jù)分頁
這篇文章主要為大家詳細介紹了nodejs個人博客開發(fā)的數(shù)據(jù)分頁,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04
詳解nodejs通過代理(proxy)發(fā)送http請求(request)
本篇文章主要介紹了nodejs通過代理(proxy)發(fā)送http請求(request),具有一定的參考價值,有興趣的可以了解一下2017-09-09
Nest.js參數(shù)校驗和自定義返回數(shù)據(jù)格式詳解
這篇文章主要給大家介紹了關(guān)于Nest.js參數(shù)校驗和自定義返回數(shù)據(jù)格式的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03

