Parse options from the list of arguments, removing those which are recognised. Anything which is not recognised is left as is. @param args --- the list of argument strings. This is modified by removing those which are processed. @param options --- the list of OptArg
(List<String> args, OptArg... options)
| 300 | * encountered (that is, a token starting with '-').. |
| 301 | */ |
| 302 | public static Map<String,Object> parseOptions(List<String> args, OptArg... options) { |
| 303 | HashMap<String,Object> result = new HashMap<>(); |
| 304 | HashMap<String,OptArg> soptmap = new HashMap<>(); |
| 305 | HashMap<String,OptArg> loptmap = new HashMap<>(); |
| 306 | |
| 307 | for(OptArg opt : options) { |
| 308 | if(opt.defaultValue != null) { |
| 309 | result.put(opt.option, opt.defaultValue); |
| 310 | } |
| 311 | loptmap.put(opt.option, opt); |
| 312 | soptmap.put(opt.shortForm, opt); |
| 313 | } |
| 314 | |
| 315 | Iterator<String> iter = args.iterator(); |
| 316 | while(iter.hasNext()) { |
| 317 | String arg = iter.next(); |
| 318 | OptArg opt; |
| 319 | String param; |
| 320 | if (arg.startsWith("--")) { |
| 321 | Object[] p = parseLongForm(arg, iter, loptmap); |
| 322 | opt = (OptArg) p[0]; |
| 323 | param = (String) p[1]; |
| 324 | } else if(arg.startsWith("-")) { |
| 325 | Object[] p = parseShortForm(arg, iter, soptmap); |
| 326 | opt = (OptArg) p[0]; |
| 327 | param = (String) p[1]; |
| 328 | } else { |
| 329 | continue; |
| 330 | } |
| 331 | // Process option |
| 332 | Kind k = opt.argument; |
| 333 | if (k != null) { |
| 334 | k.process(opt.option, param, result); |
| 335 | } else { |
| 336 | result.put(opt.option, null); |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | return result; |
| 341 | } |
| 342 | |
| 343 | private static Object[] parseLongForm(String arg, Iterator<String> iter, Map<String, OptArg> opts) { |
| 344 | arg = arg.substring(2,arg.length()); |
no test coverage detected