cmd
--- 支持面向行的命令解釋器?
源代碼: Lib/cmd.py
Cmd
類(lèi)提供簡(jiǎn)單框架用于編寫(xiě)面向行的命令解釋器。 這些通常對測試工具,管理工具和原型有用,這些工具隨后將被包含在更復雜的接口中。
- class cmd.Cmd(completekey='tab', stdin=None, stdout=None)?
一個(gè)
Cmd
實(shí)例或子類(lèi)實(shí)例是面向行的解釋器框架結構。 實(shí)例化Cmd
本身是沒(méi)有充分理由的, 它作為自定義解釋器類(lèi)的超類(lèi)是非常有用的為了繼承Cmd
的方法并且封裝動(dòng)作方法。可選參數 completekey 是完成鍵的
readline
名稱(chēng);默認是 Tab 。如果 completekey 不是None
并且readline
是可用的, 命令完成會(huì )自動(dòng)完成。可選參數 stdin 和 stdout 指定了Cmd實(shí)例或子類(lèi)實(shí)例將用于輸入和輸出的輸入和輸出文件對象。如果沒(méi)有指定,他們將默認為
sys.stdin
和sys.stdout
。如果你想要使用一個(gè)給定的 stdin ,確保將實(shí)例的
use_rawinput
屬性設置為False
,否則 stdin 將被忽略。
Cmd 對象?
Cmd
實(shí)例有下列方法:
- Cmd.cmdloop(intro=None)?
反復發(fā)出提示,接受輸入,從收到的輸入中解析出一個(gè)初始前綴,并分派給操作方法,將其余的行作為參數傳遞給它們。
可選參數是在第一個(gè)提示之前發(fā)布的橫幅或介紹字符串(這將覆蓋
intro
類(lèi)屬性)。如果
readline
繼承模塊被加載,輸入將自動(dòng)繼承類(lèi)似 bash的歷史列表編輯(例如, Control-P 滾動(dòng)回到最后一個(gè)命令, Control-N 轉到下一個(gè)命令,以 Control-F 非破壞性的方式向右 Control-B 移動(dòng)光標,破壞性地等)。輸入的文件結束符被作為字符串傳回
'EOF'
。解釋器實(shí)例將會(huì )識別命令名稱(chēng)
foo
當且僅當它有方法do_foo()
。有一個(gè)特殊情況,分派始于字符'?'
的行到方法do_help()
。另一種特殊情況,分派始于字符'!'
的行到方法do_shell()
(如果定義了這個(gè)方法)這個(gè)方法將返回當
postcmd()
方法返回一個(gè)真值 。參數 stop 到postcmd()
是命令對應的返回值do_*()
的方法。如果激活了完成,全部命令將會(huì )自動(dòng)完成,并且通過(guò)調用
complete_foo()
參數 text , line, begidx ,和 endidx 完成全部命令參數。 text 是我們試圖匹配的字符串前綴,所有返回的匹配項必須以它為開(kāi)頭。 line 是刪除了前導空格的當前的輸入行, begidx 和 endidx 是前綴文本的開(kāi)始和結束索引。,可以用于根據參數位置提供不同的完成。所有
Cmd
的子類(lèi)繼承一個(gè)預定義do_help()
。 這個(gè)方法使用參數'bar'
調用, 調用對應的方法help_bar()
,如果不存在,打印do_bar()
的文檔字符串,如果可用。沒(méi)有參數的情況下,do_help()
方法會(huì )列出所有可用的幫助主題 (即所有具有相應的help_*()
方法或命令的 文檔字符串),也會(huì )列舉所有未被記錄的命令。
- Cmd.onecmd(str)?
解釋該參數,就好像它是為響應提示而鍵入的一樣。 這可能會(huì )被覆蓋,但通常不應該被覆蓋; 請參閱:
precmd()
和postcmd()
方法,用于執行有用的掛鉤。 返回值是一個(gè)標志,指示解釋器對命令的解釋是否應該停止。 如果命令 str 有一個(gè)do_*()
方法,則返回該方法的返回值,否則返回default()
方法的返回值。
- Cmd.emptyline()?
在響應提示輸入空行時(shí)調用的方法。如果此方法未被覆蓋,則重復輸入的最后一個(gè)非空命令。
- Cmd.default(line)?
當命令前綴不能被識別的時(shí)候在輸入行調用的方法。如果此方法未被覆蓋,它將輸出一個(gè)錯誤信息并返回。
- Cmd.completedefault(text, line, begidx, endidx)?
當沒(méi)有特定于命令的
complete_*()
方法可用時(shí),調用此方法完成輸入行。默認情況下,它返回一個(gè)空列表。
- Cmd.columnize(list, displaywidth=80)?
Method called to display a list of strings as a compact set of columns. Each column is only as wide as necessary. Columns are separated by two spaces for readability.
- Cmd.precmd(line)?
鉤方法在命令行 line 被解釋之前執行,但是在輸入提示被生成和發(fā)出后。這個(gè)方法是一個(gè)在
Cmd
中的存根;它的存在是為了被子類(lèi)覆蓋。返回值被用作onecmd()
方法執行的命令;precmd()
的實(shí)現或許會(huì )重寫(xiě)命令或者簡(jiǎn)單的返回 line 不變。
- Cmd.postcmd(stop, line)?
鉤方法只在命令調度完成后執行。這個(gè)方法是一個(gè)在
Cmd
中的存根;它的存在是為了子類(lèi)被覆蓋。 line 是被執行的命令行, stop 是一個(gè)表示在調用postcmd()
之后是否終止執行的標志;這將作為onecmd()
方法的返回值。這個(gè)方法的返回值被用作與 stop 相關(guān)聯(lián)的內部標志的新值;返回 false 將導致解釋繼續。
Instances of Cmd
subclasses have some public instance variables:
- Cmd.prompt?
發(fā)出提示以請求輸入。
- Cmd.identchars?
接受命令前綴的字符串。
- Cmd.lastcmd?
看到最后一個(gè)非空命令前綴。
- Cmd.doc_header?
如果幫助輸出具有記錄命令的段落,則發(fā)出頭文件。
- Cmd.misc_header?
如果幫助輸出其他幫助主題的部分(即與
do_*()
方法沒(méi)有關(guān)聯(lián)的help_*()
方法),則發(fā)出頭文件。
- Cmd.undoc_header?
如果幫助輸出未被記錄命令的部分(即與
help_*()
方法沒(méi)有關(guān)聯(lián)的do_*()
方法),則發(fā)出頭文件。
- Cmd.ruler?
用于在幫助信息標題的下方繪制分隔符的字符,如果為空,則不繪制標尺線(xiàn)。這個(gè)字符默認是
'='
。
Cmd 例子?
The cmd
module is mainly useful for building custom shells that let a
user work with a program interactively.
這部分提供了一個(gè)簡(jiǎn)單的例子來(lái)介紹如何使用一部分在 turtle
模塊中的命令構建一個(gè) shell 。
基礎的 turtle 命令比如 forward()
被添加進(jìn)一個(gè) Cmd
子類(lèi),方法名為 do_forward()
。參數被轉換成數字并且分發(fā)至 turtle 模塊中。 docstring 是 shell 提供的幫助實(shí)用程序。
例子也包含使用 precmd()
方法實(shí)現基礎的記錄和回放的功能,這個(gè)方法負責將輸入轉換為小寫(xiě)并且將命令寫(xiě)入文件。 do_playback()
方法讀取文件并添加記錄命令至 cmdqueue
用于即時(shí)回放:
import cmd, sys
from turtle import *
class TurtleShell(cmd.Cmd):
intro = 'Welcome to the turtle shell. Type help or ? to list commands.\n'
prompt = '(turtle) '
file = None
# ----- basic turtle commands -----
def do_forward(self, arg):
'Move the turtle forward by the specified distance: FORWARD 10'
forward(*parse(arg))
def do_right(self, arg):
'Turn turtle right by given number of degrees: RIGHT 20'
right(*parse(arg))
def do_left(self, arg):
'Turn turtle left by given number of degrees: LEFT 90'
left(*parse(arg))
def do_goto(self, arg):
'Move turtle to an absolute position with changing orientation. GOTO 100 200'
goto(*parse(arg))
def do_home(self, arg):
'Return turtle to the home position: HOME'
home()
def do_circle(self, arg):
'Draw circle with given radius an options extent and steps: CIRCLE 50'
circle(*parse(arg))
def do_position(self, arg):
'Print the current turtle position: POSITION'
print('Current position is %d %d\n' % position())
def do_heading(self, arg):
'Print the current turtle heading in degrees: HEADING'
print('Current heading is %d\n' % (heading(),))
def do_color(self, arg):
'Set the color: COLOR BLUE'
color(arg.lower())
def do_undo(self, arg):
'Undo (repeatedly) the last turtle action(s): UNDO'
def do_reset(self, arg):
'Clear the screen and return turtle to center: RESET'
reset()
def do_bye(self, arg):
'Stop recording, close the turtle window, and exit: BYE'
print('Thank you for using Turtle')
self.close()
bye()
return True
# ----- record and playback -----
def do_record(self, arg):
'Save future commands to filename: RECORD rose.cmd'
self.file = open(arg, 'w')
def do_playback(self, arg):
'Playback commands from a file: PLAYBACK rose.cmd'
self.close()
with open(arg) as f:
self.cmdqueue.extend(f.read().splitlines())
def precmd(self, line):
line = line.lower()
if self.file and 'playback' not in line:
print(line, file=self.file)
return line
def close(self):
if self.file:
self.file.close()
self.file = None
def parse(arg):
'Convert a series of zero or more numbers to an argument tuple'
return tuple(map(int, arg.split()))
if __name__ == '__main__':
TurtleShell().cmdloop()
這是一個(gè)示例會(huì )話(huà),其中 turtle shell 顯示幫助功能,使用空行重復命令,以及簡(jiǎn)單的記錄和回放功能:
Welcome to the turtle shell. Type help or ? to list commands.
(turtle) ?
Documented commands (type help <topic>):
========================================
bye color goto home playback record right
circle forward heading left position reset undo
(turtle) help forward
Move the turtle forward by the specified distance: FORWARD 10
(turtle) record spiral.cmd
(turtle) position
Current position is 0 0
(turtle) heading
Current heading is 0
(turtle) reset
(turtle) circle 20
(turtle) right 30
(turtle) circle 40
(turtle) right 30
(turtle) circle 60
(turtle) right 30
(turtle) circle 80
(turtle) right 30
(turtle) circle 100
(turtle) right 30
(turtle) circle 120
(turtle) right 30
(turtle) circle 120
(turtle) heading
Current heading is 180
(turtle) forward 100
(turtle)
(turtle) right 90
(turtle) forward 100
(turtle)
(turtle) right 90
(turtle) forward 400
(turtle) right 90
(turtle) forward 500
(turtle) right 90
(turtle) forward 400
(turtle) right 90
(turtle) forward 300
(turtle) playback spiral.cmd
Current position is 0 0
Current heading is 0
Current heading is 180
(turtle) bye
Thank you for using Turtle