DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

petercour
petercour

Posted on

Generate CLI for any Python object

You can generate command line interfaces (CLI) easily with the fire module. For the Linux or Apple terminal.

This is for a Python object, so you need to know the object oriented programming paradigm.

class Bot():
    def hello(self, name):
        print("Hello " + name)

    def whatsup(self):
        print("All good!")

Then import the fire module and load it in the main function.

import fire

class Bot():
    def hello(self, name):
        print("Hello " + name)

    def whatsup(self):
        print("All good!")

if __name__ == '__main__':
    fire.Fire(Bot)

Then in the command line:

Ξ»  ~  python example.py
Type:        instance
String form: <__main__.Bot instance at 0x7fa7e1062320>

Usage:       example.py 
             example.py hello
             example.py whatsup

Need to write arguments with the command :)

You can do:

Ξ»  ~  python example.py hello bob
Hello bob

To call the other method

Ξ»  ~  python example.py whatsup  
All good!
Ξ»  ~  

Yes, all methods of the class are included.

More on Python:

Top comments (0)

Does your company have something to share with DEV?

Create an Organization and start sharing content with the community on DEV.