Python 命令行界面一直执行相同的命令?

3
我正在尝试创建一个命令行,允许用户执行各种功能。例如,如果我在终端中输入“scriptrun”,我希望它能从另一个.py文件中运行一个函数,然后返回到终端(->>)。但是,出现了一些问题:如果我输入“scriptrun”,它会正常运行,但是如果我再次按下“enter”键,它会再次运行该命令。我是基于找到的一个Turtle CLI进行构建的。当我输入help时,我想要它只显示帮助主题列表一次,然后如果我不断按下enter键,它将只显示终端,但以下情况正在发生!:
(Terminal) help 已记录的命令(输入help):
bye color goto help left position reset scriptrun circle forward heading home playback record right undo
(Terminal) [这里我只是按了enter键,但下面你可以看到它再次调用了help函数!]
已记录的命令(输入help):
bye color goto help left position reset scriptrun circle forward heading home playback record right undo
(Terminal)
下面是我尝试解决方案的示例代码:
import cmd, sys
from turtle import *
from orion_package import *

class TurtleShell(cmd.Cmd):
    intro = 'Welcome to the turtle shell.   Type help or ? to list commands.\n'
    prompt = '(Terminal) '
    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 postion:  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 turle position:  POSITION'
        print('Current position is %d %d\n' % position())
    def do_heading(self, arg):
        'Print the current turle 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 do_scriptrun(self, arg):
        'Run the script: SCRIPTRUN'
        print("Let's run this thing!  :)")
        scriptrun()


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

非常感谢您提供帮助和建议!如果有解决方案,请不吝赐教!谢谢! :)
2个回答

1
>>> help (cmd)

第四个列出的项目是:“键入空行会重复上一条命令”。因此,这是按照文档进行操作的。

谢谢你指出这个问题,Jon。有没有可能绕过这个问题,而不必修改cmd文件呢? - LuckyLuc
实际上,并不是所有情况下都会重复执行最后一条命令。例如,当我尝试执行reset或者color BLUE命令时,并不会发生这种情况!...嗯 - LuckyLuc
1
你想让它忽略空行?好的,我之前没有用过cmd,但看了文档后,发现有一些钩子可能可以使用。我对emptyline()钩子特别有信心。:) http://docs.python.org/2/library/cmd.html - Jon Kiparsky
是的,基本上目标就是让它无限重复:(终端):(终端):(终端): 直到我停止输入空行。谢谢,我会检查它! - LuckyLuc
1
不,谢谢!我以前没有接触过cmd库,它看起来非常有用! - Jon Kiparsky

1
似乎最好的解决方案是简单地创建自己的cmd版本,以便修改此条件,并在此基础上将其包含在项目中并导入它,这样您就可以每次按Enter键时重复(终端)而不是重复上一个命令!

我也有同样的想法。我最终做的是将cmd作为cmd2放在我的工作目录中,然后将emptyline的返回语句更改为以下内容:if self.lastcmd: return [] 简单易行的修复! :) - LuckyLuc

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接