Parse command line in java


/ Published in: Java
Save to your folder(s)

input: -width 123 -height 234 -minimized -visible
output map: {"-width":"123", "-height":"234", "-minimized":null, "-visible":null}


Copy this code and paste it in your HTML
  1. public static void main(String[] args) throws Exception
  2. {
  3. // parse arguments
  4. Map<String, String> params = new HashMap<String, String>();
  5. for (int i = 0; i < args.length; i++) {
  6. String k = args[i], v = null;
  7. assert (k.startsWith("-"));
  8.  
  9. // see if there's a value following
  10. int j = i + 1;
  11. if (j < args.length) {
  12. String _v = args[j];
  13. if (!_v.startsWith("-")) {
  14. v = _v;
  15. i++;
  16. }
  17. }
  18.  
  19. params.put(k, v);
  20. }
  21. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.