Created
September 27, 2017 23:16
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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