@bergus
2015-12-02T06:41:05.000000Z
字数 880
阅读 2205
python cmd模块
# encoding=utf-8import cmdimport sys# cmd模块练习class Client(cmd.Cmd):'''1)cmdloop():类似与Tkinter的mainloop,运行Cmd解析器;2)onecmd(str):读取输入,并进行处理,通常不需要重载该函数,而是使用更加具体的do_command来执行特定的命名;3)emptyline():当输入空行时调用该方法;4)default(line):当无法识别输入的command时调用该方法;5)completedefault(text,line,begidx,endidx):如果不存在针对的complete_*()方法,那么会调用该函数6)precmd(line):命令line解析之前被调用该方法;7)postcmd(stop,line):命令line解析之后被调用该方法;8)preloop():cmdloop()运行之前调用该方法;9)postloop():cmdloop()退出之后调用该方法;'''def __init__(self):cmd.Cmd.__init__(self)self.prompt = '>'def do_hello(self, arg):print "hello again", arg, "!"def help_hello(self):print "syntax: hello [message]",print "-- prints a hello message"def do_quit(self, arg):sys.exit(1)def help_quit(self):print "syntax: quit",print "-- terminates the application"# shortcutsdo_q = do_quitdo_EOF = do_quitif __name__ == '__main__':client = Client()client.cmdloop() # cmdloop():类似与Tkinter的mainloop,运行Cmd解析器;
