Class Commandline
- java.lang.Object
-
- org.codehaus.plexus.util.cli.Commandline
-
- All Implemented Interfaces:
java.lang.Cloneable
public class Commandline extends java.lang.Object implements java.lang.CloneableCommandline 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
someelementmust provide a methodcreateAcommandlinewhich returns an instance of this class.- Author:
- thomas.haas@softwired-inc.com, Stefan Bodewig
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classCommandline.ArgumentclassCommandline.MarkerClass to keep track of the position of an Argument.
-
Field Summary
Fields Modifier and Type Field Description protected java.util.Vector<Arg>argumentsprotected java.util.Map<java.lang.String,java.lang.String>envVarsprotected java.lang.StringexecutableDeprecated.UsesetExecutable(String)instead.protected static java.lang.StringOS_NAMEDeprecated.UseOsclass instead.protected static java.lang.StringWINDOWSDeprecated.UseOsclass instead.
-
Constructor Summary
Constructors Constructor Description Commandline()Create a new command line object.Commandline(java.lang.String toProcess)Create a new command line object, given a command following POSIX sh quoting rulesCommandline(java.lang.String toProcess, Shell shell)Create a new command line object.Commandline(Shell shell)Create a new command line object.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidaddArg(Arg argument)Adds an argument object to our list of args.voidaddArg(Arg argument, boolean insertAtStart)Adds an argument object to our list of args.voidaddArguments(java.lang.String[] line)voidaddEnvironment(java.lang.String name, java.lang.String value)Add an environment variablevoidaddSystemEnvironment()Add system environment variablesvoidclear()Clear out the whole command line.voidclearArgs()Clear out the arguments but leave the executable in place for another operation.java.lang.Objectclone()ArgcreateArg()Creates an argument object.ArgcreateArg(boolean insertAtStart)Creates an argument object and adds it to our list of args.Commandline.ArgumentcreateArgument()Deprecated.UsecreateArg()insteadCommandline.ArgumentcreateArgument(boolean insertAtStart)Deprecated.UsecreateArg(boolean)insteadCommandline.MarkercreateMarker()Return a marker.java.lang.Processexecute()Executes the command.java.lang.String[]getArguments()Returns all arguments defined byaddLine,addValueor the argument object.java.lang.String[]getCommandline()Returns the executable and all defined arguments.
For Windows Family,getShellCommandline()is returnedjava.lang.String[]getEnvironmentVariables()Return the list of environment variablesjava.lang.StringgetExecutable()Return an executable name, quoted for shell use.java.lang.StringgetLiteralExecutable()longgetPid()ShellgetShell()Get the shell to be used in this command line.java.lang.String[]getShellCommandline()Returns the shell, executable and all defined arguments.java.util.PropertiesgetSystemEnvVars()java.io.FilegetWorkingDirectory()static java.lang.StringquoteArgument(java.lang.String argument)Deprecated.UseCommandLineUtils.quote(String)instead.voidsetExecutable(java.lang.String executable)Sets the executable to run.voidsetPid(long pid)voidsetShell(Shell shell)Allows to set the shell to be used in this command line.voidsetWorkingDirectory(java.io.File workingDirectory)Sets execution directory.voidsetWorkingDirectory(java.lang.String path)Sets execution directory.intsize()java.lang.StringtoString()static java.lang.StringtoString(java.lang.String[] line)Deprecated.UseCommandLineUtils.toString(String[])instead.static java.lang.String[]translateCommandline(java.lang.String toProcess)Deprecated.UseCommandLineUtils.translateCommandline(String)instead.
-
-
-
Field Detail
-
OS_NAME
protected static final java.lang.String OS_NAME
Deprecated.UseOsclass instead.- See Also:
- Constant Field Values
-
WINDOWS
protected static final java.lang.String WINDOWS
Deprecated.UseOsclass instead.- See Also:
- Constant Field Values
-
arguments
protected java.util.Vector<Arg> arguments
-
envVars
protected java.util.Map<java.lang.String,java.lang.String> envVars
-
executable
protected java.lang.String executable
Deprecated.UsesetExecutable(String)instead.
-
-
Constructor Detail
-
Commandline
public Commandline(java.lang.String toProcess, Shell shell)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-
-
Commandline
public Commandline(Shell shell)
Create a new command line object. Shell is autodetected from operating system Shell usage is only desirable when generating code for remote execution.
-
Commandline
public Commandline(java.lang.String toProcess)
Create a new command line object, given a command following POSIX sh quoting rules- Parameters:
toProcess-
-
Commandline
public Commandline()
Create a new command line object.
-
-
Method Detail
-
getPid
public long getPid()
-
setPid
public void setPid(long pid)
-
createArgument
public Commandline.Argument 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(boolean)
-
createArgument
public Commandline.Argument createArgument(boolean insertAtStart)
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.
-
createArg
public Arg 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:
createArgument(boolean)
-
createArg
public Arg createArg(boolean insertAtStart)
Creates 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.
-
addArg
public void addArg(Arg argument)
Adds an argument object to our list of args.- See Also:
addArg(Arg,boolean)
-
addArg
public void addArg(Arg argument, boolean insertAtStart)
Adds an argument object to our list of args.- Parameters:
insertAtStart- if true, the argument is inserted at the beginning of the list of args, otherwise it is appended.
-
setExecutable
public void setExecutable(java.lang.String executable)
Sets the executable to run.
-
getLiteralExecutable
public java.lang.String getLiteralExecutable()
- Returns:
- Executable to be run, as a literal string (no shell quoting/munging)
-
getExecutable
public java.lang.String 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
public void addArguments(java.lang.String[] line)
-
addEnvironment
public void addEnvironment(java.lang.String name, java.lang.String value)Add an environment variable
-
addSystemEnvironment
public void addSystemEnvironment() throws java.lang.ExceptionAdd system environment variables- Throws:
java.lang.Exception
-
getEnvironmentVariables
public java.lang.String[] getEnvironmentVariables() throws CommandLineExceptionReturn the list of environment variables- Throws:
CommandLineException
-
getCommandline
public java.lang.String[] getCommandline()
Returns the executable and all defined arguments.
For Windows Family,getShellCommandline()is returned
-
getShellCommandline
public java.lang.String[] getShellCommandline()
Returns the shell, executable and all defined arguments. Shell usage is only desirable when generating code for remote execution.
-
getArguments
public java.lang.String[] getArguments()
Returns all arguments defined byaddLine,addValueor the argument object.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
size
public int size()
-
clone
public java.lang.Object clone()
- Overrides:
clonein classjava.lang.Object
-
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
public Commandline.Marker createMarker()
Return a marker.
This marker can be used to locate a position on the commandline - to insert something for example - when all parameters have been set.
-
setWorkingDirectory
public void setWorkingDirectory(java.lang.String path)
Sets execution directory.
-
setWorkingDirectory
public void setWorkingDirectory(java.io.File workingDirectory)
Sets execution directory.
-
getWorkingDirectory
public java.io.File getWorkingDirectory()
-
execute
public java.lang.Process execute() throws CommandLineExceptionExecutes the command.- Throws:
CommandLineException
-
getSystemEnvVars
public java.util.Properties getSystemEnvVars() throws java.lang.Exception- Throws:
java.lang.Exception
-
setShell
public void setShell(Shell shell)
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-- Since:
- 1.2
-
getShell
public Shell getShell()
Get the shell to be used in this command line. Shell usage is only desirable when generating code for remote execution.- Since:
- 1.2
-
translateCommandline
public static java.lang.String[] translateCommandline(java.lang.String toProcess) throws java.lang.ExceptionDeprecated.UseCommandLineUtils.translateCommandline(String)instead.- Throws:
java.lang.Exception
-
quoteArgument
public static java.lang.String quoteArgument(java.lang.String argument) throws CommandLineExceptionDeprecated.UseCommandLineUtils.quote(String)instead.- Throws:
CommandLineException
-
toString
public static java.lang.String toString(java.lang.String[] line)
Deprecated.UseCommandLineUtils.toString(String[])instead.
-
-