最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Conditional Prompt rendering in Inquirer? - Stack Overflow

programmeradmin0浏览0评论

I am building a command line interface in node.js using library: inquirer.

based on my need I want to render prompt, confirmation text etc when user input's. example.

inquirer usage

var _questions = [{
  'type': 'list',
  'name': 'databasetype',
  'message': 'Choose database :',
  'choices': ['mongoDB', 'mysql [alpha]', 'firebase [alpha]', 'url [alpha]'],
  'default': 'mongoDB'
}, {
 'type': 'input',
 'name': 'xfactor',
 'message': 'X Factor [email, username etc..] :'
}]

// show question's.
Inquirer.prompt(_questions).then(async (__answers) => {
 console.log(__answers)
})

what i want

if user chooses mongoDB than it should render another prompt asking mongodb url

I am building a command line interface in node.js using library: inquirer.

based on my need I want to render prompt, confirmation text etc when user input's. example.

inquirer usage

var _questions = [{
  'type': 'list',
  'name': 'databasetype',
  'message': 'Choose database :',
  'choices': ['mongoDB', 'mysql [alpha]', 'firebase [alpha]', 'url [alpha]'],
  'default': 'mongoDB'
}, {
 'type': 'input',
 'name': 'xfactor',
 'message': 'X Factor [email, username etc..] :'
}]

// show question's.
Inquirer.prompt(_questions).then(async (__answers) => {
 console.log(__answers)
})

what i want

if user chooses mongoDB than it should render another prompt asking mongodb url

Share Improve this question asked Jun 2, 2019 at 5:50 Manu YadavManu Yadav 1251 silver badge10 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 24

You can use the when question property, its value should be a function that returns a boolean; true for show question, false for don't show question

so using your example:

_questions = [{
    type: 'list',
    name: 'databasetype',
    message: 'Choose database :',
    choices: ['mongoDB', 'mysql [alpha]', 'firebase [alpha]', 'url [alpha]'],
    default: 'mongoDB'
}, {
   type: 'input',
   name: 'url',
   message: 'Enter the URL',
   when: (answers) => answers.databasetype === 'mongoDB'
}]

see more examples here when usage examples

发布评论

评论列表(0)

  1. 暂无评论