Package org.apache.commons.exec
Class CommandLine
- java.lang.Object
-
- org.apache.commons.exec.CommandLine
-
public class CommandLine extends java.lang.ObjectCommandLine objects help handling command lines specifying processes to execute. The class can be used to a command line by an application.- Version:
- $Id: CommandLine.java 1613094 2014-07-24 12:20:14Z ggregory $
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) classCommandLine.ArgumentEncapsulates a command line argument.
-
Field Summary
Fields Modifier and Type Field Description private java.util.Vector<CommandLine.Argument>argumentsThe arguments of the command.private java.lang.StringexecutableThe program to execute.private booleanisFileWas a file being used to set the executable?private java.util.Map<java.lang.String,?>substitutionMapA map of name value pairs used to expand command line arguments
-
Constructor Summary
Constructors Constructor Description CommandLine(java.io.File executable)Create a command line without any arguments.CommandLine(java.lang.String executable)Create a command line without any arguments.CommandLine(CommandLine other)Copy constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description CommandLineaddArgument(java.lang.String argument)Add a single argument.CommandLineaddArgument(java.lang.String argument, boolean handleQuoting)Add a single argument.CommandLineaddArguments(java.lang.String addArguments)Add multiple arguments.CommandLineaddArguments(java.lang.String[] addArguments)Add multiple arguments.CommandLineaddArguments(java.lang.String[] addArguments, boolean handleQuoting)Add multiple arguments.CommandLineaddArguments(java.lang.String addArguments, boolean handleQuoting)Add multiple arguments.private java.lang.StringexpandArgument(java.lang.String argument)Expand variables in a command line argument.java.lang.String[]getArguments()Returns the expanded and quoted command line arguments.java.lang.StringgetExecutable()Returns the executable.java.util.Map<java.lang.String,?>getSubstitutionMap()booleanisFile()Was a file being used to set the executable?static CommandLineparse(java.lang.String line)Create a command line from a string.static CommandLineparse(java.lang.String line, java.util.Map<java.lang.String,?> substitutionMap)Create a command line from a string.voidsetSubstitutionMap(java.util.Map<java.lang.String,?> substitutionMap)Set the substitutionMap to expand variables in the command line.private java.lang.StringtoCleanExecutable(java.lang.String dirtyExecutable)Cleans the executable string.java.lang.StringtoString()Stringify operator returns the command line as a string.java.lang.String[]toStrings()Returns the command line as an array of strings.private static java.lang.String[]translateCommandline(java.lang.String toProcess)Crack a command line.
-
-
-
Field Detail
-
arguments
private final java.util.Vector<CommandLine.Argument> arguments
The arguments of the command.
-
executable
private final java.lang.String executable
The program to execute.
-
substitutionMap
private java.util.Map<java.lang.String,?> substitutionMap
A map of name value pairs used to expand command line arguments
-
isFile
private final boolean isFile
Was a file being used to set the executable?
-
-
Constructor Detail
-
CommandLine
public CommandLine(java.lang.String executable)
Create a command line without any arguments.- Parameters:
executable- the executable
-
CommandLine
public CommandLine(java.io.File executable)
Create a command line without any arguments.- Parameters:
executable- the executable file
-
CommandLine
public CommandLine(CommandLine other)
Copy constructor.- Parameters:
other- the instance to copy
-
-
Method Detail
-
parse
public static CommandLine parse(java.lang.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:
java.lang.IllegalArgumentException- If line is null or all whitespace
-
parse
public static CommandLine parse(java.lang.String line, java.util.Map<java.lang.String,?> substitutionMap)
Create a command line from a string.- Parameters:
line- the first element becomes the executable, the rest the argumentssubstitutionMap- the name/value pairs used for substitution- Returns:
- the parsed command line
- Throws:
java.lang.IllegalArgumentException- If line is null or all whitespace
-
getExecutable
public java.lang.String getExecutable()
Returns the executable.- Returns:
- The executable
-
isFile
public boolean isFile()
Was a file being used to set the executable?- Returns:
- true if a file was used for setting the executable
-
addArguments
public CommandLine addArguments(java.lang.String[] addArguments)
Add multiple arguments. Handles parsing of quotes and whitespace.- Parameters:
addArguments- An array of arguments- Returns:
- The command line itself
-
addArguments
public CommandLine addArguments(java.lang.String[] addArguments, boolean handleQuoting)
Add multiple arguments.- Parameters:
addArguments- An array of argumentshandleQuoting- Add the argument with/without handling quoting- Returns:
- The command line itself
-
addArguments
public CommandLine addArguments(java.lang.String addArguments)
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:
addArguments- An string containing multiple arguments.- Returns:
- The command line itself
-
addArguments
public CommandLine addArguments(java.lang.String addArguments, 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:
addArguments- An string containing multiple arguments.handleQuoting- Add the argument with/without handling quoting- Returns:
- The command line itself
-
addArgument
public CommandLine addArgument(java.lang.String argument)
Add a single argument. Handles quoting.- Parameters:
argument- The argument to add- Returns:
- The command line itself
- Throws:
java.lang.IllegalArgumentException- If argument contains both single and double quotes
-
addArgument
public CommandLine addArgument(java.lang.String argument, boolean handleQuoting)
Add a single argument.- Parameters:
argument- The argument to addhandleQuoting- Add the argument with/without handling quoting- Returns:
- The command line itself
-
getArguments
public java.lang.String[] getArguments()
Returns the expanded and quoted command line arguments.- Returns:
- The quoted arguments
-
getSubstitutionMap
public java.util.Map<java.lang.String,?> getSubstitutionMap()
- Returns:
- the substitution map
-
setSubstitutionMap
public void setSubstitutionMap(java.util.Map<java.lang.String,?> substitutionMap)
Set the substitutionMap to expand variables in the command line.- Parameters:
substitutionMap- the map
-
toStrings
public java.lang.String[] toStrings()
Returns the command line as an array of strings.- Returns:
- The command line as an string array
-
toString
public java.lang.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:
toStringin classjava.lang.Object- Returns:
- the command line as single string
-
expandArgument
private java.lang.String expandArgument(java.lang.String argument)
Expand variables in a command line argument.- Parameters:
argument- the argument- Returns:
- the expanded string
-
translateCommandline
private static java.lang.String[] translateCommandline(java.lang.String toProcess)
Crack a command line.- Parameters:
toProcess- the command line to process- Returns:
- the command line broken into strings. An empty or null toProcess parameter results in a zero sized array
-
toCleanExecutable
private java.lang.String toCleanExecutable(java.lang.String dirtyExecutable)
Cleans the executable string. The argument is trimmed and '/' and '\\' are replaced with the platform specific file separator char- Parameters:
dirtyExecutable- the executable- Returns:
- the platform-specific executable string
-
-