Is there a common library which helps to implement commands entered on a Java console program? I don’t mean a library for parsing Java command line options like Commons CLI. I am talking about creating commands which are used in a running Java program entered into the console:
> connect 127.0.01
connected!
> load poem.txt 23
loading….
Something like this – I could write it myself, but the question is, whether there is already a library.
I ended up using inheritance of an abstract class Command. It has execute() and with Java reflections I parse the input and receive the command class from its first input argument:
connect … -> ConnectCommand
You could use a parser to do this. You want to take some free form text and convert it into appropriate Java objects that represent the expression the user typed in.
There are many libraries available from the heavyweight (AntLR for example) to something simpler (like JParsec). You have also always got the option of doing it manually with regular expressions and so on.