Python cmd模組
分為三部分程式碼
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')
相關文章
- 在 CMD 執行 python 檔案,找不到引入的模組Python
- CMD 模組定義規範
- AMD、CMD、UMD 模組的寫法
- 模組化之AMD、CMD、UMD、commonJSJS
- [面試專題]JS中模組AMD,CMD,import面試JSImport
- js模組 - amd cmd commonjs esm umdJS
- 前端模組化(CommonJs,AMD和CMD)前端JS
- 前端模組化,AMD與CMD的區別前端
- cmd中如何退出PythonPython
- AMD and CMD are dead之js模組化黑魔法JS
- 前端模組化,AMD和CMD的區別總結前端
- 前端模組化AMD、CMD、CommonJS&ES6前端JS
- 前端模組化之AMD與CMD原理(附原始碼)前端原始碼
- 前端進階課程之模組化(三)CMD規範前端
- 前端模組化:CommonJS,AMD,CMD,ES6前端JS
- 關於前端模組化 CommonJS、AMD、CMD、ES6中模組載入前端JS
- python 模組:itsdangerous 模組Python
- Python模組:time模組Python
- urlparse模組(python模組)Python
- python模組-re模組Python
- python模組 - functools模組Python
- python模組之collections模組Python
- 前端模組化:CommonJS,AMD,CMD,ES6(轉載)前端JS
- cmd中如何執行Python檔案Python
- python如何在cmd中升級pip?Python
- 怎樣在Python中執行cmdPython
- Python 模組Python
- python模組Python
- Python 內建模組:os模組Python
- Python模組之urllib模組Python
- [Python模組學習] glob模組Python
- python讀取excel所有資料(cmd介面)PythonExcel
- python裡執行shell命令或cmd命令Python
- 如何在cmd下切換python版本使用Python
- Python中模組是什麼?Python有哪些模組?Python
- Python的defaultdict模組和namedtuple模組Python
- Python functools 模組Python
- Python Execl模組Python