public static final Map<String,List<Object>> parseArgs(String optionsKey, List<String> defaultKeys, Object... args)
defaultKey
.
Options are stored in the return map under the optionsKey
with
the option long or short names as values in the list. Option long names
are added to the list without the leading "--" and short names without
the leading single dash "-".
The argument "--" is accepted as a delimiter indicating the end of
options and named operands. Any following arguments are treated as
default operands returned in the map under the defaultKey
, even
if they begin with the '-' character.
String args that could be passed as arguments to the echo
command,
assuming optionsKey="options"
and defaultKeys=["default"]
:
"--message" "hello" "world" --> {"message":["hello", "world"]} "-n --message" "hello" "world" --> {"message":["hello", "world"], "options":["n"]} "--noNewline --message" "ping" --> {"message":["ping"], "options":["noNewline"]} "hello" "world" --> {"default":["hello", "world"]} "-n" "hello" "world" --> {"default":["hello", "world"], "options":"n"} "--noNewline" "--" "hello" "world" --> {"default":["hello", "world"], "options":"noNewline"} "--" "8" "-" "7" "=" "1" --> {"default":["8", "-", "7", "=", "1"} "--" "8" "--" "7" "=" "15" --> {"default":["8", "--", "7", "=", "15"}
String args that could be passed as arguments to the ls
command,
again with optionsKey="options"
and defaultKeys=["default"]
:
"-lart" --> {"options":["l", "a", "r", "t"]} "-laR" "--files" "*.txt" "*.log" --> {"options":["l", "a", "R"], "files":["*.txt", "*.log"]} "-a" "--longFormat" "--files" "*" --> {"options":["a", "longFormat"], "files":["*"]} "-laR" "*.txt" "*.log" --> {"options":["l", "a", "R"], "default":["*.txt", "*.log"]} "-la" "--" "-*" "--*" --> {"options":["l", "a"], "default":["-*", "--*"]}
String args that could be passed as arguments to the grep
command
which has two default operands, hence optionsKey="options"
and
defaultKeys=["pattern","paths"]
:
"myword" "myfile.txt" --> {"pattern":["myword"], paths:["myfile.txt"]} "-i" "myword" "myfile.txt" --> {"options":["i"], "pattern":["myword"], paths:["myfile.txt"]} "-i" "error" "*.txt" "*.log" --> {"options":["i"], "pattern":["error"], paths:["*.txt", "*.log"]} "--ignoreCase" "--" "error" "*" --> {"options":["ignoreCase"], "pattern":["error"], paths:["*"]} "--ignoreCase" "--pattern" "error" "--" "*" --> {"options":["ignoreCase"], "pattern":["error"], paths:["*"]} "-i" "error" "--paths" "*" --> {"options":["i"], "pattern":["error"], paths:["*"]} "--ignoreCase" "--paths" "*" "--" "error" --> {"options":["ignoreCase"], "pattern":["error"], paths:["*"]}
optionsKey
- the map key to use for options aka no-value operandsdefaultKeys
- a list of map keys to use for operands when no operand name is
specified; only the last key can have multiple operand valuesargs
- the arguments to be parsedCopyright © 2024. All rights reserved.