有没有一种模式来编写一个像shell程序一样响应的python程序

时间:2018-04-09 20:29:44

标签: python python-3.x interactive-shell

我想修改我编写的python程序,以接受来自命令行的命令,然后像shell一样响应这些命令。

是否有标准模式或库来执行此操作,或者我只使用while True:stdin.readline()之类的内容?

1 个答案:

答案 0 :(得分:2)

这就是标准库中cmd module的设计目的:

  

x = c(2.3, NA, 2.1, 2.2, NA, 2.4, 2.3, 2.1, NA, NA, 2.6) y <- which(!is.na(x))[findInterval(which(is.na(x)), which(!is.na(x)))] y[y==1] = NA x[which(is.na(x))] = (x[y-1] + x[y])/2 x # [1] 2.30 NA 2.10 2.20 2.15 2.40 2.30 2.10 2.20 2.20 2.60 类为编写面向行的命令解释器提供了一个简单的框架。这些通常对测试工具,管理工具和原型很有用,这些工​​具随后会被包含在更复杂的界面中。

来自Example section

  

y <- which(!is.na(x))[findInterval(which(is.na(x)), which(!is.na(x)))] x[which(is.na(x))] = (x[pmax(1,y-1)] + x[y])/2 # [1] 2.30 2.30 2.10 2.20 2.15 2.40 2.30 2.10 2.20 2.20 2.60 模块主要用于构建自定义shell,让用户以交互方式使用程序。

和一个快速演示示例:

Cmd

运行时产生:

cmd