Command Loop
Contents
Command Loop#
There are two ways to set up a command-line application: one takes all parameters needed to run a single use case from the command line as arguments, and the other lets the user run multiple internal commands while the application runs. The original Qprop program ran using the first approach. We will build this application using second approach by creating something called an Event Loop.
Event Loops#
Most programs users are familiar with these days use an event loop to manage user input. Think about any graphical program you drive with your mouse. The user decides what to do and when to do it, so the program has to be structured using a simple loop that looks something like this:
while True:
cmd = input()
if cmd == 'quit':
break
process(cmd)
The only way to stop this loop, other than having something fail in the process step, is for the user to enter quit as a command.
Program Context#
As the event loop runs the user will enter various commands that provide additional information to the program. This new data needs to be saved since it will affect future commands selected by the user. For that reason, we will need to set up a store of some kind that can be modified as the program runs. All commands need access to this store so they can inspect it as needed to do whatever task they are set up to do during processing.
Fortunately all of this is fairly easy to set up in Python