標籤:
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 # shortcuts41 do_q = do_quit42 do_EOF = do_quit43 44 if __name__ == ‘__main__‘:45 client = Client()46 client.cmdloop() # cmdloop():類似與Tkinter的mainloop,運行Cmd解析器;
python cmd模組練習