public final class DumbCLIParse extends Object
This is in honor of a past boss of mine, a crackerjack dev, who struggled (one time! but sadly on a projected screen... :-) ) with regular expressions. Here we have a tiny but useful command line arg parser based entirely on regular expressions. This one is for you, Louis.
The way you use this is by first call args(java.lang.String[]) with the main() args.
Just gives you a list back, but feels nice for examples.
There are two ways to use it. You can use the per arg parsing methods,
scanForArgWithParm(List, String) or scanForFlag(List, String, boolean)
passing the arg list in. Each of the scan methods will scan the entire list,
removing the first match and returning appropriately.
When you are done scanning for args, the remainder args are whatever is in the list.
Alternatively, you can pass the list to {link scanForAllFlags(List) and
then scanForAllParamArgs(List) in turn, and each will return a list of
matching flag or string args.
Personally, I find the incremental ones easier and more natural to code against, but some may prefer the "all" variants. See unit test for sample usage.
No one will mistake it for JCommander or any of the other full up CLI parsing packages, but very sufficient for rapid prototyping.
| Modifier and Type | Method and Description |
|---|---|
static List<String> |
args(String[] args)
Just turn main() args into a mutable list.
|
static List<org.sfj.DumbCLIParse.FlagArg> |
scanForAllFlags(List<String> all) |
static List<org.sfj.DumbCLIParse.StringArg> |
scanForAllParamArgs(List<String> all) |
static Optional<String> |
scanForArgWithParm(List<String> remaining,
String argname)
Scan for the first argument with a param name that matches the argname.
|
static boolean |
scanForFlag(List<String> remaining,
String argname,
boolean foundVal)
Scan for the first argument with a param name that matches the argname.
|
public static List<String> args(String[] args)
args - array of argspublic static Optional<String> scanForArgWithParm(List<String> remaining, String argname)
remaining - list of remaining args to parse fromargname - name of arg, case sensitivepublic static boolean scanForFlag(List<String> remaining, String argname, boolean foundVal)
remaining - list of remaining args to scan throughargname - argname to scan for, case sensitivefoundVal - value to use if it is foundCopyright © 2020. All rights reserved.