Package org.apache.commons.exec
Class CommandLine
java.lang.Object
org.apache.commons.exec.CommandLine
CommandLine objects help handling command lines specifying processes to execute. The class can be used to a command line by an application.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static final class
Encapsulates a command line argument. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Vector
<CommandLine.Argument> The arguments of the command.private final String
The program to execute.private final boolean
Tests whether a file was used to set the executable.A map of name value pairs used to expand command line arguments. -
Constructor Summary
ConstructorsConstructorDescriptionCommandLine
(File executable) Constructs a command line without any arguments.CommandLine
(String executable) Constructs a command line without any arguments.CommandLine
(Path executable) Constructs a command line without any arguments.CommandLine
(CommandLine other) Copy constructor. -
Method Summary
Modifier and TypeMethodDescriptionaddArgument
(String argument) Add a single argument.addArgument
(String argument, boolean handleQuoting) Add a single argument.addArguments
(String addArguments) Add multiple arguments.addArguments
(String[] addArguments) Add multiple arguments.addArguments
(String[] addArguments, boolean handleQuoting) Add multiple arguments.addArguments
(String addArguments, boolean handleQuoting) Add multiple arguments.private String
expandArgument
(String argument) Expand variables in a command line argument.String[]
Gets the expanded and quoted command line arguments.Gets the executable.Gets the substitution map.boolean
isFile()
Tests whether a file was used to set the executable.static CommandLine
Create a command line from a string.static CommandLine
Create a command line from a string.void
setSubstitutionMap
(Map<String, ?> substitutionMap) Sets the substitutionMap to expand variables in the command line.private String
toCleanExecutable
(String dirtyExecutable) Cleans the executable string.toString()
Stringify operator returns the command line as a string.String[]
Converts the command line as an array of strings.private static String[]
translateCommandline
(String toProcess) Crack a command line.
-
Field Details
-
arguments
The arguments of the command. -
executable
The program to execute. -
substitutionMap
A map of name value pairs used to expand command line arguments. -
isFile
private final boolean isFileTests whether a file was used to set the executable.
-
-
Constructor Details
-
CommandLine
Copy constructor.- Parameters:
other
- the instance to copy.
-
CommandLine
Constructs a command line without any arguments.- Parameters:
executable
- the executable file.
-
CommandLine
Constructs a command line without any arguments.- Parameters:
executable
- the executable file.- Since:
- 1.5.0
-
CommandLine
Constructs a command line without any arguments.- Parameters:
executable
- the executable.- Throws:
NullPointerException
- on null input.IllegalArgumentException
- on empty input.
-
-
Method Details
-
parse
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
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.
-
translateCommandline
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.
-
addArgument
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
Add a single argument.- Parameters:
argument
- The argument to add.handleQuoting
- Add the argument with/without handling quoting.- Returns:
- The command line itself.
-
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
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.
-
addArguments
Add multiple arguments. Handles parsing of quotes and whitespace.- Parameters:
addArguments
- An array of arguments.- Returns:
- The command line itself.
-
addArguments
Add multiple arguments.- Parameters:
addArguments
- An array of arguments.handleQuoting
- Add the argument with/without handling quoting.- Returns:
- The command line itself.
-
expandArgument
Expand variables in a command line argument.- Parameters:
argument
- the argument.- Returns:
- the expanded string.
-
getArguments
Gets the expanded and quoted command line arguments.- Returns:
- The quoted arguments.
-
getExecutable
Gets the executable.- Returns:
- The executable.
-
getSubstitutionMap
Gets the substitution map.- Returns:
- the substitution map.
-
isFile
public boolean isFile()Tests whether a file was used to set the executable.- Returns:
- true whether a file was used for setting the executable.
-
setSubstitutionMap
Sets the substitutionMap to expand variables in the command line.- Parameters:
substitutionMap
- the map
-
toCleanExecutable
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.
- Throws:
NullPointerException
- on null input.IllegalArgumentException
- on empty input.
-
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. -
toStrings
Converts the command line as an array of strings.- Returns:
- The command line as an string array.
-