A really excellent library is cmd which is part of the python standard library. It is cross platform Windows, Linux, Mac. You just have to implement one class and it offers so many great features:
- provides list of supported commands(they end up being implemented as methods)
- help command that can be called to explain one of your commands
- handles the whole entering a command, checking syntax, and calling your method command loop cycle.
- users can save the commands they have run in an interactive session and run them as a script. check out the example below.
If you take the turtle shell example and save it as turtleshell.py and save the below turtle script file as circles.txt
circle 20
circle 50
circle 100
bye
then you could run the turtle script with the following command:
cat circles.txt | ./turtleshell.py
so in the simple example shown in the docs the developer has essentially made a simple mini-language that can be used as an easier interface to to the turtle module making it even easier to introduce programming to kids. The examples above have been taken from the python3 docs because they have included a detailed example in their docs which wasn't there in the 2.7 docs, but cmd is available and fully functional in python 2.3 and later.
Answer from Marwan Alsabbagh on Stack OverflowPython command line interaction library? - Stack Overflow
The best Python CLI library, arguably.
Recommended library for an interactive CLI?
Best library for TUI (Text User Interface) and CLI (Command line Interface)
I'm trying to build a simple script. I'd like to present a list of options to a user, then allow the user to tab up and down through the list (using arrows and/or j+k) and select an option with enter.
Bonus points if the library can:
-
present follow up options/questions/commands after selecting an option
-
accept one of multiple keystrokes/commands when selecting the option, not just <enter>
-
refresh the view of the list of options (e.g. if there are associated statuses with each option)
Any suggestions?
Thanks!
A really excellent library is cmd which is part of the python standard library. It is cross platform Windows, Linux, Mac. You just have to implement one class and it offers so many great features:
- provides list of supported commands(they end up being implemented as methods)
- help command that can be called to explain one of your commands
- handles the whole entering a command, checking syntax, and calling your method command loop cycle.
- users can save the commands they have run in an interactive session and run them as a script. check out the example below.
If you take the turtle shell example and save it as turtleshell.py and save the below turtle script file as circles.txt
circle 20
circle 50
circle 100
bye
then you could run the turtle script with the following command:
cat circles.txt | ./turtleshell.py
so in the simple example shown in the docs the developer has essentially made a simple mini-language that can be used as an easier interface to to the turtle module making it even easier to introduce programming to kids. The examples above have been taken from the python3 docs because they have included a detailed example in their docs which wasn't there in the 2.7 docs, but cmd is available and fully functional in python 2.3 and later.
You can control the Unix terminal with the curses library. The library essentially lets you build a simple terminal GUI.
If you need more, take a look at Urwid as well. Urwid offers more complex GUI widgets for the discerning terminal GUI developer. :-)