Module de.carne

Class CmdLineProcessor


  • public final class CmdLineProcessor
    extends java.lang.Object
    Utility 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:

    Invoking the process() function processes all arguments and invokes the corresponding actions.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static void ignore​(java.lang.String arg)
      Pre-defined ignore action to ignore specific arguments.
      static boolean isActionArg​(java.lang.String arg)
      Checks whether an argument string is a valid action argument.
      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.
      CmdLineAction onSwitch​(java.util.function.Consumer<@NonNull java.lang.String> action)
      Add a CmdLineAction for switch argument (e.g. --switch) processing.
      void onUnknownArg​(java.util.function.Consumer<@NonNull java.lang.String> action)
      Add an action for unknown arguments (e.g.
      void onUnnamedOption​(java.util.function.Consumer<@NonNull java.lang.String> action)
      Add an action for unnamed options (e.g.
      void process()
      Processes the command line and invoke the correspond actions.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • 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:
        true if the argument string is a valid action argument.
      • 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:
        toString in class java.lang.Object