i'm trying create command line allow user perform various functions. example, if type "scriptrun" terminal, want run function .py file, , return terminal (->>). reason, if type "scriptrun", run properly, if hit "enter" again, cause command run again. i'm basing off turtle cli found. after enter help, want show list of topics once, , if keep hitting enter, show terminal, following happening!:
(terminal) help
documented commands (type ):
bye color goto left position reset scriptrun circle forward heading home playback record right undo
(terminal) [here hit enter, below can see it's calling function again!]
documented commands (type ):
bye color goto left position reset scriptrun circle forward heading home playback record right undo
(terminal)
below sample code trying figure out solution to:
import cmd, sys turtle import * orion_package import * class turtleshell(cmd.cmd): intro = 'welcome turtle shell. type or ? list commands.\n' prompt = '(terminal) ' file = none # ----- basic turtle commands ----- def do_forward(self, arg): 'move turtle forward specified distance: forward 10' forward(*parse(arg)) def do_right(self, arg): 'turn turtle right given number of degrees: right 20' right(*parse(arg)) def do_left(self, arg): 'turn turtle left given number of degrees: left 90' left(*parse(arg)) def do_goto(self, arg): 'move turtle absolute position changing orientation. goto 100 200' goto(*parse(arg)) def do_home(self, arg): 'return turtle home postion: home' home() def do_circle(self, arg): 'draw circle given radius options extent , steps: circle 50' circle(*parse(arg)) def do_position(self, arg): 'print current turle position: position' print('current position %d %d\n' % position()) def do_heading(self, arg): 'print current turle heading in degrees: heading' print('current heading %d\n' % (heading(),)) def do_color(self, arg): 'set color: color blue' color(arg.lower()) def do_undo(self, arg): 'undo (repeatedly) last turtle action(s): undo' def do_reset(self, arg): 'clear screen , return turtle center: reset' reset() def do_bye(self, arg): 'stop recording, close turtle window, , exit: bye' print('thank using turtle') self.close() bye() return true # ----- record , playback ----- def do_record(self, arg): 'save future commands filename: record rose.cmd' self.file = open(arg, 'w') def do_playback(self, arg): 'playback commands file: playback rose.cmd' self.close() open(arg) f: self.cmdqueue.extend(f.read().splitlines()) def precmd(self, line): line = line.lower() if self.file , '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 script: scriptrun' print("let's run thing! :)") scriptrun() def parse(arg): 'convert series of 0 or more numbers argument tuple' return tuple(map(int, arg.split())) if __name__ == '__main__': turtleshell().cmdloop()
any or advice solution appreciated! thank you! :)
it seems best solution make own version of cmd such modify condition , here include project , import , way can have repeat (terminal): every time hit enter rather repeating last command!
Comments
Post a Comment