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:

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:

cd environments
  • Now activate the virtual environment:

source my_env/bin/activate
  • Now, install Fire :

pip 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:

nano pythonFile.py
  • Create a new file and add the following code:

def my_name(name):
  return "My name is {name}".format(name=name)
  • 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:

    python -m fire pythonFile my_name --name=Michael​

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:

import fire
 
def my_name(name):
  return "My name is {name}".format(name=name)
 
if __name__ == '__main__':
  fire.Fire()

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:

python pythonFile.py my_name Michael

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:

import fire
 
def my_name(name):
  return "My name is {name}".format(name=name)
 
def my_full_name(first_name, last_name):
    return "My name is {first_name} {last_name}".format(first_name=first_name, last_name=last_name)
 
if __name__ == '__main__':
  fire.Fire(my_name)
  • 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:

python pythonFile.py Michael

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:

import fire
 
def my_name(name):
  return "My name is {name}".format(name=name)
 
def my_full_name(first_name, last_name):
    return "My name is {first_name} {last_name}".format(first_name=first_name, last_name=last_name)
 
if __name__ == '__main__':
  fire.Fire({
      'my_name': my_name,
      'my_full_name': my_full_name,
  })
  • Now, call my_name function:

python pythonFile.py my_name Michael
  • Call my_full_name function:

python pythonFile.py my_full_name Michael Welsh

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:

import fire
 
class Namer(object):
    def my_name(self, name):
        return "My name is {name}".format(name=name)
 
    def my_full_name(self, first_name, last_name):
        return "My name is {first_name} {last_name}".format(first_name=first_name, last_name=last_name)
 
if __name__ == '__main__':
  fire.Fire(Namer)
  • You can call the my_name function using your CLI by using the same command as in the previous step:

    python pythonFile.py my_name Michael​

  • You can call your my_full_name function:

python pythonFile.py my_full_name Michael Welsh

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