Create Python CLI with select interface Create Python CLI with select interface python python

Create Python CLI with select interface


After a bit of searching, I found two libraries that met my needs!

The first is python-inquirer, a Python port of Inquirer.js, a CLI library used by projects like Yeoman. I found this library to have a really nice API (built on top of blessings) but lacks polish when it comes to design/features.

The second (which I will be using) is whaaaaat, another Python port of Inquirer. This library offers functionality much closer to the original Inquirer.js and is exactly what I needed. The API is less clean than that of python-inquirer, however.

Examples:

python-inquirer example:

from pprint import pprintimport inquirerquestions = [    inquirer.List(        "size",        message="What size do you need?",        choices=["Jumbo", "Large", "Standard", "Medium", "Small", "Micro"],    ),]answers = inquirer.prompt(questions)pprint(answers)

whaaaaat example:

from whaaaaat import prompt, print_json, Separatorquestions = [    {        "type": "list",        "name": "theme",        "message": "What do you want to do?",        "choices": [            "Order a pizza",            "Make a reservation",            Separator(),            "Ask for opening hours",            {"name": "Contact support", "disabled": "Unavailable at this time"},            "Talk to the receptionist",        ],    },    {        "type": "list",        "name": "size",        "message": "What size do you need?",        "choices": ["Jumbo", "Large", "Standard", "Medium", "Small", "Micro"],        "filter": lambda val: val.lower(),    },]answers = prompt(questions)print_json(answers)


For simple choices you can use the simple-term-menu package. It is simple, small and has no dependencies to other packages.

Example:

from simple_term_menu import TerminalMenuterminal_menu = TerminalMenu(["entry 1", "entry 2", "entry 3"])choice_index = terminal_menu.show()

simple-term-menu


You mention the click package, and mention that you are not sure how to implement this feature. It seems like choice-options are the intended way to implement single choice questions.

The generated output will not be nearly as nice as in some of the other packages mentioned in the other answers. However, click is well maintained, active, and works on both UNIX and WIN - critical arguments if you are planning to release a library.