Even though this might not directly answer your question, I strongly suggest that you use a library for that. Some widely used options:
- Commons CLI - http://commons.apache.org/proper/commons-cli/
- JCommander - http://jcommander.org/
- JOpt-Simple - http://pholser.github.io/jopt-simple/
- ArgParse4J - http://argparse4j.sourceforge.net/
After all, "life is too short to parse command line parameters". As an example (using JCommander), you just need to define your parameters through annotations:
//class MyOptions
@Parameter(names = { "-d", "--outputDirectory" }, description = "Directory")
private String outputDirectory;
And then parse your bean using:
public static void main(String[] args){
MyOptions options = new MyOptions();
new JCommander(options, args);
}