python cmd 模块测试

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/tiantao2012/article/details/87113626
#!/usr/bin/python
from cmd import Cmd
import os
import sys

class cli(Cmd):
        prompt ='tao>'
        intro = "welcom test"
        def __init(self):
                Cmd.__init__(self)
        def do_hello(self,args):
                print "hello",args
        def do_exit(self,args):
                print "exit"
                return True
        def default(self,args):
                print "pardon ?"

if __name__ == '__main__':
        try:
                test=cli()
                test.cmdloop()
        except:
                exit()

测试结果如下:
tiantao@BoardServer2:~$ python cmd_test.py
welcom test
tao>hello
hello
tao>hello tao
hello tao
tao>qwer
pardon ?
tao>exit
exit

猜你喜欢

转载自blog.csdn.net/tiantao2012/article/details/87113626