tmbo / questionary

Python library to build pretty command line user prompts ✨Easy to use multi-select lists, confirmations, free text prompts ...
MIT License
1.57k stars 90 forks source link

Using questionary to wrap prompts for `click` #354

Closed riziles closed 9 months ago

riziles commented 10 months ago

Describe the problem

The more I use questionary, the more I love it.

click allows you to prompt the user if command line parameter is missing, but the functionality is minimal. I discovered that I can do WAY better if pass questionary callables as a default.

Describe the solution

Example program:


import click
import questionary

def asyncquestiontest():
    """Prompts for color, but times out after 3 seconds and defaults to black"""

    import asyncio

    async def timeoutquestion():
        try:
            return await asyncio.wait_for(
                questionary.text("What's your favorite color?").ask_async(), 3
            )
        except:
            print("defaulting to black")
            return "black"

    return asyncio.run(timeoutquestion())

@click.command()
@click.option("--username", default=questionary.text("What's your username").ask)
@click.option("--color", default=asyncquestiontest)
def hello(username, color):
    """A simple response to the user prompts above"""
    click.echo(f"Hello, {username}! I like {color}, too!")

hello()

Alternatives considered

No response

riziles commented 9 months ago

Closing this because there was no issue. Just posting a cool use case I found.