Argument processing using GWT ToolBase
suggest changeIf 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:
https://gwt.googlesource.com/gwt/+/2.8.0-beta1/dev/core/src/com/google/gwt/util/tools/ToolBase.java
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() { @Override public void setDir(File dir) { this.dir = dir; } }); this.registerHandler(new ArgHandlerInt() { @Override public String[] getTagArgs() { return new String[]{"port"}; } @Override 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())); } System.exit(1); } }
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.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents