Skip to content

终端交互——inquire.js #41

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
youngwind opened this issue Mar 12, 2016 · 0 comments
Open

终端交互——inquire.js #41

youngwind opened this issue Mar 12, 2016 · 0 comments

Comments

@youngwind
Copy link
Owner

起因

前些日子在用yeoman自定义自己的脚手架的时候忽然对终端交互产生了兴趣。
这些下拉框,单选框,复选框到底是怎么做的呢?
2016-03-12 5 16 13
后来了解到yeoman用的是**inquirer.js**。

使用方法

1. 安装

npm install inquirer

2. 使用

var inquirer = require("inquirer");
var validator = require("validator");  // 我使用了validator校验手机号

var questions = [

 //是否类型
  {
    type: "confirm",
    name: "sex",
    message: "Are you male?",
    default: false
  },
// 输入类型,添加校验
  {
    type: "input",
    name: "phone",
    message: "What's your phone number",
    validate: function (value) {
      return validator.isMobilePhone(value, 'zh-CN') ? true : "Please enter a valid phone number";
    }
  },

  //select下拉选项
  {
    type: "list",
    name: "weight",
    message: "How much is your weight",
    choices: ["Large", "Middle", "Small"],
    filter: function (value) {
      return value.toLowerCase();
    }
  },
];

inquirer.prompt(questions, function (answers) {
  console.log(answers);
});

3. 结果

2016-03-12 10 10 35

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

1 participant