简介
用户与命令行交互的工具
安装
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)
综合案例
输入字符串和数字(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
}
});
执行结果:
实现输入字符串和数字(input、number)
确认框(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
}
});
执行结果:
实现确认框(confirm)
选择框(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
}
});
执行结果:
实现选择框(list)
单选框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
}
});
执行结果:
单选(rawlist)
单选框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
}
});
执行结果:
选择框,选择h会展开所有的列表(expand)
复选框(用空格进行选中)(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
}
});
执行结果:
复选框(用空格进行选中)(checkbox)
密码(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
}
});
执行结果:
密码(password)
打开编辑器(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
}
});
执行结果:
打开编辑器(mac系统:wq保存;而window系统是打开记事本)(editor)
网友评论