Python cmd模組

小屋子大俠發表於2017-04-18

分為三部分程式碼

1.純處理單一函式

#coding:utf-8
import sys  
from cmd import Cmd

import os


class Client(Cmd):
    prompt = '>'  
      
    def __init__(self):  
        Cmd.__init__(self)  

    def do_dir(self, arg):
        if not arg:

            self.help_dir()

        elif os.path.exists(arg):

            print("\n".join(os.listdir(arg)))

        else:

            print("No such pathexists.")


    def help_dir(self):
        print("syntax: dir path -- displaya list of files and directories")


    def do_quit(self, arg):
        return True


    def help_quit(self):
        print("syntax: quit -- terminatesthe application")
          
if __name__ == '__main__':  
    client = Client()  
    client.cmdloop()  
處理結果如下

/Users/wuzi/python35/venv/bin/python /Users/wuzi/workpy/worktest/cmdtest.py
>dir /Users
.localized
Guest
Shared
wuzi
>dir
syntax: dir path -- displaya list of files and directories
>help


Documented commands (type help <topic>):
========================================
dir  help  quit


>help dir
syntax: dir path -- displaya list of files and directories
>quit


Process finished with exit code 0

處理單一函式的結果值

2,處理所有的文字處理包括無法對應到的命令

#coding:utf-8
import sys  
from cmd import Cmd

import os


class Client(Cmd):
    prompt = '>'  
      
    def __init__(self):  
        Cmd.__init__(self)  
          
    def onecmd(self,arg):  
        print ('onecmd:',arg)
        print(self.parseline(arg))

    def do_dir(self, arg):
        if not arg:

            self.help_dir()

        elif os.path.exists(arg):

            print("\n".join(os.listdir(arg)))

        else:

            print("No such pathexists.")


    def help_dir(self):
        print("syntax: dir path -- displaya list of files and directories")


    def do_quit(self, arg):
        return True


    def help_quit(self):
        print("syntax: quit -- terminatesthe application")
          
if __name__ == '__main__':  
    client = Client()  
    client.cmdloop()  
此時所有的命令資料都會被onecmd()函式接受,此時就不能像上面那個例子直接使用了

>dir /Users
onecmd: dir /Users
('dir', '/Users', 'dir /Users')
>uu
onecmd: uu
('uu', '', 'uu')
>uu lll
onecmd: uu lll
('uu', 'lll', 'uu lll')
>help dir
onecmd: help dir
('help', 'dir', 'help dir')
>quit
onecmd: quit
('quit', '', 'quit')
>


3.使用反射機制實現cmd

#coding:utf-8
import sys  
from cmd import Cmd

import os


class Client(Cmd):
    prompt = '>'  
      
    def __init__(self):  
        Cmd.__init__(self)  
          
    def onecmd(self,arg):
        print ('onecmd:',arg)
        print(self.parseline(arg))
        cmd,args,line = self.parseline(arg)
        try:
            func = getattr(self,"do_"+cmd)
            func(args)
        except Exception as e:
            print("error",e)

    def do_dir(self, arg):
        if not arg:

            self.help_dir()

        elif os.path.exists(arg):

            print("\n".join(os.listdir(arg)))

        else:

            print("No such pathexists.")


    def help_dir(self):
        print("syntax: dir path -- displaya list of files and directories")


    def do_quit(self, arg):
        sys.exit(0)
        return True


    def help_quit(self):
        print("syntax: quit -- terminatesthe application")
          
if __name__ == '__main__':  
    client = Client()  
    client.cmdloop()  
這樣就實現了命令列的基本功能處理

>dd
onecmd: dd
('dd', '', 'dd')
error 'Client' object has no attribute 'do_dd'
>dir /Users
onecmd: dir /Users
('dir', '/Users', 'dir /Users')
.localized
Guest
Shared
wuzi
>help dir
onecmd: help dir
('help', 'dir', 'help dir')
syntax: dir path -- displaya list of files and directories
>quit
onecmd: quit
('quit', '', 'quit')

相關文章