zoukankan      html  css  js  c++  java
  • noode inquirer

    一、

    由于交互的问题种类不同,inquirer为每个问题提供很多参数:

    type:表示提问的类型,包括:input, confirm, list, rawlist, expand, checkbox, password, editor;
    name: 存储当前问题回答的变量;
    message:问题的描述;
    default:默认值;
    choices:列表选项,在某些type下可用,并且包含一个分隔符(separator);
    validate:对用户的回答进行校验;
    filter:对用户的回答进行过滤处理,返回处理后的值;
    transformer:对用户回答的显示效果进行处理(如:修改回答的字体或背景颜色),但不会影响最终的答案的内容;
    when:根据前面问题的回答,判断当前问题是否需要被回答;
    pageSize:修改某些type类型下的渲染行数;
    prefix:修改message默认前缀;
    suffix:修改message默认后缀。

    #!/usr/bin/env node
    const inquirer = require('inquirer');
    
    const promtList = [
        {
            type: 'input',
            message: '输入用户名:',
            name: 'name',
            default: 'shangyy'
        },
        {
            type: 'password',
            message:  '输入密码',
            name: 'password',
            validate: function(val){
                if(val === '123456') return  true;
                return '密码错误,请重新输入';
            }
        },
        {
            type: 'input',
            message: function(params){
                return `输入${params.name}年龄`
            },
            name: 'age',
            default: '18',
            validate: function(value){
                if(value > 0 && value < 100) return true;
                return "请输入1~99位数字";
            }
        },
        {
            type: 'confirm',
            message: '是否监听',
            name: 'isWatch',  // 直接回车 yes
        },
        {
            type: 'list',
            message: 'install tool',
            name: 'tool',
            choices: [
                'npm',
                'yarn',
                'cnpm'
            ],
            filter: function(value){
                return value;
            },
            when: function(answers){
                return answers.isWatch;
            }
    
        }
    ]
    inquirer.prompt(promtList).then(answers => {
        console.log("answers",answers)
    });
  • 相关阅读:
    时间选择框(可用于Form)
    点击复制指定内容
    ajax中多个模板之间套用ajax
    Java学习路径
    Windows平台安装Python
    Python语法-第2关
    Python语法-第1关
    Python语法-第0关
    图像识别
    wx:for用法
  • 原文地址:https://www.cnblogs.com/shangyueyue/p/10506587.html
Copyright © 2011-2022 走看看