kazhala / InquirerPy

:snake: Python port of Inquirer.js (A collection of common interactive command-line user interfaces)
https://inquirerpy.readthedocs.io
MIT License
341 stars 18 forks source link

prompt() doesn't accept a dict of existing answers #40

Open irons opened 2 years ago

irons commented 2 years ago

In PyInquirer, I'm benefiting from the ability to pass the same dict into prompt() that gets returned:

answers = prompt(questions, answers)

This lets me seed initial defaults from CLI options, and operate successive groups of questions that can reference past answers. Here's the PyInquirer implementation. I don't see anything comparable for InquirerPy (looking here), so I'm merging my own dicts between prompt invocations. Which seems surprisingly fiddly in python until one can require 3.9.

Thanks for your time.

kazhala commented 2 years ago

Hi @irons

You are correct, at the moment this feature does not exist, however looks like it's quite a straightforward feature and reasonable use-case. I can look into including it in the next release.

The only concern or edge case I can think about at the moment is what if the provided answer is using the "index" as the key (question without "name") in the answer dictionary. Do you always explicitly define the name of each question, what do you think would be appropriate when let's say the name of the question is the same (perhaps simply override it)?

# answer of first batch of questions
answers = {
    "question1": True
}

# second batch of questions
answers = prompt([{"name": "question1", "type": "input", "message": "Name:"}], answers=answers)

Thanks, Kevin

irons commented 2 years ago

Thanks. I do define question names consistently, because I nearly always ask more than one question at a time, so the names are how I get the answers out — until I tried it just now, I didn't know it would generate indexes in lieu of names.

If I re-use a name in a subsequent question, clobbering the existing value would be the outcome I'd expect, just as if I'd used the same name twice in a single block of questions.