How to create a CLI with Python Fire on Ubuntu 22.04?
Overview
A Python package called Fire enables you to create CLIs automatically from Python objects.
Installing Python Fire and using it to build unique CLIs are covered in this tutorial.
Prerequisites
There are certain prerequisites that need to be met before you begin:
A server running Ubuntu 22.04 with sudo access and a UFW firewall is turned on.
Good understanding of Python.
Note: The commands inside the red and green rectangles denote the input and output, respectively.
Get Started
Step 1 - Install Fire
A virtual environment must already be configured. Go to your environments directory if it is already not enabled:
Now activate the virtual environment:
Now, install Fire :
There are other ways to call Fire, which you will learn later in this tutorial.
Step 2 - Create a CLI without modifying the existing code
Create a Python file first.
Use nano to create a new Python file:
Create a new file and add the following code:
You can save and exit the file by pressing CTRL+O and CTRL+X, respectively.
You can use Python Fire to convert the existing code into a command line interface command and run it while inserting a variable. Type a new command line interface command:
This is indicating that you want to run pythonFile.py and the function my_name specifically. The --name parameter is used to provide the argument to the function my_name, which only accepts the name argument.
In this instance, you are specifically asking example.py to run the function named my_name.
You have created an extremely basic and functional CLI without making any modifications to your code.
Step 3 - Create a CLI by Importing Fire
Import Fire to use Fire in a more conventional way.
Remove the previous code and add the below code into your pythonFile.py file:
The actual code has undergone two significant changes. Fire is imported initially which is invoked in the end with fire.Fire(). The CLI feature is enabled with the fire.Fire() call.
Save your file and exit.
You can now use your command line interface by typing the following command:
Now, you can simply open the file and call your CLI, passing the name of the function and variable as parameters.
While requiring changes to your actual code, this offers a cleaner command line interface experience. The next step is to directly generate a CLI from a function rather than the complete program.
Step 4 - Create CLI from a Function
You can decide to develop a CLI from a set of your program's functions rather than exposing all of them to it. Remove the previous code and write the following code in your pythonFile file:
Now, exit the file.
Two functions present are my_name and my_full_name function.
Furthermore, an argument is now required when calling fire.Fire in the end. Here, you can include the desired function as an argument to specify it. Here, you will be aiming for my_name when using your CLI.
You need to specify the program and argument required to execute the function because you specified the function to be used in the CLI:
As you specified, my_name function is returned in the output. The new function my_full_name is unaffected.
Add the following code into pythonFile.py:
Now, call my_name function:
Call my_full_name function:
The passage of the dictionary is the only difference this time. Now, you will be creating a command line interface from a Python class.
Step 5 - Create CLI from a Class
You must re-structure the previous code and include your function as a class.
Add the following code to pythonFile.py:
You can call the my_name function using your CLI by using the same command as in the previous step:
You can call your my_full_name function:
The new CLI uses the exact same commands as the one created in the previous section, making it functionally similar. The use of classes in the code restructuring is the only thing that has changed.
Python Fire adapts to your choices and lets you use classes or standard functions to code the way you want.
Conclusion
You have successfully completed this tutorial by installing Python Fire and looking at various approaches to create custom CLIs with Python classes, functions, and programs.
Last updated