Depending on how complex your input is you can use either handcrafted solution using Command and/or Interpreter pattern or you can employ free XText framework.
Interpreter design pattern is very useful if your grammar is not too complex, but program input conforms to a DSL (Domain Specific Language). In your case, inputs add 5 2
and define foo
look like a part of larger grammar. If so, go with Interpreter. If the grammar is complex, however, then the best approach is to utilize DSL-generation-library like XText.
In case you wanted parse command line arguments you should have tried Apache Commons CLI library.
When it comes to Java, however, there is one more library worth checking - Cliche. Its main pros are extreme simplicity and annotation-driven model. Please find an example below:
// Cliche usage example
public class Calculator {
@Command
public void define(String variable) { ... }
@Command
public int add(int a, int b) {
return a + b;
}
public static void main(String[] args) throws IOException {
ShellFactory
.createConsoleShell("my-prompt", "", new Calculator())
.commandLoop();
}
}