4

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?

ButterDog
  • 5,115
  • 6
  • 43
  • 61
  • So a command line within a program? Sounds like you need a parser! – justderb Jul 24 '12 at 20:50
  • 1
    Looks like a work for [Command pattern](http://en.wikipedia.org/wiki/Command_pattern). – Luiggi Mendoza Jul 24 '12 at 20:52
  • I need not only parse the commands, but also map them to a set of methods on the application, display the result of each command and keep everything de-attached from the app itself so I could switch to and from GUI – ButterDog Jul 24 '12 at 20:55
  • 1
    see http://stackoverflow.com/questions/1524661/the-best-cli-parser-for-java – moritz Jul 24 '12 at 20:55
  • 1
    @moritz He wants more than something that parses a single command line like that question is asking about. He's looking for a complete REPL, that's why something like Spring Shell might fit, but if he were to use something like Commons CLI, he has to build the rest of the it (prompt, environment variables, etc.) himself. – John Munsch Jul 25 '12 at 12:54

4 Answers4

4

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.

LoicAG
  • 603
  • 3
  • 19
John Munsch
  • 19,530
  • 8
  • 42
  • 72
2

I used clamshell-cli in the past, didn't do anything too complicated with it but it seems that it got many features.

Aviram Segal
  • 10,962
  • 3
  • 39
  • 52
1

There is a command line parser Picocli. It was launched in September 2017 and it is already used as command line by Groovy, Micronaut and JUnit.

It has these features:

  • autocompletion
  • ansi colors and styles
  • nice user help
  • supports subcommands
Black
  • 9,541
  • 3
  • 54
  • 54
  • FYI: picocli-3.2.0 added JLine integration support, so you can use picocli commands and get command line completion in your JLine command line shell application. Here’s an example: https://github.com/remkop/picocli/releases/tag/v3.2.0#3.2.0-jline – Remko Popma Sep 16 '18 at 02:01
0

One way to implement this would be via reflection. you would define fields server and connections and give them the methods you need (start() stop() close()). Then when input is parsed, use the reflection mechanism to execute methods you need.

vandale
  • 3,600
  • 3
  • 22
  • 39