I would suggest using args4j.
All Java programs allow for command-line inputs through the main method:
public void main(String[] args)
But args4j allows you to easily create a class and parse flags something.exe -f flag1
:
public class MyOptions {
@Option(name="-r",usage="recursively run something")
private boolean recursive;
@Option(name="-o",usage="output to this file")
private File out;
@Option(name="-str") // no usage
private String str = "(default value)";
@Option(name="-n",usage="usage can have new lines in it\n and also it can be long")
private int num;
// receives other command line parameters than options
@Argument
private List arguments = new ArrayList();
}
And you just have to call this class from your main method:
public void main(String[] args) throws IOException {
MyOptions bean = new MyOptions()
CmdLineParser parser = new CmdLineParser(bean);
parser.parseArgument(args);
// ...
}