Qprompt is a simple CLI library for Python that I have slowly working on for about two years now. It provides basic typed prompts and menus in a CLI environment along with some additional convenience functions. Recently, I added a feature that allows prompt inputs to be automatically entered via stdin; I have found this feature to be enormously helpful. Let's look at an example use case.
Use Case - QuickWin Build Menu
QuickWin is another project of mine. It is a GUI window switching application written in C++/Qt.
I have a fairly opinionated view on how software projects should be structured and how files should be named (more info here). When a script is solely intended for user interaction, I like naming conventions like
_Cleanup.py and similar. I find this style helps make user scripts stand out from scripts that would otherwise be named like
build.py and similar.
That being said, Qprompt works wonders for user scripts. Here is an excerpt from the build script for QuickWin:
menu = qprompt.Menu() menu.add("b", "Build release", build, [config, "release"]) menu.add("d", "Build debug", build, [config, "debug"]) menu.add("r", "Run app", run) menu.add("k", "Kill app", kill) menu.add("c", "Cleanup", cleanup) menu.main(loop=True)
This puppy shows the user the following when run:
-- MENU -- (b) Build release (d) Build debug (r) Run app (k) Kill app (c) Cleanup (q) Quit [!] Menu loops until quit. [?] Enter menu selection:
The menu provides the user with the basic actions required during development. The build process may change but the menu stays the same. Additionally the script can be run from the shell with automatic inputs, for example:
$ _Build_App.py k c b r q
This will run all of the given menu commands in the given order. Couple items of note:
- The menu needs to be run by calling the
- To run multiple commands automatically, the
loop=Trueflag needs to be set.
- While not required, it is recommended to end the sequence of commands with
q; this will exit the script gracefully.
- On Windows, add
startbefore the script call to run it asynchronously.
So give Qprompt a try! I hope you find it as useful as I do!