public class GeneralCommandLine extends java.lang.Object implements UserDataHolder
Main idea of the class is to accept parameters "as-is", just as they should look to an external process, and quote/escape them
as required by the underlying platform - so to run some program with a "parameter with space" all that's needed is
new GeneralCommandLine("some program", "parameter with space").createProcess()
.
Consider the following things when using this class.
Three options here
.
For commands designed from the ground up for typing into a terminal, use CONSOLE
(typical cases: version controls, Node.js and all the surrounding stuff, Python and Ruby interpreters and utilities, etc).
For GUI apps and CLI tools that aren't primarily intended to be launched by humans, use SYSTEM
(examples: UI builders, browsers, XCode components). And for the empty environment, there is NONE
.
According to an extensive research conducted by British scientists (tm) on a diverse population of both wild and domesticated tools
(no one was harmed), most of them are either insensitive to an environment or fall into the first category,
thus backing up the choice of CONSOLE as the default value.
getCharset()
method is used by classes like OSProcessHandler
or ExecUtil
to decode bytes of a child's output stream. For proper conversion,
the same value should be used on another side of the pipe. Chances are you don't have to mess with the setting -
because a platform-dependent guessing behind Charset.defaultCharset()
is used by default and a child process
may happen to use a similar heuristic.
If the above automagic fails or more control is needed, the charset may be set explicitly. Again, do not forget the other side -
call addParameter("-Dfile.encoding=...")
for Java-based tools, or use withEnvironment("HGENCODING", "...")
for Mercurial, etc.com.intellij.execution.util.ExecUtil
,
OSProcessHandler
Modifier and Type | Class and Description |
---|---|
static class |
GeneralCommandLine.ParentEnvironmentType
Determines the scope of a parent environment passed to a child process.
|
Modifier | Constructor and Description |
---|---|
|
GeneralCommandLine() |
protected |
GeneralCommandLine(GeneralCommandLine original) |
|
GeneralCommandLine(java.util.List<java.lang.String> command) |
|
GeneralCommandLine(java.lang.String... command) |
Modifier and Type | Method and Description |
---|---|
void |
addParameter(java.lang.String parameter) |
void |
addParameters(java.util.List<java.lang.String> parameters) |
void |
addParameters(java.lang.String... parameters) |
protected java.lang.ProcessBuilder |
buildProcess(java.lang.ProcessBuilder builder)
Executed with pre-filled ProcessBuilder as the param and
gives the last chance to configure starting process
parameters before a process is started
|
java.lang.Process |
createProcess() |
java.nio.charset.Charset |
getCharset() |
java.util.List<java.lang.String> |
getCommandLineList(java.lang.String exeName) |
java.lang.String |
getCommandLineString()
Returns string representation of this command line.
Warning: resulting string is not OS-dependent - do not use it for executing this command line. |
java.lang.String |
getCommandLineString(java.lang.String exeName)
Returns string representation of this command line.
Warning: resulting string is not OS-dependent - do not use it for executing this command line. |
java.util.Map<java.lang.String,java.lang.String> |
getEffectiveEnvironment()
Returns an environment as seen by a child process,
that is the
environment merged with the parent one. |
java.util.Map<java.lang.String,java.lang.String> |
getEnvironment()
Note: the map returned is forgiving to passing null values into putAll().
|
java.lang.String |
getExePath() |
java.io.File |
getInputFile() |
ParametersList |
getParametersList() |
java.util.Map<java.lang.String,java.lang.String> |
getParentEnvironment()
Returns an environment that will be inherited by a child process.
|
GeneralCommandLine.ParentEnvironmentType |
getParentEnvironmentType() |
java.lang.String |
getPreparedCommandLine()
Prepares command (quotes and escapes all arguments) and returns it as a newline-separated list.
|
java.lang.String |
getPreparedCommandLine(Platform platform)
Prepares command (quotes and escapes all arguments) and returns it as a newline-separated list
(suitable e.g.
|
<T> T |
getUserData(Key<T> key) |
java.io.File |
getWorkDirectory() |
static java.lang.String |
inescapableQuote(java.lang.String parameter)
Normally, double quotes in parameters are escaped, so they arrive to a called program as-is.
|
boolean |
isPassParentEnvironment() |
boolean |
isRedirectErrorStream() |
protected java.util.List<java.lang.String> |
prepareCommandLine(java.lang.String command,
java.util.List<java.lang.String> parameters,
Platform platform) |
<T> void |
putUserData(Key<T> key,
T value)
Add a new user data value to this object.
|
void |
setCharset(java.nio.charset.Charset charset) |
void |
setExePath(java.lang.String exePath) |
void |
setPassParentEnvironment(boolean passParentEnvironment)
Deprecated.
|
void |
setRedirectErrorStream(boolean redirectErrorStream) |
protected void |
setupEnvironment(java.util.Map<java.lang.String,java.lang.String> environment) |
void |
setWorkDirectory(java.io.File workDirectory) |
void |
setWorkDirectory(java.lang.String path) |
protected java.lang.Process |
startProcess(java.util.List<java.lang.String> escapedCommands) |
java.lang.String |
toString() |
GeneralCommandLine |
withCharset(java.nio.charset.Charset charset) |
GeneralCommandLine |
withEnvironment(java.util.Map<java.lang.String,java.lang.String> environment) |
GeneralCommandLine |
withEnvironment(java.lang.String key,
java.lang.String value) |
GeneralCommandLine |
withExePath(java.lang.String exePath) |
GeneralCommandLine |
withInput(java.io.File file) |
GeneralCommandLine |
withParameters(java.util.List<java.lang.String> parameters) |
GeneralCommandLine |
withParameters(java.lang.String... parameters) |
GeneralCommandLine |
withParentEnvironmentType(GeneralCommandLine.ParentEnvironmentType type) |
GeneralCommandLine |
withRedirectErrorStream(boolean redirectErrorStream) |
GeneralCommandLine |
withWorkDirectory(java.io.File workDirectory) |
GeneralCommandLine |
withWorkDirectory(java.lang.String path) |
public GeneralCommandLine()
public GeneralCommandLine(java.lang.String... command)
public GeneralCommandLine(java.util.List<java.lang.String> command)
protected GeneralCommandLine(GeneralCommandLine original)
public java.lang.String getExePath()
public GeneralCommandLine withExePath(java.lang.String exePath)
public void setExePath(java.lang.String exePath)
public java.io.File getWorkDirectory()
public GeneralCommandLine withWorkDirectory(java.lang.String path)
public GeneralCommandLine withWorkDirectory(java.io.File workDirectory)
public void setWorkDirectory(java.lang.String path)
public void setWorkDirectory(java.io.File workDirectory)
public java.util.Map<java.lang.String,java.lang.String> getEnvironment()
public GeneralCommandLine withEnvironment(java.util.Map<java.lang.String,java.lang.String> environment)
public GeneralCommandLine withEnvironment(java.lang.String key, java.lang.String value)
public boolean isPassParentEnvironment()
@Deprecated public void setPassParentEnvironment(boolean passParentEnvironment)
withParentEnvironmentType(ParentEnvironmentType)
public GeneralCommandLine.ParentEnvironmentType getParentEnvironmentType()
public GeneralCommandLine withParentEnvironmentType(GeneralCommandLine.ParentEnvironmentType type)
public java.util.Map<java.lang.String,java.lang.String> getParentEnvironment()
getEffectiveEnvironment()
public java.util.Map<java.lang.String,java.lang.String> getEffectiveEnvironment()
environment
merged with the parent
one.public void addParameters(java.lang.String... parameters)
public void addParameters(java.util.List<java.lang.String> parameters)
public GeneralCommandLine withParameters(java.lang.String... parameters)
public GeneralCommandLine withParameters(java.util.List<java.lang.String> parameters)
public void addParameter(java.lang.String parameter)
public ParametersList getParametersList()
public java.nio.charset.Charset getCharset()
public GeneralCommandLine withCharset(java.nio.charset.Charset charset)
public void setCharset(java.nio.charset.Charset charset)
public boolean isRedirectErrorStream()
public GeneralCommandLine withRedirectErrorStream(boolean redirectErrorStream)
public void setRedirectErrorStream(boolean redirectErrorStream)
public java.io.File getInputFile()
public GeneralCommandLine withInput(java.io.File file)
public java.lang.String getCommandLineString()
public java.lang.String getCommandLineString(java.lang.String exeName)
exeName
- use this executable name instead of given by setExePath(String)
public java.util.List<java.lang.String> getCommandLineList(java.lang.String exeName)
public java.lang.String getPreparedCommandLine()
getPreparedCommandLine(Platform)
public java.lang.String getPreparedCommandLine(Platform platform)
platform
- a target platformprotected java.util.List<java.lang.String> prepareCommandLine(java.lang.String command, java.util.List<java.lang.String> parameters, Platform platform)
public java.lang.Process createProcess() throws ExecutionException
ExecutionException
protected java.lang.Process startProcess(java.util.List<java.lang.String> escapedCommands) throws java.io.IOException
java.io.IOException
protected java.lang.ProcessBuilder buildProcess(java.lang.ProcessBuilder builder)
builder
- filed ProcessBuilderprotected void setupEnvironment(java.util.Map<java.lang.String,java.lang.String> environment)
public static java.lang.String inescapableQuote(java.lang.String parameter)
'cmd /c start "title" ...'
) should get their quotes non-escaped.
Wrapping a parameter by this method (instead of using quotes) will do exactly this.com.intellij.execution.util.ExecUtil#getTerminalCommand(String, String)
public java.lang.String toString()
toString
in class java.lang.Object
public <T> T getUserData(Key<T> key)
getUserData
in interface UserDataHolder
public <T> void putUserData(Key<T> key, T value)
UserDataHolder
putUserData
in interface UserDataHolder