• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

node:inquirer基本用法和常用属性入门

武飞扬头像
无懈可击者
帮助3

简介

用户与命令行交互的工具

安装

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
系列文章
更多 icon
同类精品
更多 icon
继续加载