Java - Get Command Line Parameters15/07/18 PermalinkI couldn't find a simple way of parsing command line parameters passed to programs without needlessly importing libraries.
String[]args is parsed using spaces only, so if you want more advanced parameters were you name the values and have multiple values like java Prog -opt0 blah blah2 -opt1 blah you'll need to parse it yourself.
Here's a simple way of doing more advanced command line parsing in Java:
public HashMap> getCommandLineOptions(String[]args){
HashMap> opts = null;
String opt = null;
for(String arg:args){
if(arg.startsWith("-")){
opt=arg;
}else if(opt!=null){
ArrayList oopts = opts.get(opt);
if(oopts==null){
oopts=new ArrayList();
}
oopts.add(arg);
opts.put(opt,oopts);
}
}
return opts;
}
|
|