import sys class CLI: def __init__(self): self.cmds = {} def add_cmd(self, cmd, action): self.cmds[cmd] = action def run_cmd(self, cmd, params=[]): try: self.cmds[cmd](params) except Exception as e: print type(e), ':', e def start(self): while(1): cmd = raw_input() # tokenize tokens = cmd.split(' ') if tokens[0] in ['exit','quit','bye']: print( 'bye' ) break # process if len(tokens)>1: self.run_cmd( tokens[0], tokens[1:] ) else: self.run_cmd( tokens[0], [] ) sys.stdout.flush() # very basic Person class class Person(): def __init__(self,name): self.name=name # this method will be called via the CLI def say_hi(self, params): print "Hi, I'm", self.name print '\tparams: ', params # if we're running this script if __name__=='__main__': # instantiate new CLI and Person cli = CLI() p = Person('Joe') # link CLI command 'hi' to say_hi method cli.add_cmd('hi',p.say_hi) # run the CLI # by default exit on 'exit', 'bye', or 'quit' cli.start()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question