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)

综合案例

输入字符串和数字(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)

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值