Class CommandLine


  • public class CommandLine
    extends java.lang.Object
    CommandLine objects help handling command lines specifying processes to execute. The class can be used to a command line by an application.
    • Constructor Summary

      Constructors 
      Constructor Description
      CommandLine​(java.io.File executable)
      Constructs a command line without any arguments.
      CommandLine​(java.lang.String executable)
      Constructs a command line without any arguments.
      CommandLine​(java.nio.file.Path executable)
      Constructs a command line without any arguments.
      CommandLine​(CommandLine other)
      Copy constructor.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      CommandLine addArgument​(java.lang.String argument)
      Add a single argument.
      CommandLine addArgument​(java.lang.String argument, boolean handleQuoting)
      Add a single argument.
      CommandLine addArguments​(java.lang.String addArguments)
      Add multiple arguments.
      CommandLine addArguments​(java.lang.String[] addArguments)
      Add multiple arguments.
      CommandLine addArguments​(java.lang.String[] addArguments, boolean handleQuoting)
      Add multiple arguments.
      CommandLine addArguments​(java.lang.String addArguments, boolean handleQuoting)
      Add multiple arguments.
      java.lang.String[] getArguments()
      Gets the expanded and quoted command line arguments.
      java.lang.String getExecutable()
      Gets the executable.
      java.util.Map<java.lang.String,​?> getSubstitutionMap()
      Gets the substitution map.
      boolean isFile()
      Tests whether a file was used to set the executable.
      static CommandLine parse​(java.lang.String line)
      Create a command line from a string.
      static CommandLine parse​(java.lang.String line, java.util.Map<java.lang.String,​?> substitutionMap)
      Create a command line from a string.
      void setSubstitutionMap​(java.util.Map<java.lang.String,​?> substitutionMap)
      Sets the substitutionMap to expand variables in the command line.
      java.lang.String toString()
      Stringify operator returns the command line as a string.
      java.lang.String[] toStrings()
      Converts the command line as an array of strings.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • CommandLine

        public CommandLine​(CommandLine other)
        Copy constructor.
        Parameters:
        other - the instance to copy.
      • CommandLine

        public CommandLine​(java.io.File executable)
        Constructs a command line without any arguments.
        Parameters:
        executable - the executable file.
      • CommandLine

        public CommandLine​(java.nio.file.Path executable)
        Constructs a command line without any arguments.
        Parameters:
        executable - the executable file.
        Since:
        1.5.0
      • CommandLine

        public CommandLine​(java.lang.String executable)
        Constructs a command line without any arguments.
        Parameters:
        executable - the executable.
        Throws:
        java.lang.NullPointerException - on null input.
        java.lang.IllegalArgumentException - on empty input.
    • Method Detail

      • parse

        public static CommandLine parse​(java.lang.String line)
        Create a command line from a string.
        Parameters:
        line - the first element becomes the executable, the rest the arguments.
        Returns:
        the parsed command line.
        Throws:
        java.lang.IllegalArgumentException - If line is null or all whitespace.
      • parse

        public static CommandLine parse​(java.lang.String line,
                                        java.util.Map<java.lang.String,​?> substitutionMap)
        Create a command line from a string.
        Parameters:
        line - the first element becomes the executable, the rest the arguments.
        substitutionMap - the name/value pairs used for substitution.
        Returns:
        the parsed command line.
        Throws:
        java.lang.IllegalArgumentException - If line is null or all whitespace.
      • addArgument

        public CommandLine addArgument​(java.lang.String argument)
        Add a single argument. Handles quoting.
        Parameters:
        argument - The argument to add.
        Returns:
        The command line itself.
        Throws:
        java.lang.IllegalArgumentException - If argument contains both single and double quotes.
      • addArgument

        public CommandLine addArgument​(java.lang.String argument,
                                       boolean handleQuoting)
        Add a single argument.
        Parameters:
        argument - The argument to add.
        handleQuoting - Add the argument with/without handling quoting.
        Returns:
        The command line itself.
      • addArguments

        public CommandLine addArguments​(java.lang.String addArguments)
        Add multiple arguments. Handles parsing of quotes and whitespace. Please note that the parsing can have undesired side-effects therefore it is recommended to build the command line incrementally.
        Parameters:
        addArguments - A string containing multiple arguments.
        Returns:
        The command line itself.
      • addArguments

        public CommandLine addArguments​(java.lang.String addArguments,
                                        boolean handleQuoting)
        Add multiple arguments. Handles parsing of quotes and whitespace. Please note that the parsing can have undesired side-effects therefore it is recommended to build the command line incrementally.
        Parameters:
        addArguments - A string containing multiple arguments.
        handleQuoting - Add the argument with/without handling quoting.
        Returns:
        The command line itself.
      • addArguments

        public CommandLine addArguments​(java.lang.String[] addArguments)
        Add multiple arguments. Handles parsing of quotes and whitespace.
        Parameters:
        addArguments - An array of arguments.
        Returns:
        The command line itself.
      • addArguments

        public CommandLine addArguments​(java.lang.String[] addArguments,
                                        boolean handleQuoting)
        Add multiple arguments.
        Parameters:
        addArguments - An array of arguments.
        handleQuoting - Add the argument with/without handling quoting.
        Returns:
        The command line itself.
      • getArguments

        public java.lang.String[] getArguments()
        Gets the expanded and quoted command line arguments.
        Returns:
        The quoted arguments.
      • getExecutable

        public java.lang.String getExecutable()
        Gets the executable.
        Returns:
        The executable.
      • getSubstitutionMap

        public java.util.Map<java.lang.String,​?> getSubstitutionMap()
        Gets the substitution map.
        Returns:
        the substitution map.
      • isFile

        public boolean isFile()
        Tests whether a file was used to set the executable.
        Returns:
        true whether a file was used for setting the executable.
      • setSubstitutionMap

        public void setSubstitutionMap​(java.util.Map<java.lang.String,​?> substitutionMap)
        Sets the substitutionMap to expand variables in the command line.
        Parameters:
        substitutionMap - the map
      • toString

        public java.lang.String toString()
        Stringify operator returns the command line as a string. Parameters are correctly quoted when containing a space or left untouched if they are already quoted.
        Overrides:
        toString in class java.lang.Object
        Returns:
        the command line as single string.
      • toStrings

        public java.lang.String[] toStrings()
        Converts the command line as an array of strings.
        Returns:
        The command line as a string array.