Skip to content

Latest commit

 

History

History
86 lines (75 loc) · 1.94 KB

fire.md

File metadata and controls

86 lines (75 loc) · 1.94 KB

Command Line Interface using python-fire

Fire provides a truly simple way to get started with command line applications. It is python friendly and can expose any python object as a command line interface

To get started pip install fire

Docs: read the docs

Simple Use Case 1

Expose a function in your module as CLI

   1import fire
   23def human_population(year=2021):
   4if year == 2021:
   5return 8000000000
   67return "unknown"
   89if __name__ == "__main__":
  10fire.Fire(human_population)

Output

python module-example.py
8000000000

Simple Use Case 2

Expore your class as CLI

   1import fire
   23database = {
   4"United States": {
   5"GDP": "20 Trillion",
   6"pop": 330000000
   7   │    },
   8"India": {
   9"GDP": "5 Trillion",
  10"pop": 1500000000
  11   │    },
  1213   │ }
  141516class Census:
  17"""This class handles the census for the UN"""
  1819def __init__(self):
  20self.countries = database
  2122def info(self, country, attribute):
  23res = self.countries[country.title()].get(attribute)
  24return res
  252627if __name__ == "__main__":
  28"""
  29   │   use this as key based interface to the dict
  30   │   outputs
  31   │   $python component-example.py India
  32   │   GDP: 5 Trillion
  33   │   pop: 1500000000
  34   │   """
  35# fire.Fire(database)
  3637"""
  38   │   Expose the class as an Interface
  39   │   """
  40fire.Fire(Census)

Output

python component-example.py info "united states" GDP
20 Trillion