Class CommandLineUtil


  • public class CommandLineUtil
    extends java.lang.Object
    Utilities to work with command line, parse arguments, etc.
    Since:
    5.1
    Restriction:
    This class is not intended to be subclassed by clients.
    Restriction:
    This class is not intended to be instantiated by clients.
    • Constructor Summary

      Constructors 
      Constructor Description
      CommandLineUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String[] argumentsToArray​(java.lang.String line)  
      static java.lang.String[] argumentsToArrayUnixStyle​(java.lang.String line)
      Parsing arguments in a shell style.
      static java.lang.String[] argumentsToArrayWindowsStyle​(java.lang.String line)
      Parsing arguments in a cmd style.
      static java.lang.String argumentsToString​(java.lang.String[] args, boolean encodeNewline)
      Converts argument array to a string suitable for passing to Bash like: This process reverses argumentsToArray(String), but does not restore the exact same results.
      static java.lang.String argumentsToStringBash​(java.lang.String[] args, boolean encodeNewline)
      Converts argument array to a string suitable for passing to Bash like:
      static java.lang.String argumentsToStringWindowsCreateProcess​(java.lang.String[] args, boolean encodeNewline)
      Converts argument array to a string suitable for passing to Windows CreateProcess
      • Methods inherited from class java.lang.Object

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

      • CommandLineUtil

        public CommandLineUtil()
    • Method Detail

      • argumentsToArray

        public static java.lang.String[] argumentsToArray​(java.lang.String line)
      • argumentsToArrayUnixStyle

        public static java.lang.String[] argumentsToArrayUnixStyle​(java.lang.String line)
        Parsing arguments in a shell style. i.e.
         ["a b c" d] -> [[a b c],[d]]
         [a   d] -> [[a],[d]]
         ['"quoted"'] -> [["quoted"]]
         [\\ \" \a] -> [[\],["],[a]]
         ["str\\str\a"] -> [[str\str\a]]
         
        Parameters:
        line -
        Returns:
        array of arguments, or empty array if line is null or empty
      • argumentsToArrayWindowsStyle

        public static java.lang.String[] argumentsToArrayWindowsStyle​(java.lang.String line)
        Parsing arguments in a cmd style. i.e.
         ["a b c" d] -> [[a b c],[d]]
         [a   d] -> [[a],[d]]
         ['"quoted"'] -> [['quoted']]
         [\\ \" \a] -> [[\\],["],[\a]]
         ["str\\str\a"] -> [[str\\str\a]]
         
        Parameters:
        line -
        Returns:
        array of arguments, or empty array if line is null or empty
      • argumentsToString

        public static java.lang.String argumentsToString​(java.lang.String[] args,
                                                         boolean encodeNewline)
        Converts argument array to a string suitable for passing to Bash like: This process reverses argumentsToArray(String), but does not restore the exact same results.
        Parameters:
        args - the arguments to convert and escape
        encodeNewline - true if newline (\r or \n) should be encoded
        Returns:
        args suitable for passing to some process that decodes the string into an argument array
        Since:
        6.2
      • argumentsToStringBash

        public static java.lang.String argumentsToStringBash​(java.lang.String[] args,
                                                             boolean encodeNewline)
        Converts argument array to a string suitable for passing to Bash like:
         /bin/bash -c <args>
         
        In this case the arguments array passed to exec or equivalent will be:
         argv[0] = "/bin/bash"
         argv[1] = "-c"
         argv[2] = argumentsToStringBashStyle(argumentsAsArray)
         
        Replace and concatenate all occurrences of:
        • ' with "'"

          (as ' is used to surround everything else it has to be quoted or escaped)

        • newline character, if encoded, with $'\n'

          (\n is treated literally within quotes or as just 'n' otherwise, whilst supplying the newline character literally ends the command)

        • Anything in between and around these occurrences is surrounded by single quotes.

          (to prevent bash from carrying out substitutions or running arbitrary code with backticks or $())

          Parameters:
          args - the arguments to convert and escape
          encodeNewline - true if newline (\r or \n) should be encoded
          Returns:
          args suitable for passing as single argument to bash
          Since:
          6.2
        • argumentsToStringWindowsCreateProcess

          public static java.lang.String argumentsToStringWindowsCreateProcess​(java.lang.String[] args,
                                                                               boolean encodeNewline)
          Converts argument array to a string suitable for passing to Windows CreateProcess
          Parameters:
          args - the arguments to convert and escape
          encodeNewline - true if newline (\r or \n) should be encoded
          Returns:
          args suitable for passing as single argument to CreateProcess on Windows
          Since:
          6.2