node:inquirer基本用法和常用属性入门
简介
用户与命令行交互的工具
安装
cnpm install -S inquirer
简单案例(input)
const inquirer = require('inquirer')
inquirer
.prompt([{
type: 'input', // 类型
name: 'yourName', // 字段名称,在then里可以打印出来
message: 'your name:' // 提示信息
}])
.then(answers => {
console.log('answers', answers.yourName) // 与prompt的name字段对应
})
.catch(error => {
if(error.isTtyError) {
// Prompt couldn't be rendered in the current environment
} else {
// Something else went wrong
}
});
执行结果:
综合案例
输入字符串和数字(input、number)
inquirer
// 可支持多个输入
.prompt([{
type: 'input', // 类型
name: 'yourName', // 字段名称,在then里可以打印出来
message: 'your name:', // 提示信息
default: 'noname', // 默认值
validate: function (v) {// 校验:当输入的值为是string类型,才能按回车,否则按了回车并无效果
return typeof v === 'string'
},
transformer: function (v) {// 提示信息(输入的信息后缀添加(input your name))
return v '(input your name)'
},
filter: function (v) {// 最终结果
return 'name[' v ']'
}
}, {
type: 'number', // 类型(数字)
name: 'num',
message: 'your number'
}])
.then(answers => {
console.log('answers', answers.yourName) // 与prompt的name字段对应
})
.catch(error => {
if(error.isTtyError) {
// Prompt couldn't be rendered in the current environment
} else {
// Something else went wrong
}
});
执行结果:
确认框(confirm)
inquirer
.prompt([{
type: 'confirm',
name: 'choice',
message: 'your choice:',
default: false,
}])
.then(answers => {
console.log('answers', answers)
})
.catch(error => {
if(error.isTtyError) {
// Prompt couldn't be rendered in the current environment
} else {
// Something else went wrong
}
});
执行结果:
选择框(list)
inquirer
.prompt([{
type: 'list',
name: 'choice',
message: 'your choice:',
default: 0,
choices: [
{ value: 1, name: 'hjy' },
{ value: 2, name: 'lio' }
]
}])
.then(answers => {
console.log('answers', answers)
})
.catch(error => {
if(error.isTtyError) {
// Prompt couldn't be rendered in the current environment
} else {
// Something else went wrong
}
});
执行结果:
单选框1(rawlist)
inquirer
.prompt([{
type: 'rawlist',
name: 'choice',
message: 'your choice:',
default: 0,
choices: [
{ value: 1, name: 'hjy' },
{ value: 2, name: 'lio' }
]
}])
.then(answers => {
console.log('answers', answers)
})
.catch(error => {
if(error.isTtyError) {
// Prompt couldn't be rendered in the current environment
} else {
// Something else went wrong
}
});
执行结果:
单选框2,选择h会展开所有的列表(expand)
inquirer
.prompt([{
type: 'expand',
name: 'choice',
message: 'your choice:',
default: 'red',
choices: [
{ key: 'R', value: 'red' },
{ key: 'G', value: 'green' },
{ key: 'B', value: 'blue' },
]
}])
.then(answers => {
console.log('answers', answers)
})
.catch(error => {
if(error.isTtyError) {
// Prompt couldn't be rendered in the current environment
} else {
// Something else went wrong
}
});
执行结果:
复选框(用空格进行选中)(checkbox)
inquirer
.prompt([{
type: 'checkbox',
name: 'choice',
message: 'your choice:',
default: 0,
choices: [
{ value: 1, name: 'hjy' },
{ value: 2, name: 'lio' }
]
}])
.then(answers => {
console.log('answers', answers)
})
.catch(error => {
if(error.isTtyError) {
// Prompt couldn't be rendered in the current environment
} else {
// Something else went wrong
}
});
执行结果:
密码(password)
inquirer
.prompt([{
type: 'password',
name: 'choice',
message: 'your choice:'
}])
.then(answers => {
console.log('answers', answers)
})
.catch(error => {
if(error.isTtyError) {
// Prompt couldn't be rendered in the current environment
} else {
// Something else went wrong
}
});
执行结果:
打开编辑器(mac系统:wq保存;而window系统是打开记事本)(editor)
inquirer
.prompt([{
type: 'editor',
name: 'choice',
message: 'your choice:'
}])
.then(answers => {
console.log('answers', answers)
})
.catch(error => {
if(error.isTtyError) {
// Prompt couldn't be rendered in the current environment
} else {
// Something else went wrong
}
});
执行结果:
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanheiaggh
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01