Package de.weltraumschaf.commons.shell

Sub package for a simple interactive shell.

See: Description

Package de.weltraumschaf.commons.shell Description

Sub package for a simple interactive shell.

This package is a part of the open-source Commons

Contains:

Example

First define main and sub command types.

// CHECKSTYLE:OFF
public enum MyMainType implements MainCommandType {

     HELP("help"),
     FOO("foo"),
     EXIT("exit");

     // Literal command string used in shell.
     private final String literal;

     private NeuronMainType(final String name) {
         this.literal = name;
     }

     {@literal @}Override
     public String toString() {
         return literal;
     }
 }

 public enum MySubType implements SubCommandType {

     NONE(""), // Default for main commands w/o sub commands.
     BAR("bar"),
     BAZ("baz");

     // Literal sub command string used in shell.
     private final String literal;

     private NeuronSubType(final String name) {
         this.literal = name;
     }

     {@literal @}Override
     public String toString() {
         return literal;
     }
 }
 
// CHECKSTYLE:ON

And then define a literal to command map:

// CHECKSTYLE:OFF
public class MyLiteralCommandMap extends LiteralCommandMap {

     public MyLiteralCommandMap() {
         super(MySubType.NONE); // Default for main commands w/o sub commands.
     }

     {@literal @}Override
     protected void initCommandMap(Map<String, MainCommandType> map) {
         for (final MyMainType t : MyMainType.values()) {
             map.put(t.toString(), t);
         }
     }

     {@literal @}Override
     protected void initSubCommandMap(Map<String, SubCommandType> map) {
         for (final MySubType t : MySubType.values()) {
             if (t.toString().isEmpty()) {
                 continue; // Ignore to do not recognize empty strings as sub command in parser.
             }
             map.put(t.toString(), t);
         }
     }
 }
 
// CHECKSTYLE:ON

Now you can implement a simple REPL:

// CHECKSTYLE:OFF
Parser parser = Parsers.newParser(new MyLiteralCommandMap());
 BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

 while (true) {
     System.out.println("prompt> ");
     final String inputLine = input.readLine();

     try {
         final ShellCommand cmd = parser.parse(inputLine);
         // Do something with the shell command...

         if (shellCmd.getCommand() == NeuronMainType.EXIT) {
             break;
         }
     } catch (SyntaxException ex) {
         System.out.println("Error: " + ex.getMessage());
     }
 }

 input.close();
 
// CHECKSTYLE:ON

If you want to verify the parsed commands, e.g. check if a command has a proper subcommand or arguments you must implement CommandVerifier and pass it to the parser factory.

Copyright © 2014 Sven Strittmatter. All Rights Reserved.