Guide to CLI Development with Typer
Learn how to create command-line interfaces (CLI) with the Typer library.
Sinvaldo [Profile]
Chief Operating Officer at Square CloudArticle written by Sinvaldo in 02/05/2024.
Introduction
In this article, we’ll explore the Typer library, a modern tool for creating command-line interfaces (CLI) in Python. We’ll learn how to use Typer to create simple and effective CLIs. Additionally, we’ll discuss the advantages and reasons for using Typer in your projects.
What is Typer?
Typer is a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python type hints. It’s built on top of Starlette for web routing and Pydantic for data validation and settings management.
Installation
To install Typer, you can use pip:
Creating a Simple Command Line Interface (CLI)
Setting Up Your CLI
Let’s create a simple CLI that greets the user. For this, we’ll use the typer.Typer()
function and the @app.command()
decorator.
You can run this script with the command python script.py hello --name YourName
. This command will display the greeting “Hello, YourName!”.
Adding More Commands
You can add more commands to your CLI by simply adding more functions decorated with @app.command()
. For example:
Now you can get information about a GitHub user with the command python script.py get-github-user --username [username]
. This will display information about the specified user.
Remember, this is a simple search and does not include advanced search features. For more complex queries, consider using the full GitHub website.
Using Optional Arguments
Harnessing the Power of Optional Arguments in Typer
Typer supports optional arguments. You can define an argument as optional by providing a default value for it:
Now, if you run python main.py greet
, the CLI will greet the world.
Enhancing Your CLI with Flags in Typer
Understanding and Using Flags
Flags are a convenient way to pass boolean options to your CLI. They are used to enable or disable certain features or behaviors in your command. In Typer, you can define a flag using the typer.Option
function with a default value of False
for disabled or True
for enabled.
Here’s an example of how you can use flags with Typer:
In this example, we have two flags: --formal
and --excited
. The --formal
flag changes the greeting from “Hi” to “Hello”, and the --excited
flag changes the punctuation from a period to an exclamation mark.
You can run this script with the command python main.py YourName --formal --excited
. This will display the greeting “Hello, YourName!”.
Requesting User Input with Prompts
Typer allows you to request user input during command execution. Here’s an example:
Using Callbacks in Typer
Typer allows you to execute a function before any command is executed using callbacks. Here’s an example:
To execute this script, you can use the command python script.py run
. This will display the message “Running callback”.
Handling Arguments of Multiple Types
Typer allows you to handle arguments of multiple types, including lists and dictionaries. Here’s an example of how you can do that:
To execute this script, you can use the command python script.py process --items "1 2 3"
. This will display the messages “Processing item 1”, “Processing item 2”, and “Processing item 3”.
Creating Command Groups
Typer allows you to create command groups, which is useful when you have many related commands. Here’s an example of how you can do that:
You can run this script with the command python main.py greetings hello YourName
. This will display the greeting “Hello, YourName!”. Similarly, you can use the command python main.py greetings hello YourName
to bid farewell to the user, displaying “Goodbye, YourName!”.
Conclusion
Typer is a powerful and flexible tool for creating command-line interfaces (CLI) in Python. Its clear and concise syntax makes code more readable and maintainable, which is essential for high-quality software development.
Typer’s flexibility allows you to create anything from simple command-line scripts to complex CLIs. Furthermore, its support for dependency injection makes code organization and reuse easier.
In summary, if you’re looking for a tool to create CLIs in Python, Typer is definitely worth considering. Whether you’re a beginner or experienced developer, Typer has the features and flexibility to meet your needs. Try Typer today and see how it can improve your software development workflow.