问题描述
我一直在与 click 一起制作命令行程序.现在,我正在以非常文本的方式实现交互式菜单.例如:
I've been working with click to make a command line program. Right now I'm implementing interactive menus in a very textual way. for example:
1-option #1
2-option #2
Enter the index of the option you want to select:
但是我很想以一种更加优雅和互动的方式来做到这一点.例如,我喜欢Yeoman实现菜单的方式.这是起作用的菜单.
But I would love to do this in a more elegant and interactive way. For example, I love the way Yeoman implements its menus. Here is the menu in action.
是否有任何python库可以让我们构建这样的命令行菜单?我看过诸如curses,cmd等之类的库.但是它们似乎为您提供了一个单独的窗口来进行管理,并且看起来有点不合常规.
Is there any python library that let's us build command line menus like this? I have looked at libraries like curses, cmd etc. But they seem to give you a whole separate window to manage and look kind of unpythonic.
推荐答案
curses
或类似的东西,确实是您想要的.
curses
, or something like it, really is what you want.
curses
可以用来弹出带有边框的窗口",擦除整个窗口等.在此基础上,其目的是使您可以控制终端窗口-左右移动光标,突出显示文本,以及您要执行的所有其他操作.
While curses
can be used to pop up "windows" with borders around them, erase the whole window, etc., at its base, what it's about is giving you control over your terminal window—moving a cursor around, highlighting text, all the other things you're trying to do.
一些使事情变得更容易的高级库确实将您推向了更具体的外观&.感觉可能不是您要追求的,但是curses
可以轻松地用于您要构建的内容.
Some of the higher-level libraries that make things easier (like urwid
) do push you toward a more specific look & feel that may not be what you're after, but curses
can easily be used for exactly what you're trying to build.
curses
的唯一真正问题是它并非无处不在.几乎所有* nix平台都将拥有它,但Windows则不会.但是答案并没有太大不同-存在curses
伪造的库或Windows特定的conio
库(包括在mscvrt
内部的stdlib中的有限库).
The only real problem with curses
it's that it's not ubiquitous. Almost all *nix platforms will have it, but Windows won't. But the answer there isn't much different—there are curses
-faking libraries, or Windows-specific conio
libraries (including a limited one in the stdlib, inside mscvrt
).
这篇关于如何在python中为命令行应用程序构建交互式菜单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!