cmd打开python3解释器_cmd --- 支持面向行的命令解释器 — Python 3.7.9 說明文件

Cmd 例子¶

The cmd module is mainly useful for building custom shells that let a

user work with a program interactively.

这部分提供了一个简单的例子来介绍如何使用一部分在 turtle 模块中的命令构建一个 shell 。

基础的 turtle 命令比如 forward() 被添加进一个 Cmd 子类,方法名为 do_forward() 。参数被转换成数字并且分发至 turtle 模块中。 docstring 是 shell 提供的帮助实用程序。

例子也包含使用 precmd() 方法实现基础的记录和回放的功能,这个方法负责将输入转换为小写并且将命令写入文件。 do_playback() 方法读取文件并添加记录命令至 cmdqueue 用于即时回放:

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()

这是一个示例会话,其中 turtle shell 显示帮助功能,使用空行重复命令,以及简单的记录和回放功能:

Welcome to the turtle shell. Type help or ? to list commands.

(turtle) ?

Documented commands (type help ):

========================================

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值