Class Commandline
- All Implemented Interfaces:
Cloneable
Commandline objects help handling command lines specifying processes to execute.
The class can be used to define a command line as nested elements or as a helper to define a command line by an application.
<someelement>
<acommandline executable="/executable/to/run">
<argument value="argument 1" />
<argument line="argument_1 argument_2 argument_3" />
<argument value="argument 4" />
</acommandline>
</someelement>
The element someelement
must provide a method createAcommandline
which returns an instance
of this class.
- Author:
- thomas.haas@softwired-inc.com, Stefan Bodewig
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
class
Class to keep track of the position of an Argument. -
Field Summary
-
Constructor Summary
ConstructorDescriptionCreate a new command line object.Commandline
(String toProcess) Create a new command line object, given a command following POSIX sh quoting rulesCommandline
(String toProcess, Shell shell) Create a new command line object.Commandline
(Shell shell) Create a new command line object. -
Method Summary
Modifier and TypeMethodDescriptionvoid
void
Adds an argument object to our list of args.void
addArguments
(String[] line) void
addEnvironment
(String name, String value) Add an environment variablevoid
Add system environment variablesvoid
clear()
Clear out the whole command line.void
Clear out the arguments but leave the executable in place for another operation.clone()
Creates an argument object.createArg
(boolean insertAtStart) Deprecated.UsecreateArg()
insteadcreateArgument
(boolean insertAtStart) Deprecated.UsecreateArg(boolean)
insteadThis marker can be used to locate a position on the commandline - to insert something for example - when all parameters have been set.execute()
Executes the command.String[]
String[]
String[]
Return an executable name, quoted for shell use.long
getPid()
String[]
Returns the executable and all defined arguments.getShell()
Get the shell to be used in this command line.String[]
Returns the shell, executable and all defined arguments.static String
quoteArgument
(String argument) Deprecated.UseCommandLineUtils.quote(String)
instead.void
setExecutable
(String executable) Sets the executable to run.void
setPid
(long pid) void
Allows to set the shell to be used in this command line.void
setWorkingDirectory
(File workingDirectory) Sets execution directory.void
setWorkingDirectory
(String path) Sets execution directory.int
size()
toString()
static String
Deprecated.UseCommandLineUtils.toString(String[])
instead.static String[]
translateCommandline
(String toProcess) Deprecated.UseCommandLineUtils.translateCommandline(String)
instead.
-
Field Details
-
OS_NAME
Deprecated.UseOs
class instead.- See Also:
-
WINDOWS
Deprecated.UseOs
class instead.- See Also:
-
arguments
-
envVars
-
executable
Deprecated.UsesetExecutable(String)
instead.
-
-
Constructor Details
-
Commandline
Create a new command line object. Shell is autodetected from operating system Shell usage is only desirable when generating code for remote execution.- Parameters:
toProcess
- sh to processshell
- Shell to use
-
Commandline
Create a new command line object. Shell is autodetected from operating system Shell usage is only desirable when generating code for remote execution.- Parameters:
shell
- the Shell
-
Commandline
Create a new command line object, given a command following POSIX sh quoting rules- Parameters:
toProcess
- the process
-
Commandline
public Commandline()Create a new command line object.
-
-
Method Details
-
getPid
public long getPid() -
setPid
public void setPid(long pid) -
createArgument
Deprecated.UsecreateArg()
insteadCreates an argument object.
Each commandline object has at most one instance of the argument class. This method calls
this.createArgument(false)
.- Returns:
- the argument object.
- See Also:
-
createArgument
Deprecated.UsecreateArg(boolean)
insteadCreates an argument object and adds it to our list of args.
Each commandline object has at most one instance of the argument class.
- Parameters:
insertAtStart
- if true, the argument is inserted at the beginning of the list of args, otherwise it is appended.- Returns:
- Argument the argument Object
-
createArg
Creates an argument object.
Each commandline object has at most one instance of the argument class. This method calls
this.createArgument(false)
.- Returns:
- the argument object.
- See Also:
-
createArg
- Parameters:
insertAtStart
- if true, the argument is inserted at the beginning of the list of args, otherwise it is appended.- Returns:
- Creates an argument object and adds it to our list of args.
Each commandline object has at most one instance of the argument class.
-
addArg
- Parameters:
argument
- the argument- See Also:
-
addArg
Adds an argument object to our list of args.- Parameters:
argument
- the argumentinsertAtStart
- if true, the argument is inserted at the beginning of the list of args, otherwise it is appended.
-
setExecutable
Sets the executable to run.- Parameters:
executable
- the executable
-
getLiteralExecutable
- Returns:
- Executable to be run, as a literal string (no shell quoting/munging)
-
getExecutable
Return an executable name, quoted for shell use. Shell usage is only desirable when generating code for remote execution.- Returns:
- Executable to be run, quoted for shell interpretation
-
addArguments
-
addEnvironment
Add an environment variable- Parameters:
name
- namevalue
- value
-
addSystemEnvironment
Add system environment variables- Throws:
Exception
- if error
-
getEnvironmentVariables
- Returns:
- String[] Return the list of environment variables
- Throws:
CommandLineException
- if error
-
getCommandline
- Returns:
- Returns the executable and all defined arguments.
For Windows Family,
getShellCommandline()
is returned
-
getRawCommandline
Returns the executable and all defined arguments.- Returns:
- the command line as array not escaped neither quoted
-
getShellCommandline
Returns the shell, executable and all defined arguments. Shell usage is only desirable when generating code for remote execution.- Returns:
- the command line as array
-
getArguments
- Returns:
- Returns all arguments defined by
addLine
,addValue
or the argument object.
-
toString
-
size
public int size() -
clone
-
clear
public void clear()Clear out the whole command line. -
clearArgs
public void clearArgs()Clear out the arguments but leave the executable in place for another operation. -
createMarker
This marker can be used to locate a position on the commandline - to insert something for example - when all parameters have been set.
- Returns:
- Return a marker.
-
setWorkingDirectory
Sets execution directory.- Parameters:
path
- the working directory as String
-
setWorkingDirectory
Sets execution directory.- Parameters:
workingDirectory
- the File used as working directory
-
getWorkingDirectory
-
execute
Executes the command.- Returns:
- the Process
- Throws:
CommandLineException
- if error
-
getSystemEnvVars
- Throws:
Exception
-
setShell
Allows to set the shell to be used in this command line. Shell usage is only desirable when generating code for remote execution.- Parameters:
shell
- Shell to use- Since:
- 1.2
-
getShell
Get the shell to be used in this command line. Shell usage is only desirable when generating code for remote execution.- Returns:
- the Shell
- Since:
- 1.2
-
translateCommandline
Deprecated.UseCommandLineUtils.translateCommandline(String)
instead.- Parameters:
toProcess
- the process- Returns:
- the command line arguments
- Throws:
Exception
- if error happen
-
quoteArgument
Deprecated.UseCommandLineUtils.quote(String)
instead.- Parameters:
argument
- the argument- Returns:
- the quote arg
- Throws:
CommandLineException
- if error happen
-
toString
Deprecated.UseCommandLineUtils.toString(String[])
instead.- Parameters:
line
- the lines- Returns:
- lines as single String
-
setExecutable(String)
instead.