Generate CLI for any Python object

petercour - Jul 2 '19 - - Dev Community

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:

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player