General explaination - How CIH works
Intro
Dust Signs Console Input Handler (short CIH) is a set of classes and functions which allows you to react on user input in a console application in a quite comfortable way: the input is parsed automatically and the appropriate function which handles the parameters of the command entered will be found by the given registred "Command Handlers". What you have to provide is an endless loop which gets user input (p.e. using ReadLn) and handles it over to an instance of a TConsoleInputHandler class. Actually, there is a little bit more than that; please read the explaination of the demo code for further information.
How do Command Handlers work?
Command Handlers (instances of TCommandHandler) mainly consist of three important components:
- A string which determines which command the handler reacts on (p.e. "echo" or "exit")
- A validator function (a function which checks the given parameters and return an error if they are not correct)
- An executor function (a function which performs an action with the parameters given and returns errors or additional return data if necessary)
These are the main parts a Command Handler consists of. To see how these parts work together, read the next paragraph.
Input Handlers and Command Handlers
An Input Handler can consist of multiple Command Handlers. If a given input should be executed after parsing, the Input Handler tries to find a Command Handler which can handle the given command (p.e. "echo"). If there is a Command Handler which is able to handle the command given, the parameters are given over to the validator function of the Command Handler (compare last paragraph). If the validator function returns that the parameters are correct (valid), the executor function is executed and the Input Handler can react on the result of the executor function. The "echo" command p.e. returns information to be printed out onto the console. It's the Input Handler's task to print this information.