-
Cmd.
cmdloop
(
intro
=
None
)
¶
-
重复发出提示、接受输入、剖析收到输入的初始前缀,并将其分派给动作方法,将行剩余部分作为自变量传递给它们。
可选自变量是发出的横幅 (或介绍) 字符串在第一提示前 (这覆写
intro
类属性)。
若
readline
module is loaded, input will automatically inherit
bash
-like history-list editing (e.g.
Control
-
P
scrolls back to the last command,
Control
-
N
forward to the next one,
Control
-
F
moves the cursor to the right non-destructively,
Control
-
B
moves the cursor to the left non-destructively, etc.).
An end-of-file on input is passed back as the string
'EOF'
.
An interpreter instance will recognize a command name
foo
if and only if it has a method
do_foo()
. As a special case, a line beginning with the character
'?'
is dispatched to the method
do_help()
. As another special case, a line beginning with the character
'!'
is dispatched to the method
do_shell()
(if such a method is defined).
This method will return when the
postcmd()
method returns a true value. The
stop
自变量对于
postcmd()
is the return value from the command’s corresponding
do_*()
方法。
If completion is enabled, completing commands will be done automatically, and completing of commands args is done by calling
complete_foo()
采用自变量
text
,
line
,
begidx
,和
endidx
.
text
is the string prefix we are attempting to match: all returned matches must begin with it.
line
is the current input line with leading whitespace removed,
begidx
and
endidx
are the beginning and ending indexes of the prefix text, which could be used to provide different completion depending upon which position the argument is in.
Cmd 范例
¶
The
cmd
模块主要用于构建自定义 Shell,让用户工作于交互程序。
本节呈现如何构建 Shell 的简单范例,围绕一些命令在
turtle
模块。
基本 Turtle 命令譬如
forward()
被添加到
Cmd
子类采用方法命名
do_forward()
. The argument is converted to a number and dispatched to the turtle module. The docstring is used in the help utility provided by the shell.
The example also includes a basic record and playback facility implemented with the
precmd()
method which is responsible for converting the input to lowercase and writing the commands to a file. The
do_playback()
method reads the file and adds the recorded commands to the
cmdqueue
for immediate playback:
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()