【问题标题】:Python CLI Keeps Performing the Same Command?Python CLI 继续执行相同的命令?
【发布时间】:2013-07-15 18:45:17
【问题描述】:

我正在尝试创建一个允许用户执行各种功能的命令行。 例如,如果我在终端中键入“scriptrun”,我希望它从另一个 .py 文件运行一个函数,然后返回到终端 (->>)。 出于某种原因,如果我输入“scriptrun”,它会正常运行,但是如果我再次点击“enter”,它将导致命令再次运行。我基于我找到的 Turtle CLI。输入帮助后,我希望它只显示一次帮助主题列表,然后如果我一直按回车,它只会显示终端,但会发生以下情况!:

(终端)帮助

记录的命令(输入帮助):

bye color goto help left position reset scriptrun 循环前进航向首页播放记录右撤消

(终端)[这里我刚回车,但在下面你可以看到它再次调用了帮助函数!]

记录的命令(输入帮助):

bye color goto help left position reset scriptrun 循环前进航向首页播放记录右撤消

(终端)

下面是我试图找出解决方案的示例代码:

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

非常感谢任何有关解决方案的帮助或建议!谢谢! :)

【问题讨论】:

    标签: python command-line-interface python-idle


    【解决方案1】:

    似乎最好的解决方案是简单地制作您自己的 cmd 版本,以便您修改此条件,然后从这里将其包含在您的项目中并导入它,这样您就可以让它重复(终端):每次你按回车而不是重复上一个命令!

    【讨论】:

    • 也在想同样的事情。我最终做的是使用 cmd,在我的工作目录中将其修改为 cmd2,然后将空行的返回语句更改为以下内容::if self.lastcmd: return [] 一个简单的修复! :)
    【解决方案2】:
    >>> help (cmd)
    

    列出的第四项是

    “键入空行会重复上一个命令”。

    所以这是按照记录的方式进行的。

    【讨论】:

    • 嗯,谢谢你指出这一点,乔恩。有没有办法解决这个问题,也许不必修改 cmd 文件?
    • 实际上,它并不是对所有情况都重复上一个命令。例如,当我尝试重置或设置蓝色时,情况并非如此!...hmmm
    • 您想让它忽略一个空行吗?好吧,我以前从未使用过 cmd,但是查看文档,我发现您可能可以使用一些钩子。我特别看好 emptyline() 钩子。 :) docs.python.org/2/library/cmd.html
    • 是的,目标基本上是让它无限重复:(终端):(终端):(终端):直到我停止输入空行。谢谢,会检查的!
    • 不,谢谢!以前没接触过cmd库,看起来蛮好用的!
    猜你喜欢
    • 1970-01-01
    • 2012-02-26
    • 1970-01-01
    • 1970-01-01
    • 2021-10-08
    • 1970-01-01
    • 1970-01-01
    • 2017-10-31
    • 1970-01-01
    相关资源
    最近更新 更多