org.codehaus.plexus.util.cli

Class Commandline

public class Commandline extends Object implements 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
static classCommandline.Argument
classCommandline.Marker
Class to keep track of the position of an Argument.
Field Summary
protected Vectorarguments
protected MapenvVars
protected Stringexecutable
protected static StringOS_NAME
longpid
Shellshell
FileworkingDir
protected static StringWINDOWS
Constructor Summary
Commandline(String toProcess, Shell shell)
Create a new command line object.
Commandline(Shell shell)
Create a new command line object.
Commandline(String toProcess)
Create a new command line object.
Commandline()
Create a new command line object.
Method Summary
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(String[] line)
voidaddEnvironment(String name, String value)
Add an environment variable
voidaddSystemEnvironment()
Add system environment variables
voidclear()
Clear out the whole command line.
voidclearArgs()
Clear out the arguments but leave the executable in place for another operation.
Objectclone()
ArgcreateArg()
Creates an argument object.
ArgcreateArg(boolean insertAtStart)
Creates an argument object and adds it to our list of args.
Commandline.ArgumentcreateArgument()
Creates an argument object.
Commandline.ArgumentcreateArgument(boolean insertAtStart)
Creates an argument object and adds it to our list of args.
Commandline.MarkercreateMarker()
Return a marker.
Processexecute()
Executes the command.
String[]getArguments()
Returns all arguments defined by addLine, addValue or the argument object.
String[]getCommandline()
Returns the executable and all defined arguments.
String[]getEnvironmentVariables()
Return the list of environment variables
StringgetExecutable()
longgetPid()
ShellgetShell()
Get the shell to be used in this command line.
String[]getShellCommandline()
Returns the shell, executable and all defined arguments.
PropertiesgetSystemEnvVars()
FilegetWorkingDirectory()
static StringquoteArgument(String argument)
voidsetDefaultShell()

Sets the shell or command-line interpretor for the detected operating system, and the shell arguments.

voidsetExecutable(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(String path)
Sets execution directory.
voidsetWorkingDirectory(File workingDirectory)
Sets execution directory.
intsize()
StringtoString()
static StringtoString(String[] line)
static String[]translateCommandline(String toProcess)
voidverifyShellState()

Field Detail

arguments

protected Vector arguments

envVars

protected Map envVars

executable

protected String executable

Deprecated: Use setExecutable instead.

OS_NAME

protected static final String OS_NAME

pid

private long pid

shell

private Shell shell

workingDir

private File workingDir

Deprecated: Use setWorkingDirectory or setWorkingDirectory instead.

WINDOWS

protected static final String WINDOWS

Constructor Detail

Commandline

public Commandline(String toProcess, Shell shell)
Create a new command line object. Shell is autodetected from operating system

Parameters: toProcess

Commandline

public Commandline(Shell shell)
Create a new command line object. Shell is autodetected from operating system

Commandline

public Commandline(String toProcess)
Create a new command line object. Shell is autodetected from operating system

Parameters: toProcess

Commandline

public Commandline()
Create a new command line object. Shell is autodetected from operating system

Method Detail

addArg

public void addArg(Arg argument)
Adds an argument object to our list of args.

Returns: the argument object.

See Also: Commandline

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.

addArguments

public void addArguments(String[] line)

addEnvironment

public void addEnvironment(String name, String value)
Add an environment variable

addSystemEnvironment

public void addSystemEnvironment()
Add system environment variables

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.

clone

public Object clone()

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: Commandline

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.

createArgument

public Commandline.Argument createArgument()

Deprecated: Use createArg instead

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: Commandline

createArgument

public Commandline.Argument createArgument(boolean insertAtStart)

Deprecated: Use Commandline instead

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.

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.

execute

public Process execute()
Executes the command.

getArguments

public String[] getArguments()
Returns all arguments defined by addLine, addValue or the argument object.

getCommandline

public String[] getCommandline()
Returns the executable and all defined arguments.

getEnvironmentVariables

public String[] getEnvironmentVariables()
Return the list of environment variables

getExecutable

public String getExecutable()

getPid

public long getPid()

getShell

public Shell getShell()
Get the shell to be used in this command line.

Since: 1.2

getShellCommandline

public String[] getShellCommandline()
Returns the shell, executable and all defined arguments.

getSystemEnvVars

public Properties getSystemEnvVars()

getWorkingDirectory

public File getWorkingDirectory()

quoteArgument

public static String quoteArgument(String argument)

Deprecated: Use quote instead.

setDefaultShell

private void setDefaultShell()

Sets the shell or command-line interpretor for the detected operating system, and the shell arguments.

setExecutable

public void setExecutable(String executable)
Sets the executable to run.

setPid

public void setPid(long pid)

setShell

public void setShell(Shell shell)
Allows to set the shell to be used in this command line.

Parameters: shell

Since: 1.2

setWorkingDirectory

public void setWorkingDirectory(String path)
Sets execution directory.

setWorkingDirectory

public void setWorkingDirectory(File workingDirectory)
Sets execution directory.

size

public int size()

toString

public String toString()

toString

public static String toString(String[] line)

Deprecated: Use (String[]) instead.

translateCommandline

public static String[] translateCommandline(String toProcess)

Deprecated: Use translateCommandline instead.

verifyShellState

private void verifyShellState()

Deprecated: Remove once backward compat with plexus-utils <= 1.4 is no longer a consideration