I have already developed class1.py, class2.py, etc. with functions implemented inside each class. e.g. Operator.py has add, minus, time, divide functions. How can I build a command line interface for these classes?
Also for this CLI, is it an infinite loop inside the main()
for interaction?
And how can the CLI give some feedback such as, suggesting the user for the next operation or to input right command or type --help
and check all the available commands, like the Bash shells?
Also it seems there is the optparse
module from Python. Are there some good, complete, or high quality samples showing how a CLI is built? I would like to take this chance to learn how to write a CLI program.
I have already developed several classes, and also a GUI to call the methods from these classes. Now I want to have a CLI, like the GUI, to use these classes. e.g. I have classes like CDContainer (with method like addCD, removeCD, etc), CD (with method like play, stop, pause), and I have a GUI that could be interacted. Now I want to have a CLI, which under the bash, I could run this CLI and call createCDContainer, addCD, removeCD commands.
If I use cmd
,
class CDContainerCLI(cmd.Cmd):
def do_CDContainer(self, line):
print "create CD container"
def do_addcd(self, line):
print "add cd into the container"
how do I add some options here? e.g., I want to addcd --track 3 --cdname thriller
.
I think --track 3 --cdname thriller
they are the 4 arguments for the addcd
function. How to implement that?