[关闭]
@bergus 2015-12-02T06:41:05.000000Z 字数 880 阅读 1974

python cmd模块练习

python cmd模块


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