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

Enhancements to prompt() to add if condition, repeated questions, and list/dict types #243

Open
RhetTbull opened this issue Jul 30, 2022 · 1 comment
Labels
Enhancement New feature or request

Comments

@RhetTbull
Copy link
Contributor

Describe the problem

First, thanks for creating/maintaining questionary! This is a super useful package.

I'm working on using questionary to create an interactive "wizard" for configuring a fairly complex click CLI app (osxphotos]. I'd like the output of the questions to map directly to the input needed to configure the app. I'd really like to do this using the prompt() function to pass questions as a dict. However, this is not directly possible in questionary. For example:

  • Some command line options can be repeated (thus the question would need to be asked twice), e.g. --option VALUE1 --option VALUE2. Click passes these as a list.
  • Some command line options take more than one value, e.g. --option VALUE1 VALUE2
  • Some options should only be asked about if the user passes a previous option.

All of these cases could be handled using questionary and a bit of python to handle the logic but I wanted to do this with a dict of questions.

Describe the solution

I've created my own extension to prompt() I've called superprompt that implements these features. Specifically, it adds the following:

  • An if option that will ask a nested set of questions only if the answer to the current question matches a specific value. I find this easier when you have a lot of nested questions than using when.
  • A multiple option that can ask the same question multiple times (either an exact number of up to a certain number of times) and returns the answer as a list of values.
  • A list type which asks a set of nested questions and returns the answer as a list of the answers to the nested questions.
  • A dict type which asks a set of nested questions and returns the answer as a dict of the nested questions.

You can check out the README to see examples of these features.

I welcome any feedback on this and if there's interest in merging these into questionary, I'm happy to work on a PR.

Alternatives considered

Use python code to handle the logic.

@RhetTbull RhetTbull added the Enhancement New feature or request label Jul 30, 2022
@RhetTbull
Copy link
Contributor Author

In looking at the source code for a possible implementation for #207, I see there's already a legacy list type defined as an alias for select so one change that would be needed if there's interest in merging these enhancements would be a name for what I've defined as list and dict types (which are different than defined by questionary).

# backwards compatible names
"list": select.select,
"rawlist": rawselect.rawselect,
"input": text.text,

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

No branches or pull requests

1 participant