Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Enhancement/discussion: change in flow based on answers. #45

Closed
CDHDeveloper opened this issue Jul 7, 2013 · 4 comments
Closed

Enhancement/discussion: change in flow based on answers. #45

CDHDeveloper opened this issue Jul 7, 2013 · 4 comments

Comments

@CDHDeveloper
Copy link

As pizza.js shows things work fine as long as question flow is in a strictly linear fashion. But consider where the answer to one question affects the flow of another question. For example lets say that Node Pizza also offers calzones and subs and salads. Then a user would need to be asked what type of thing they want to order and based on that they would have a subset of questions to answer (perhaps coming back to the original series of questions to finish up/continue).

Could that be handled with some alterations to the inquirier module without adversely affecting the linear/serial flow of questions?

@SBoudrias
Copy link
Owner

Hi, Inquirer prompt offer such a possibility through the when function. https://github.com/SBoudrias/Inquirer.js#question

For example:

inquirer.prompt([{
  name: "food",
  type: "list",
  message: "What do you want",
  choices: [ "Calzone", "Pizza", "Sub" ]
}, {
  name: "toppings",
  type: "list",
  when: function( answers ) {
    // Only run if user answered Pizza to the first prompt
    return answers.food === "Pizza";
  },
  // etc
}]);

Does it work for your use case?

@CDHDeveloper
Copy link
Author

Choosing Calzone (or Pizza or Sub) returns exception generated from base.js:83
throw new Error("You must provide a " + name + " parameter");

So based on that no.

For the use case as far as I see it, you want to be able to dog leg to another set of questions (like asking what you want on your calzone) then go back to asking any further questions. So really what is needed (thinking out loud) is a way to diverge to another question set and come back. Also you need to be able to specify those other questions in a reasonably easy to maintain manner. You have the "when" but what about the "then", is what I guess I'm saying.

@SBoudrias
Copy link
Owner

That's because I haven't feed every required field in my example (so I put // etc... to be filled with your logic).

I see what you mean, but this is 100% possible with the current when implementation. It may not feel like the cleanest way to you, but it helps maintaining long prompt array without entering a nested hell. Plus, it keeps the Inquirer API surface minimal (easier to read and understand).

Try it out with when, it'll work out. If you need help down the road, just wave an arm over here and I'll be happy to land a hand (You may also find me on #yeoman on IRC under the pseudo sboudrias).

@dgrcode
Copy link

dgrcode commented May 31, 2023

I think this should be in the documentation. I've also found a separate issue opened for this purpose #1081

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants