Class Args
main(String[] args) method.
Supports the following grammars (all enabled by default; toggle via Args.Builder):
- Positional arguments — any token that does not start with a recognized flag prefix.
- Short flags —
-key value (and repeated-key v1 -key v2 to collect multiple values). - Long flags —
--key value . - Equals form —
--key=value /-key=value . - System-property style —
-Dkey=value (the leadingDis stripped).
Positional arguments are only collected before the first flag is seen; once a flag appears, every subsequent non-flag token becomes a value of the most recently seen flag. This matches the legacy parsing rule and is preserved for backwards compatibility.
Example:
Notes:
- This class is immutable and thread-safe once constructed.
get(String)returns the first value when a flag is repeated; usegetAll(String)for the full list.
-
Nested Class Summary
Nested Classes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintargCount()Returns the number of positional arguments.asMap()Returns the named-option contents as an unmodifiable map.static Args.Buildercreate()Creates a new builder for parsing arguments with non-default grammar.get(int index) Returns the positional argument at the specified zero-based index.Returns the first value of the named option, or empty if the option was not set.Returns all values for the named option in the order they appeared on the command line.booleanhas(int index) Returnstrue if a positional argument exists at the given index.booleanReturnstrue if the named option appears in the parsed args (even if no values were supplied).booleanisEmpty()Returnstrue if no positional arguments and no named options were captured.intReturns the number of distinct named options.Returns the positional arguments in the order they appeared on the command line.
-
Constructor Details
-
Args
Constructor that parses a raw command-line string using the default grammar.The string is split on whitespace honouring single- and double-quoted segments via
StringUtils.splitQuoted(String).- Parameters:
line- The raw command line. Can benull (treated as an empty argv).
-
Args
Constructor that parses an argv array using the default grammar.- Parameters:
argv- The arguments passed in through amain(String[] args) method. Can benull (treated as an empty argv).
-
-
Method Details
-
create
Creates a new builder for parsing arguments with non-default grammar.- Returns:
- A new builder.
-
get
Returns the positional argument at the specified zero-based index.- Parameters:
index- The position.- Returns:
- The argument value, or empty if
indexis out of range.
-
get
Returns the first value of the named option, or empty if the option was not set.If the option was specified multiple times (e.g.
-tag a -tag b ), this returns only the first value. UsegetAll(String)to get the full list.- Parameters:
key- The option name (without the flag prefix).- Returns:
- The first value, or empty if the key is unset or has no values.
-
getAll
Returns all values for the named option in the order they appeared on the command line.- Parameters:
key- The option name (without the flag prefix).- Returns:
- All values, or an empty list if the key is unset.
-
positional
Returns the positional arguments in the order they appeared on the command line.- Returns:
- An unmodifiable list of positional arguments.
-
argCount
Returns the number of positional arguments.- Returns:
- The count.
-
optionCount
Returns the number of distinct named options.- Returns:
- The count.
-
isEmpty
Returnstrue if no positional arguments and no named options were captured.- Returns:
true if both axes are empty.
-
has
Returnstrue if a positional argument exists at the given index.- Parameters:
index- The zero-based index.- Returns:
true if an argument exists at that index.
-
has
Returnstrue if the named option appears in the parsed args (even if no values were supplied).- Parameters:
key- The option name.- Returns:
true if the option was set.
-
asMap
Returns the named-option contents as an unmodifiable map.Positional arguments are not included — use
positional()for those.- Returns:
- An unmodifiable map of named options to their value lists.
-