Created
November 11, 2018 22:08
-
-
Save awwsmm/01e8d63c24690582fa4cf0d008a93f38 to your computer and use it in GitHub Desktop.
Parse command-line arguments and categorise them as "arguments", "flags", "long flags", or "other". Return an easy-to-use List<Entry>.
This file contains 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
import java.util.AbstractMap.SimpleEntry; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Map.Entry; | |
public class ParseCLArgs { | |
// try with | |
// $ java ParseCLArgs arg -short anotherArg --long - -- ---toolong | |
public static void main (String[] args) { | |
parseArgs(args).stream().forEach(System.out::println); | |
} | |
///--------------------------------------------------------------------------- | |
/// | |
/// parseArgs(): | |
/// parses and returns a list of command-line arguments and flags | |
/// | |
///--------------------------------------------------------------------------- | |
public enum CLArgType { ARGUMENT, FLAG, LONGFLAG, OTHER; } | |
public static List<Entry<CLArgType, String>> parseArgs (String[] args) { | |
// every command line flag-argument pair is of the form: | |
// <optional flag> <optional argument> | |
// | |
// so you could have a flag without an argument, like | |
// $ ./app -h <or> $ ./app --help | |
// | |
// or you could have an argument without a flag, like | |
// $ ./app file.txt <or> $ ./app dir/*.java | |
// | |
// or you could have a flag with a following argument, like | |
// $ ./app -r . <or> $ ./app --fast ../dir/thing* | |
// | |
// flags can be combined: (-a) + (-m) => (-am) | |
// flags can be followed by multiple arguments: rm -rf a b c ... | |
// flags can be repeated | |
// ...and so on. | |
// | |
// It's up to the user to determine what to *do* with these flags and | |
// arguments, and if a particular sequence of flags and arguments is | |
// valid. So all this method does is categorize each token as a flag | |
// or as an argument, strips off leading '-'s from flags, then returns | |
// a list of the flags and arguments in the order they were received. | |
ArrayList<Entry<CLArgType, String>> parsed = new ArrayList<>(args.length); | |
for (String arg : args) { | |
CLArgType type = null; | |
// if first char is '-' | |
if (arg.charAt(0) == '-') { | |
// if only '-' char was passed | |
if (arg.length() < 2) { | |
// argument is unusual, return "OTHER" type | |
type = CLArgType.OTHER; | |
parsed.add(new SimpleEntry<CLArgType, String>(type, arg)); | |
// if second char is '-' | |
} else if (arg.charAt(1) == '-') { | |
// if only "--" was passed | |
if (arg.length() < 3) { | |
// argument is unusual, return "OTHER" type | |
type = CLArgType.OTHER; | |
parsed.add(new SimpleEntry<CLArgType, String>(type, arg)); | |
continue; | |
} | |
// we have a "double flag" (like --this) | |
// (note, we could also have ---_-_-something-weird) | |
// it's in the user's hands now, though... | |
type = CLArgType.LONGFLAG; | |
parsed.add(new SimpleEntry<CLArgType, String>(type, arg.substring(2))); | |
} else { // we have a "single flag" (like -this) | |
// (note, we could also have -_-s-om_-e_thingweird) | |
// user needs to interpret after this point | |
type = CLArgType.FLAG; | |
parsed.add(new SimpleEntry<CLArgType, String>(type, arg.substring(1))); | |
} | |
} else { // otherwise, we have an argument | |
type = CLArgType.ARGUMENT; | |
parsed.add(new SimpleEntry<CLArgType, String>(type, arg)); | |
} | |
} | |
return parsed; | |
} | |
} // end class ParseCLArgs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment