args4j is a simple library for parsing command-line arguments for Java 8+.
To parse command line arguments, simply use:
public static void main(String[] args) {
List<Token> tokens=Args.parse(args);
}
For example, in the above code, the commented list of program arguments would result in the following token objects:
// -a -abc --alpha --alpha=bravo - -- charlie --delta -echo
Arrays.asList(
new SwitchNameToken(SwitchName.of("a"), false),
new SwitchNameToken(SwitchName.of("a"), false),
new SwitchNameToken(SwitchName.of("b"), true),
new SwitchNameToken(SwitchName.of("c"), true),
new SwitchNameToken(SwitchName.of("alpha"), false),
new SwitchNameToken(SwitchName.of("alpha"), false),
new ValueToken("bravo", true),
new ValueToken("-", false),
new ValueToken("charlie", false),
new ValueToken("--delta", false),
new ValueToken("-echo", false));
The Dialect
object creates the ArgTokenizer
object that the ArgsParser
object uses to convert program arguments into Token
objects.
Users can specify a Dialect
like this:
public static void main(String[] args) {
List<Token> tokens=Args.parse(UnixDialect.INSTANCE, args);
}
If users do not specify a dialect, then the UnixDialect
is used.
The default Unix dialect supports the following syntax:
-
Long Switch:
- A command-line argument that begins with
--
followed by a name. - Example:
--alpha
- Represents an option or setting with a descriptive name.
- A command-line argument that begins with
-
Long Switch with Attached Value:
- A long switch followed by an
=
and its associated value. - Example:
--alpha=bravo
- Combines a switch and its value in a single token.
- A long switch followed by an
-
Short Switch:
- A single-character switch prefixed with
-
. - Example:
-a
- Provides a shorthand for enabling options.
- A single-character switch prefixed with
-
Short Switch Batch:
- A single argument starting with
-
, followed by multiple single-character switches concatenated together. - Example:
-abc
- Represents multiple short switches:
-a
,-b
, and-c
.
- A single argument starting with
-
Separator:
- A meta-token represented by
--
, which forces all subsequent arguments to be treated as values, even if they look like switches. - Example:
--
- Ensures arguments like
--delta
and-echo
are treated as value tokens.
- A meta-token represented by
-
Value:
- Any other token
- Example:
alpha
- Represents the actual data passed to applications
The MS/DOS dialect supports the following syntax:
-
Switch:
- A command-line argument that begins with
/
followed by a name. - Examples:
/a
,/alpha
- Represents an option or setting.
- A command-line argument that begins with
-
Value:
- An argument that does not start with
/
and is treated as a value rather than a switch. - Example:
value
- Represents data or parameters passed to the command.
- An argument that does not start with
Users are free to implement their own Dialect
objects. See UnixDialect
and MsDosDialect
for examples.