我想自动缩进控制台应用程序的下一行,但用户需要能够删除它. sys.stdout.write并print创建不可删除的字符,我不能写入sys.stdin(据我所知).我基本上试图聪明地缩进,但我只能越陷越深.关于如何爬出来的任何想法?
编辑:我应该注意到这是使用IronPython的Windows程序的一部分.虽然我可以做更多的事情(也可能在未来),但我希望能够以很少的努力作为起点快速获得一个相当愉快的体验.
该cmd模块提供了一个非常简单的界面,用于为您的程序创建命令行界面。它可能无法在下一行前面放置一些缓冲区字符,但如果您正在寻找一种明显的方法来让用户知道命令已返回,它可以在每个行的开头提供类似 shell 的提示线。如果您已经为程序定义了函数,则将它们集成到处理器中只需编写一个访问该函数的处理程序:
import cmd
import math
def findHpyot(length, height):
return math.sqrt(length **2 + height **2)
class MathProcessor(cmd.Cmd):
prompt = "Math>"
def do_hypot(self, line):
x = raw_input("Length:")
y = raw_input("Height:")
if x and y:
try:
hypot = findHypot(float(x), float(y))
print "Hypot:: %.2f" %hypot
except ValueError:
print "Length and Height must be numbers"
def do_EOF(self, line):
return True
def do_exit(self, line):
return True
def do_quit(self, line):
return True
if __name__ == "__main__":
cmdProcessor = MathProcessor()
cmdProcessor.cmdloop()
Run Code Online (Sandbox Code Playgroud)
使用 cmd 编写交互式 shell 时需要考虑的事项