1 # encoding=utf-8 2 import cmd 3 import sys 4 5 6 # cmd模块练习 7 8 class Client(cmd.Cmd): 9 10 ''' 11 1)cmdloop():类似与Tkinter的mainloop,运行Cmd解析器; 12 2)onecmd(str):读取输入,并进行处理,通常不需要重载该函数,而是使用更加具体的do_command来执行特定的命名; 13 3)emptyline():当输入空行时调用该方法; 14 4)default(line):当无法识别输入的command时调用该方法; 15 5)completedefault(text,line,begidx,endidx):如果不存在针对的complete_*()方法,那么会调用该函数 16 6)precmd(line):命令line解析之前被调用该方法; 17 7)postcmd(stop,line):命令line解析之后被调用该方法; 18 8)preloop():cmdloop()运行之前调用该方法; 19 9)postloop():cmdloop()退出之后调用该方法; 20 21 ''' 22 23 def __init__(self): 24 cmd.Cmd.__init__(self) 25 self.prompt = '>' 26 27 def do_hello(self, arg): 28 print "hello again", arg, "!" 29 30 def help_hello(self): 31 print "syntax: hello [message]", 32 print "-- prints a hello message" 33 34 def do_quit(self, arg): 35 sys.exit(1) 36 37 def help_quit(self): 38 print "syntax: quit", 39 print "-- terminates the application" 40 # shortcuts 41 do_q = do_quit 42 do_EOF = do_quit 43 44 if __name__ == '__main__': 45 client = Client() 46 client.cmdloop() # cmdloop():类似与Tkinter的mainloop,运行Cmd解析器;