I’m going to start to design a console application. This is meant to behave like MySQL client, which prompts the user for commands and then executes them (basically to open and listen TCP ports, manage some basic database operations and server management stuff). The application basically accepts connections and returns data to clients.
I want this application to run both on GUI and Console mode depending on how the app is started (no problem here)
GUI is not a problem, but console.
What I need, is a way to define a set of commands, they may have or not any number of parameters. This commands may prompt for confirmation, password or return a set of data.
I want to keep things elegant, maintainable and avoid building a “finite state machine” that may grow out of control.
I want to achieve something like:
[ready]> server:start;
Server started.
[running]> connections:list;
Conn IP Port
Name1 192.168.2.2 60000
Name2 192.168.2.4 60002
[running]> connections:close Name1;
1 connection closed.
[running]> server:stop;
password? ******
Server stopped.
[ready]>
Is there any framework, tool or technique to achieve this on Java?
There’s the recently (very recently) released Spring Shell. It’s what Spring used to power their Roo shell minus the Roo parts. You can plug your own commands into it and make use of its ability to record scripts, play back scripts, take parameters, change the prompt, etc.
I looked at the example that came with it and it had four different classes that added or changed various things and it seemed pretty straightforward.