Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java library for creating console commands

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

like image 379
Konrad Reiche Avatar asked Jun 07 '11 08:06

Konrad Reiche


People also ask

Is Java good for CLI?

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.

Which method is used to write console output in Java?

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.


2 Answers

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.

like image 182
Jeff Foster Avatar answered Sep 18 '22 02:09

Jeff Foster


You might want to look at JLine. It is similar to BSD editline and GNU readline.

like image 37
Vladimir Korenev Avatar answered Sep 18 '22 02:09

Vladimir Korenev