2013-07-15 14 views
3

我试图创建一个命令行,允许用户执行各种功能。例如,如果我在终端中键入“scriptrun”,我希望它从另一个.py文件运行一个函数,然后返回到终端( - >>)。 出于某种原因,如果我输入“scriptrun”,它将正常运行,但如果再次点击“enter”,它将导致命令再次运行。我正在将它从我找到的Turtle CLI中解放出来。我输入的帮助后,我想让它显示的帮助主题列表中只有一次,然后如果我继续按下回车键,就会只显示终端,但下面发生的事情!:Python CLI保持执行相同的命令?

(终端)帮助

记录在案的命令(下键入help):

再见颜色转到帮助左侧位置复位scriptrun 圈着回家回放记录的权利撤销

(终端)在这里,我只需点击进入,但下面你可以看到它再次呼叫帮助功能!]

记录在案的命令(下键入help):

再见颜色转到帮助左侧位置复位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() 

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

回答

1

看起来最好的解决方案是简单地制作自己的cmd版本,以便修改此条件,并从此处将此项目与您的项目一起引入并导入,并以此方式重新设置(终端) :每次你进入而不是重复上一个命令!

+0

正在考虑同样的事情。我最终做的是采取cmd,在我的工作目录中将其修改为cmd2,然后将emptyline的return语句更改为以下语句::'if self.lastcmd:return []'一个简单的修复! :) – LuckyLuc

1
>>> help (cmd) 

列出的第四个项目是

“键入一个空行重复最后的命令”。

所以这是行为记录。

+0

嗯,感谢您指出这一点,乔恩。有没有任何可能的方法来解决这个问题,也许不需要修改cmd文件? – LuckyLuc

+0

实际上,它并不重复所有情况下的最后一个命令。例如,当我尝试进行重置或颜色为蓝色时,情况并非如此!... hmmm – LuckyLuc

+1

您希望让它忽略空行吗?那么,我从来没有使用cmd,但看看文档,我发现有一些钩子可以使用。我对emptyline()钩子尤其乐观。 :) http://docs.python.org/2/library/cmd.html –