18
loading...
This website collects cookies to deliver better user experience
This is Day 6 of the #100DaysOfPython challenge.
hello-fire
directory and install python-fire
.cli.py
to hold our CLI script.# 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
Command | Description |
---|---|
ingestion run |
Print a message to the console to highlight that we have run the ingestion script |
digestion status |
Print a message based on the value of the Digestion class satiated property |
digestion run |
Print a message to the console to highlight that we have run the digestion script and set the value of satiated to True
|
run |
Run both ingestion and digestion run commands and the digestion status |
Pipeline
class and the subcommands through their own class that is initiated as properties of the Pipeline
class.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).#!/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)
pipenv shell
.$ 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
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.python-fire
package to write easier CLI scripts with their own subcommands and instance-managed state.python-fire
has been one of the most approachable libraries I have seen for building out CLI tools.