- java.lang.Object
-
- de.carne.util.cmdline.CmdLineProcessor
-
public final class CmdLineProcessor extends java.lang.ObjectUtility class for command line processing.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 (e.g.
--switchor-s; see onSwitch(Consumer)) - option (e.g.
--option valueor-o value; see onOption(BiConsumer)) - unnamed (e.g.
file.txt); see onUnnamedOption(Consumer)) - unknown (default processing; see onUnknownArg(Consumer))
- switch (e.g.
-
-
Constructor Summary
Constructors Constructor Description CmdLineProcessor(java.lang.String cmd, java.lang.Iterable<@Nullable java.lang.String> args)Constructs a new CmdLineProcessor instance.CmdLineProcessor(java.lang.String cmd, java.lang.String[] args)Constructs a new CmdLineProcessor instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static voidignore(java.lang.String arg)Pre-defined ignore action to ignore specific arguments.static booleanisActionArg(java.lang.String arg)Checks whether an argument string is a valid action argument.CmdLineActiononOption(java.util.function.BiConsumer<@NonNull java.lang.String,@NonNull java.lang.String> action)Add a CmdLineAction for option argument (e.g. --option value) processing.CmdLineActiononSwitch(java.util.function.Consumer<@NonNull java.lang.String> action)Add a CmdLineAction for switch argument (e.g. --switch) processing.voidonUnknownArg(java.util.function.Consumer<@NonNull java.lang.String> action)Add an action for unknown arguments (e.g.voidonUnnamedOption(java.util.function.Consumer<@NonNull java.lang.String> action)Add an action for unnamed options (e.g.voidprocess()Processes the command line and invoke the correspond actions.java.lang.StringtoString()
-
-
-
Constructor Detail
-
CmdLineProcessor
public CmdLineProcessor(java.lang.String cmd, java.lang.String[] args)Constructs a new CmdLineProcessor instance.- Parameters:
cmd- the command executing the command line (used by toString() to build a complete command line string).args- the command line to process.
-
CmdLineProcessor
public CmdLineProcessor(java.lang.String cmd, java.lang.Iterable<@Nullable java.lang.String> args)Constructs a new CmdLineProcessor instance.- Parameters:
cmd- the command executing the command line.args- the command line to process.
-
-
Method Detail
-
isActionArg
public static boolean isActionArg(java.lang.String arg)
Checks whether an argument string is a valid action argument.An action argument must be of the form single '-' and character (e.g.
-a) or double '-' and a name (e.g.--argument).- Parameters:
arg- the argument string to check.- Returns:
trueif the argument string is a valid action argument.
-
process
public void process() throws CmdLineExceptionProcesses the command line and invoke the correspond actions.- Throws:
CmdLineException- if the command line contains an error.- See Also:
onSwitch(Consumer),onOption(BiConsumer),onUnnamedOption(Consumer),onUnknownArg(Consumer)
-
onSwitch
public CmdLineAction onSwitch(java.util.function.Consumer<@NonNull java.lang.String> action)
Add a CmdLineAction for switch argument (e.g. --switch) processing.- Parameters:
action- The Consumer to invoke with the argument string.- Returns:
- The created CmdLineAction.
-
onOption
public CmdLineAction onOption(java.util.function.BiConsumer<@NonNull java.lang.String,@NonNull java.lang.String> action)
Add a CmdLineAction for option argument (e.g. --option value) processing.- Parameters:
action- The BiConsumer to invoke with the argument and option string.- Returns:
- The created CmdLineAction.
-
onUnnamedOption
public void onUnnamedOption(java.util.function.Consumer<@NonNull java.lang.String> action)
Add an action for unnamed options (e.g.file.txt).If no action is defined for unnamed options the command line processing will fail in case an unnamed option is encountered.
- Parameters:
action- The Consumer to invoke with the option string.
-
onUnknownArg
public void onUnknownArg(java.util.function.Consumer<@NonNull java.lang.String> action)
Add an action for unknown arguments (e.g.--unknown).If no action is defined for unknown arguments the command line processing will fail in case an unknown arguments is encountered.
- Parameters:
action- The Consumer to invoke with the argument string.
-
ignore
public static void ignore(java.lang.String arg)
Pre-defined ignore action to ignore specific arguments.- Parameters:
arg- The ignored argument.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-