如何用 Python 编写 shell
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2594560/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to write a shell in Python
提问by panzi
I've written a small console application that can perform certain tasks. The user interface is similar to things like version control systems or yum etc. So basically you can think of it as a domain specific language. Now I'd like to write a (bash like) shell that can execute and auto-complete this language and has a command history (so I do not have to load and save the quite large xml files on each command). In a nutshell I want something like ipython but not for executing python code but my own DSL.
我编写了一个可以执行某些任务的小型控制台应用程序。用户界面类似于版本控制系统或 yum 等。所以基本上你可以将其视为特定领域的语言。现在我想编写一个(类似 bash 的)shell,它可以执行和自动完成这种语言并具有命令历史记录(因此我不必在每个命令上加载和保存相当大的 xml 文件)。简而言之,我想要像 ipython 这样的东西,但不是为了执行 python 代码,而是我自己的 DSL。
Are there any libraries that help me doing this? I see that there is a readline and rlcompleter module in python but its documentation seems to indicate that this is only for use with the python shell itself, or did I miss something there?
是否有任何图书馆可以帮助我做到这一点?我看到 python 中有一个 readline 和 rlcompleter 模块,但它的文档似乎表明这仅适用于 python shell 本身,或者我错过了什么?
采纳答案by Brian Lyttle
回答by zoli2k
If you need an administrative shell to be accessed via network Twistedpython framework's manhole is a good solution. Example:
如果您需要通过网络访问管理 shell,Twistedpython 框架的检修孔是一个很好的解决方案。例子: