Class CommandLineUtil


  • public class CommandLineUtil
    extends 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 Detail

      • CommandLineUtil

        public CommandLineUtil()
    • Method Detail

      • argumentsToArray

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

        public static String[] argumentsToArrayUnixStyle​(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 String[] argumentsToArrayWindowsStyle​(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 String argumentsToString​(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 String argumentsToStringBash​(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 String argumentsToStringWindowsCreateProcess​(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