Skip to content

Instantly share code, notes, and snippets.

@gerritjvv
Created September 27, 2017 23:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gerritjvv/52d6670d5c73c9f854c4ebb89b3683f5 to your computer and use it in GitHub Desktop.
Save gerritjvv/52d6670d5c73c9f854c4ebb89b3683f5 to your computer and use it in GitHub Desktop.
Convert CLI main arguments into a Map for quick no extra lib console java apps
public static Map<String, Object> argumentsAsMap(String[] args){
Map<String, Object> m = new HashMap<>();
for(int i = 0; i < args.length; i++){
String lbl = args[i].trim();
if(lbl.startsWith("-") && i+1 < args.length && !args[i+1].trim().startsWith("-")) {
m.put(removePrefixes(lbl), args[i+1].trim());
i++;
} else {
m.put(removePrefixes(lbl), true);
}
}
return m;
}
//System.out.println("Map: " + argumentsAsMap(new String[]{"-a", "1", "--log", "-f", "--app", "323"}));
//
//Map: {app=323, a=1, log=true, f=true}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment