public class CommandLineUtil extends Object
Constructor and Description |
---|
CommandLineUtil() |
Modifier and Type | Method and Description |
---|---|
static String[] |
argumentsToArray(String line) |
static String[] |
argumentsToArrayUnixStyle(String line)
Parsing arguments in a shell style.
|
static String[] |
argumentsToArrayWindowsStyle(String line)
Parsing arguments in a cmd style.
|
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. |
static String |
argumentsToStringBash(String[] args,
boolean encodeNewline)
Converts argument array to a string suitable for passing to Bash like:
|
static String |
argumentsToStringWindowsCreateProcess(String[] args,
boolean encodeNewline)
Converts argument array to a string suitable for passing to Windows
CreateProcess
|
public static String[] argumentsToArrayUnixStyle(String line)
["a b c" d] -> [[a b c],[d]] [a d] -> [[a],[d]] ['"quoted"'] -> [["quoted"]] [\\ \" \a] -> [[\],["],[a]] ["str\\str\a"] -> [[str\str\a]]
line
- public static String[] argumentsToArrayWindowsStyle(String line)
["a b c" d] -> [[a b c],[d]] [a d] -> [[a],[d]] ['"quoted"'] -> [['quoted']] [\\ \" \a] -> [[\\],["],[\a]] ["str\\str\a"] -> [[str\\str\a]]
line
- public static String argumentsToString(String[] args, boolean encodeNewline)
argumentsToArray(String)
, but does not
restore the exact same results.args
- the arguments to convert and escapeencodeNewline
- true
if newline (\r
or
\n
) should be encodedpublic static String argumentsToStringBash(String[] args, boolean encodeNewline)
/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)
$'\n'
(\n
is treated literally within quotes or as just 'n'
otherwise, whilst supplying the newline character literally ends the
command)
(to prevent bash from carrying out substitutions or running arbitrary
code with backticks or $()
)
args
- the arguments to convert and escapeencodeNewline
- true
if newline (\r
or
\n
) should be encodedpublic static String argumentsToStringWindowsCreateProcess(String[] args, boolean encodeNewline)
args
- the arguments to convert and escapeencodeNewline
- true
if newline (\r
or
\n
) should be encodedCopyright (c) IBM Corp. and others 2004, 2020. All Rights Reserved.