org.codehaus.plexus.util.cli
Class Commandline

java.lang.Object
  extended byorg.codehaus.plexus.util.cli.Commandline
All Implemented Interfaces:
java.lang.Cloneable

public class Commandline
extends java.lang.Object
implements java.lang.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:
[email protected], Stefan Bodewig

Nested Class Summary
static class Commandline.Argument
          Used for nested xml command line definitions.
 class Commandline.Marker
          Class to keep track of the position of an Argument.
 
Field Summary
protected  java.util.Vector arguments
           
protected  java.util.Vector envVars
           
protected  java.lang.String executable
           
protected static java.lang.String OS_NAME
           
protected static java.lang.String WINDOWS
           
 
Constructor Summary
Commandline()
          Create a new command line object.
Commandline(java.lang.String toProcess)
          Create a new command line object.
 
Method Summary
 void addArguments(java.lang.String[] line)
           
 void addEnvironment(java.lang.String name, java.lang.String value)
          Add an environment variable
 void addSystemEnvironment()
          Add system environment variables
 void clear()
          Clear out the whole command line.
 void clearArgs()
          Clear out the arguments but leave the executable in place for another operation.
 java.lang.Object clone()
           
 Commandline.Argument createArgument()
          Creates an argument object.
 Commandline.Argument createArgument(boolean insertAtStart)
          Creates an argument object and adds it to our list of args.
 Commandline.Marker createMarker()
          Return a marker.
 java.lang.Process execute()
          Executes the command.
 java.lang.String[] getArguments()
          Returns all arguments defined by addLine, addValue or the argument object.
 java.lang.String[] getCommandline()
          Returns the executable and all defined arguments.
 java.lang.String[] getEnvironmentVariables()
          Return the list of environment variables
 java.lang.String getExecutable()
           
 long getPid()
           
 Shell getShell()
          Get the shell to be used in this command line.
 java.lang.String[] getShellCommandline()
          Returns the shell, executable and all defined arguments.
 java.util.Properties getSystemEnvVars()
           
 java.io.File getWorkingDirectory()
           
static java.lang.String quoteArgument(java.lang.String argument)
          Put quotes around the given String if necessary.
 void setExecutable(java.lang.String executable)
          Sets the executable to run.
 void setPid(long pid)
           
 void setShell(Shell shell)
          Allows to set the shell to be used in this command line.
 void setWorkingDirectory(java.lang.String path)
          Sets execution directory.
 int size()
           
 java.lang.String toString()
           
static java.lang.String toString(java.lang.String[] line)
           
static java.lang.String[] translateCommandline(java.lang.String toProcess)
           
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

OS_NAME

protected static final java.lang.String OS_NAME
See Also:
Constant Field Values

WINDOWS

protected static final java.lang.String WINDOWS
See Also:
Constant Field Values

executable

protected java.lang.String executable

arguments

protected java.util.Vector arguments

envVars

protected java.util.Vector envVars
Constructor Detail

Commandline

public Commandline(java.lang.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

getPid

public long getPid()

setPid

public void setPid(long pid)

createArgument

public Commandline.Argument createArgument()
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)

createArgument

public Commandline.Argument createArgument(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.

setExecutable

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


getExecutable

public java.lang.String getExecutable()

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.Exception
Add system environment variables

Throws:
java.lang.Exception

getEnvironmentVariables

public java.lang.String[] getEnvironmentVariables()
                                           throws CommandLineException
Return the list of environment variables

Throws:
CommandLineException

getCommandline

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


getShellCommandline

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


getArguments

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


toString

public java.lang.String toString()

quoteArgument

public static java.lang.String quoteArgument(java.lang.String argument)
                                      throws CommandLineException

Put quotes around the given String if necessary.

If the argument doesn't include spaces or quotes, return it as is. If it contains double quotes, use single quotes - else surround the argument by double quotes.

Throws:
CommandLineException - if the argument contains both, single and double quotes.

toString

public static java.lang.String toString(java.lang.String[] line)

translateCommandline

public static java.lang.String[] translateCommandline(java.lang.String toProcess)
                                               throws java.lang.Exception
Throws:
java.lang.Exception

size

public int size()

clone

public java.lang.Object 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

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.


getWorkingDirectory

public java.io.File getWorkingDirectory()

execute

public java.lang.Process execute()
                          throws CommandLineException
Executes 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.

Parameters:
shell -
Since:
1.2

getShell

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

Since:
1.2


Copyright © 2001-2006 Codehaus. All Rights Reserved.