Python Fire
google/python-fire
Python Fire is a library for automatically generating command line interfaces (CLIs) from absolutely any Python object.
Try Python Fire
This is what the “hello, world” of Python Fire looks like.
import fire
def hello(name="fire"):
print(f"hello, {name}")
if __name__ == '__main__':
fire.Fire()
You can run the above via the command line (on macOS, Linux, or Windows Subsytem for Linux (WSL) as follows:
$ mkdir hello-fire
$ cd hello-fire
$ vim main.py # copy/paste the above snippet
$ python3 -m venv env
$ source env/bin/activate
$ pip install -U fire
$ python main.py hello
The output will be ‘hello, fire’. Now run python main.py hello --help
to see how you can pass the name parameter via a flag.
I recently created a demo of Dapr (Distributed Application Runtime) where I discussed the Try Dapr experience and compared the Dapr REST API which is accessed via curl in Try Dapr, but which we explored using Python Fire, both with requests instead of curl, as well as the Dapr SDK for Python. This is a perfect example of a small script that benefits from Python Fire. It becomes simple, easy to read, explore, borrow from, and extend.
You can see it at asw101/2021 at github.com/asw101/2021/tree/main/dev/dapr-lab/main.py.
Another helpful example of Python Fire is pg-lab.py
in our Explore Azure Database for PostgreSQL with Python lab at Azure-Samples/azure-python-labs.
Questions? Need help?
I’d love to hear any feedback, questions, or help out if you are trying any of these projects or examples.
You can reach me on Twitter (@as_w) or Aaron W#0101 on the Microsoft Open Source Discord (aka.ms/open-source-discord).