如何在python中制作命令行界面或解释器 [英] how to make a Command Line Interface or Interpreter in python

查看:269
本文介绍了如何在python中制作命令行界面或解释器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们。我在这里看到了一些CLI问题,但是我仍然想问这个问题以获得更详细的答案。

guys. I have seen some CLI questions here, but I still want to ask this question for more detailed answers.

我已经开发了class1.py,class2.py等。在每个类中实现的功能。例如Operator.py具有加,减,时间,表示功能。如何为这些类构建命令行界面?

I have already developed class1.py, class2.py, etc. with functions implemented inside each class. e.g. Operator.py has add, minus,time, devide functions. how can I build a command line interface for these classes?

对于此CLI也是如此,它是main()内进行交互的无限循环吗?

also for this CLI, is it a infinite loop inside the main() for interaction?

CLI如何提供一些反馈,例如建议用户进行下一步操作或输入正确的命令或键入--help并检查所有可用命令。像Bash炮弹一样

And how can the CLI give some feedback such as, suggesting the user for the next operation or to input right command or type --help and check all the available commands. like the Bash shells.

似乎也有python的optparse模块。是否有一些好的,完整的或高质量的示例显示CLI的构建方式?我想借此机会学习如何编写CLI程序。

also it seems there is optparse module from python. is there some good, complete, or high quality samples showing how a CLI is built? I would like to take this chance to learn how to write a CLI program.

我想要的是:我已经开发了几个类,还有一个GUI来调用这些类中的方法。现在,我想拥有一个像GUI这样的CLI来使用这些类。例如我有CDContainer之类的类(具有addCD,removeCD等方法),CD(具有诸如play,stop,pause之类的方法),并且我具有可以交互的GUI。现在,我想拥有一个CLI,可以轻松运行此CLI并调用createCDContainer,addCD,removeCD命令。

what i want is: I have already developed several classes, and also a GUI to call the methods from these classes. Now i want to have a CLI, like the GUI, to use these classes. e.g. I have classes like CDContainer (with method like addCD, removeCD, etc), CD (with method like play, stop, pause), and I have a GUI that could be interacted. Now I want to have a CLI, which under the bash, I could run this CLI and call createCDContainer, addCD, removeCD commands.

如果我使用cmd,

class CDContainerCLI(cmd.Cmd):

    def do_CDContainer(self, line):
        print "create CD container"

    def do_addcd(self, line):
        print "add cd into the container"

如何在此处添加一些选项?例如,我要添加cd --track 3 --cdname thriller
我认为 --track 3 --cdname thriller是addcd函数的4个参数。

how do I add some options here? e.g., I want to addcd --track 3 --cdname thriller I think "--track 3 --cdname thriller" they are the 4 arguments for the addcd function. how to implement that?

推荐答案

cmd.Cmd ,根据需要覆盖各种方法。

Derive from cmd.Cmd, overriding the various methods as necessary.

这篇关于如何在python中制作命令行界面或解释器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆