org.apache.commons.exec
Class CommandLine

java.lang.Object
  extended by org.apache.commons.exec.CommandLine

public class CommandLine
extends Object

CommandLine objects help handling command lines specifying processes to execute. The class can be used to a command line by an application.


Constructor Summary
CommandLine(File executable)
          Create a command line without any arguments.
CommandLine(String executable)
          Create a command line without any arguments.
 
Method Summary
 CommandLine addArgument(String argument)
          Add a single argument.
 CommandLine addArgument(String argument, boolean handleQuoting)
          Add a single argument.
 CommandLine addArguments(String arguments)
          Add multiple arguments.
 CommandLine addArguments(String[] arguments)
          Add multiple arguments.
 CommandLine addArguments(String[] arguments, boolean handleQuoting)
          Add multiple arguments.
 CommandLine addArguments(String arguments, boolean handleQuoting)
          Add multiple arguments.
 String[] getArguments()
          Returns the quoted arguments.
 String getExecutable()
          Returns the executable.
 Map getSubstitutionMap()
           
 boolean isFile()
           
static CommandLine parse(String line)
          Create a command line from a string.
static CommandLine parse(String line, Map substitutionMap)
          Create a command line from a string.
 void setSubstitutionMap(Map substitutionMap)
          Set the substitutionMap to expand variables in the command line.
 String toString()
          Stringify operator returns the command line as a string.
 String[] toStrings()
          Returns the command line as an array of strings.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

CommandLine

public CommandLine(String executable)
Create a command line without any arguments.

Parameters:
executable - the executable

CommandLine

public CommandLine(File executable)
Create a command line without any arguments.

Parameters:
executable - the executable file
Method Detail

parse

public static CommandLine parse(String line)
Create a command line from a string.

Parameters:
line - the first element becomes the executable, the rest the arguments
Returns:
the parsed command line
Throws:
IllegalArgumentException - If line is null or all whitespace

parse

public static CommandLine parse(String line,
                                Map substitutionMap)
Create a command line from a string.

Parameters:
line - the first element becomes the executable, the rest the arguments
substitutionMap - the name/value pairs used for substitution
Returns:
the parsed command line
Throws:
IllegalArgumentException - If line is null or all whitespace

getExecutable

public String getExecutable()
Returns the executable.

Returns:
The executable

isFile

public boolean isFile()
Returns:
Was a file being used to set the executable?

addArguments

public CommandLine addArguments(String[] arguments)
Add multiple arguments. Handles parsing of quotes and whitespace.

Parameters:
arguments - An array of arguments
Returns:
The command line itself

addArguments

public CommandLine addArguments(String[] arguments,
                                boolean handleQuoting)
Add multiple arguments.

Parameters:
arguments - An array of arguments
handleQuoting - Add the argument with/without handling quoting
Returns:
The command line itself

addArguments

public CommandLine addArguments(String arguments)
Add multiple arguments. Handles parsing of quotes and whitespace. Please note that the parsing can have undesired side-effects therefore it is recommended to build the command line incrementally.

Parameters:
arguments - An string containing multiple arguments.
Returns:
The command line itself

addArguments

public CommandLine addArguments(String arguments,
                                boolean handleQuoting)
Add multiple arguments. Handles parsing of quotes and whitespace. Please note that the parsing can have undesired side-effects therefore it is recommended to build the command line incrementally.

Parameters:
arguments - An string containing multiple arguments.
handleQuoting - Add the argument with/without handling quoting
Returns:
The command line itself

addArgument

public CommandLine addArgument(String argument)
Add a single argument. Handles quoting.

Parameters:
argument - The argument to add
Returns:
The command line itself
Throws:
IllegalArgumentException - If argument contains both single and double quotes

addArgument

public CommandLine addArgument(String argument,
                               boolean handleQuoting)
Add a single argument.

Parameters:
argument - The argument to add
handleQuoting - Add the argument with/without handling quoting
Returns:
The command line itself

getArguments

public String[] getArguments()
Returns the quoted arguments.

Returns:
The quoted arguments

getSubstitutionMap

public Map getSubstitutionMap()
Returns:
the substitution map

setSubstitutionMap

public void setSubstitutionMap(Map substitutionMap)
Set the substitutionMap to expand variables in the command line.

Parameters:
substitutionMap - the map

toStrings

public String[] toStrings()
Returns the command line as an array of strings.

Returns:
The command line as an string array

toString

public String toString()
Stringify operator returns the command line as a string. Parameters are correctly quoted when containing a space or left untouched if the are already quoted.

Overrides:
toString in class Object
Returns:
the command line as single string


Copyright © 2011 Apache Software Foundation. All Rights Reserved.