Back to home

PrerequisitesGetting startedThe CLI scriptAdding the codeRunning the scriptSummaryResources and further reading
Building CLIs With python-fire main image

Building CLIs With python-fire

This post will use the Python Fire library to work through a simple example of setting up the library.

Prerequisites

  1. Familiarity with Pipenv. See here for my post on Pipenv.

Getting started

Let's create the hello-fire directory and install python-fire.

We will also create a file cli.py to hold our CLI script.

1 2 3 4 5 6 7 8 9 # Make the `hello-fire` directory $ mkdir hello-fire $ cd hello-fire # Make file the CLI script $ touch cli.py # Init the virtual environment $ pipenv --three $ pipenv install python-fire

We are now ready to add a script.

The CLI script

For our demo example, we are going to take a slightly modified version of grouping commands script to demo how to run subcommands.

Our aim is to have the following commands:

CommandDescription
ingestion runPrint a message to the console to highlight that we have run the ingestion script
digestion statusPrint a message based on the value of the Digestion class satiated property
digestion runPrint a message to the console to highlight that we have run the digestion script and set the value of satiated to True
runRun both ingestion and digestion run commands and the digestion status

Adding the code

We set the base commands through the Pipeline class and the subcommands through their own class that is initiated as properties of the Pipeline class.

If you notice the optional volume argument for DigestionStage.run, it is used to set the volume of the Digestion class based on an argument passed to the CLI (defaulting to 1).

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 #!/usr/bin/env python import fire class IngestionStage(object): def run(self): return 'Ingesting! Nom nom nom...' class DigestionStage(object): def __init__(self): self.satiated = False def run(self, volume: int = 1) -> str: self.satiated = True return ' '.join(['Burp!'] * volume) def status(self): return 'Satiated.' if self.satiated else 'Not satiated.' class Pipeline(object): def __init__(self): self.ingestion = IngestionStage() self.digestion = DigestionStage() def run(self): print(self.ingestion.run()) print(self.digestion.run()) print(self.digestion.status()) return 'Pipeline complete' if __name__ == '__main__': fire.Fire(Pipeline)

Running the script

To run the script, we need to ensure we are running the Pipenv virtual environment.

We can do this with pipenv shell.

Once, in the shell, we can run our script and see the results:

1 2 3 4 5 6 7 8 9 10 11 12 13 $ python cli.py ingestion run # Ingesting! Nom nom nom... $ python cli.py digestion run # Burp! $ python cli.py digestion run --volume=10 # Burp! Burp! Burp! Burp! Burp! Burp! Burp! Burp! Burp! Burp! $ python cli.py status # Not satiated. $ python cli.py run # Ingesting! Nom nom nom... # Burp! # Satiated. # Pipeline complete

Running through our script, we can now see the results of our work.

Notice that the status property of the Digestion class is set to True when we run the run command and the number of "Burp!" messages printed is based on the volume argument passed to the run command.

Summary

Today's post demonstrated how to use the python-fire package to write easier CLI scripts with their own subcommands and instance-managed state.

Of most languages that I have used, it must be said that python-fire has been one of the most approachable libraries I have seen for building out CLI tools.

Resources and further reading

Photo credit: cullansmith

Dennis O'Keeffe

@dennisokeeffe92
  • Melbourne, Australia

Hi, I am a professional Software Engineer. Formerly of Culture Amp, UsabilityHub, Present Company and NightGuru.
I am currently working on workingoutloud.dev, Den Dribbles and LandPad .

Related articles


1,200+ PEOPLE ALREADY JOINED ❤️️

Get fresh posts + news direct to your inbox.

No spam. We only send you relevant content.