Zuhaib

Build Modern Command Line Interfaces (CLI) with Python

SUNDAY JANUARY 29 2023 - 2 MIN

When it comes to command-line interfaces (CLI), they are often viewed as dull and unengaging. However, CLI wizards offer a fun and interactive way for users to interact with the CLI. In this post, we'll take a deep dive into how to create a beautiful CLI wizard using PyInquirer.

What is PyInquirer?

PyInquirer is an interactive CLI prompt library that allows you to build interactive command-line interfaces. The library provides intuitive prompts with various controls and widgets, such as dropdowns, checkboxes, text input, and more. The library is built on top of the prompt-toolkit library for Python.

How to Install PyInquirer

To install PyInquirer, open your terminal and run the following command:

pip install PyInquirer

Creating a CLI Wizard with PyInquirer

To create a CLI wizard using PyInquirer, you need to first define the questions to ask the user. In PyInquirer, these questions are referred to as prompts. The library supports various types of prompts, including input prompts, list prompts, checkbox prompts, and more.

Here's an example of a simple CLI wizard using PyInquirer:

from PyInquirer import prompt questions = [ { 'type': 'input', 'name': 'name', 'message': 'What is your name?' }, { 'type': 'checkbox', 'message': 'Select toppings', 'name': 'toppings', 'choices': [ {'name': 'Pepperoni'}, {'name': 'Mushroom'}, {'name': 'Onion'}, {'name': 'Tomato'}, {'name': 'Extra cheese'}, ], 'validate': lambda answer: 'You must choose at least one topping.' \ if len(answer) == 0 else True } ] answers = prompt(questions) print(answers)

In this example, we define two prompts: one for the user's name and another for the pizza toppings they want. The prompts are defined using a Python dictionary, with each prompt having a name, type, and message.

We use the prompt method to display the prompts and get the user's answers. The method returns a dictionary containing the answers provided by the user.

Customizing the Look of the CLI Wizard

pyinquirer provides several customization options that allow you to tweak the look and feel of the CLI wizard. You can change the color of the prompts, change the borders, add headers and footers, and more.

Here's an example of a custom CLI wizard using pyinquirer:

from PyInquirer import prompt, style_from_dict, Token style = style_from_dict({ Token.Separator: '#cc5454', Token.QuestionMark: '#7f7fff bold', Token.Selected: '#5F819D', # default Token.Pointer: '#5F819D bold', Token.Instruction: '', # default Token.Answer: '#5F819D bold', Token.Question: '', }) questions = [ { 'type': 'list', 'name': 'theme', 'message': 'What do you want to do?', 'choices': [ 'Create a new project', 'Help', 'Exit', ], 'default': 'Create a new project', 'filter': lambda val: val.lower() }, { 'type': 'input', 'name': 'project_name', 'message': 'Project name:', 'validate': lambda answer: 'You must enter a project name.' \ if len(answer) == 0 else True }, ] answers = prompt(questions, style=style) print(answers)

In this example, we define a custom style for the CLI wizard using a Python dictionary. We then pass the style to the prompt method to apply it to the prompts.

Custom CLI Wizard

Conclusion

Creating interactive CLI wizards is a great way to engage users and provide a more enjoyable experience when using the CLI. pyinquirer is an easy-to-use library that allows you to create beautiful and functional CLI wizards in Python. With pyinquirer, you can customize the prompts to fit your needs, and with a little creativity, you can create a unique CLI wizard that stands out.


For suggestions and queries, just contact me.

Zuhaib Ahmad © 2024