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
We've seen that Java isn't just useful for big web applications - it works just fine for short-running CLI applications too, made easier with jbang and picocli.
Console output is most easily accomplished with print() and println() methods, as described earlier. These methods are defined by the class PrintStream which is the type of object referenced by System.in.
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.
You might want to look at JLine. It is similar to BSD editline and GNU readline.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With