Argument processing using GWT ToolBase

suggest change

If you want to parse more complex command-line arguments, e.g. with optional parameters, than the best is to use google’s GWT approach. All classes are public available at:

An example for handling the command-line myprogram -dir "~/Documents" -port 8888 is:

public class MyProgramHandler extends ToolBase {
   protected File dir;
   protected int port;
   // getters for dir and port

   public MyProgramHandler() {
       this.registerHandler(new ArgHandlerDir() {
            public void setDir(File dir) {
                this.dir = dir;
       this.registerHandler(new ArgHandlerInt() {
            public String[] getTagArgs() {
               return new String[]{"port"};
            public void setInt(int value) {
               this.port = value;
   public static void main(String[] args) {
      MyProgramHandler myShell = new MyProgramHandler();
      if (myShell.processArgs(args)) {
         // main program operation
         System.out.println(String.format("port: %d; dir: %s",
            myShell.getPort(), myShell.getDir()));

ArgHandler also has a method isRequired() which can be overwritten to say that the command-line argument is required (default return is false so that the argument is optional.

Feedback about page:

Optional: your email if you want me to get back to you:

Table Of Contents