How to build interactive menu for command-line application in python? [closed]

拟墨画扇 提交于 2019-12-06 00:30:14

问题


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: 

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.

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.


回答1:


curses, or something like it, really is what you want.

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.

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.

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).



来源:https://stackoverflow.com/questions/25227519/how-to-build-interactive-menu-for-command-line-application-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!