public final class CmdLineProcessor
extends java.lang.Object
An instance of this class wraps a single command line.
Actions can be added to define how the encountered arguments are processed. The following action types are supported:
--switch or -s; see onSwitch(Consumer))--option value or -o value; see onOption(BiConsumer))file.txt); see onUnnamedOption(Consumer))| Constructor and Description |
|---|
CmdLineProcessor(java.lang.String cmd,
java.lang.Iterable<java.lang.String> args)
Construct CmdLineProcessor.
|
CmdLineProcessor(java.lang.String cmd,
java.lang.String[] args)
Construct CmdLineProcessor.
|
| Modifier and Type | Method and Description |
|---|---|
static void |
ignore(java.lang.String arg)
Pre-defined ignore action to ignore specific arguments.
|
static boolean |
isActionArg(java.lang.String arg)
Check whether an argument string is a valid action argument.
|
CmdLineAction |
onOption(java.util.function.BiConsumer<java.lang.String,java.lang.String> action)
Add a CmdLineAction for option argument ({@code e.g.
|
CmdLineAction |
onSwitch(java.util.function.Consumer<java.lang.String> action)
Add a CmdLineAction for switch argument ({@code e.g.
|
void |
onUnknownArg(java.util.function.Consumer<java.lang.String> action)
Add an action for unknown arguments (e.g.
|
void |
onUnnamedOption(java.util.function.Consumer<java.lang.String> action)
Add an action for unnamed options (e.g.
|
void |
process()
Process the command line and invoke the correspond actions.
|
java.lang.String |
toString() |
public CmdLineProcessor(java.lang.String cmd,
java.lang.String[] args)
cmd - The command executing the command line (used by toString() to build a complete command
line string).args - The command line to process.public CmdLineProcessor(java.lang.String cmd,
java.lang.Iterable<java.lang.String> args)
cmd - The command executing the command line.args - The command line to process.public static boolean isActionArg(java.lang.String arg)
An action argument must be of the form single '-' and character (e.g. -a) or double '-' and a name (e.g.
--argument).
arg - The argument string to check.true if the argument string is a valid action argument.public void process()
throws CmdLineException
CmdLineException - if the command line contains an error.onSwitch(Consumer),
onOption(BiConsumer),
onUnnamedOption(Consumer),
onUnknownArg(Consumer)public CmdLineAction onSwitch(java.util.function.Consumer<java.lang.String> action)
e.g. --switch) processing.action - The Consumer to invoke with the argument string.public CmdLineAction onOption(java.util.function.BiConsumer<java.lang.String,java.lang.String> action)
e.g. --option value) processing.action - The BiConsumer to invoke with the argument and option string.public void onUnnamedOption(java.util.function.Consumer<java.lang.String> action)
file.txt).
If no action is defined for unnamed options the command line processing will fail in case an unnamed option is encountered.
action - The Consumer to invoke with the option string.public void onUnknownArg(java.util.function.Consumer<java.lang.String> action)
--unknown).
If no action is defined for unknown arguments the command line processing will fail in case an unknown arguments is encountered.
action - The Consumer to invoke with the argument string.public static void ignore(java.lang.String arg)
arg - The ignored argument.public java.lang.String toString()
toString in class java.lang.Object