public class CommandLineBuilder extends java.lang.Object
Typical usage:
builder = new CommandLineBuilder();
builder.addArg("--someoption");
builder.addArg("optionValue");
...
builder.saveArgs();
doSomething(builder.getCommandLineFile());
builder.dispose();
It will save options in builder.getCommandLineFile(). Options
will be stored one in a line. To retrieve options from file helper method can
be used (see documentation):
String[] args = CommandLineBuilder.preprocessCommandLineArguments(args);
NOTICE: No protection against line separators in arguments, should be OK for Cobertura needs.
NOTICE: This class depends on local machine settings (line separator, default encoding). If arguments are saved on different machine than they are loaded, results are unspecified. No problem in Cobertura.
| Modifier and Type | Field and Description |
|---|---|
private java.io.File |
commandLineFile |
private java.io.FileWriter |
commandLineWriter |
private static java.lang.String |
LINESEP |
private static org.apache.log4j.Logger |
logger |
| Constructor and Description |
|---|
CommandLineBuilder()
Creates a new instance of the builder.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addArg(java.lang.String arg)
Adds command line argument.
|
void |
addArg(java.lang.String arg1,
java.lang.String arg2)
Adds two command line arguments.
|
void |
dispose()
Explicity frees all resources associated with this instance.
|
java.lang.String |
getCommandLineFile()
Gets absolute path to the file with saved arguments.
|
static java.lang.String[] |
preprocessCommandLineArguments(java.lang.String[] args)
Loads arguments from file if
--commandsfile option is used. |
void |
saveArgs()
Saves options and made file available to use.
|
private static final org.apache.log4j.Logger logger
private static final java.lang.String LINESEP
private java.io.File commandLineFile
private java.io.FileWriter commandLineWriter
public CommandLineBuilder() throws java.io.IOException
java.io.IOException - if problems with creating temporary file for storing command
line occurpublic void addArg(java.lang.String arg) throws java.io.IOException
arg - command line argument to savejava.io.IOException - if problems with temporary file occurjava.lang.NullPointerException - if arg is nullpublic void addArg(java.lang.String arg1, java.lang.String arg2) throws java.io.IOException
addArg(String) two times.arg1 - first command line argument to savearg2 - second command line argument to savejava.io.IOException - if problems with temporary file occurjava.lang.NullPointerException - if any arg is nullpublic void saveArgs() throws java.io.IOException
getCommandLineFile() to get the file the arguments are saved in.java.io.IOException - if problems with temporary file occurpublic java.lang.String getCommandLineFile()
saveArgs() method.public void dispose()
public static java.lang.String[] preprocessCommandLineArguments(java.lang.String[] args) throws java.io.IOException
--commandsfile option is used. Checks
if passed array contains --commandsfile String, and if
so arguments from file specified in the very next array cell are read. If
there are more then one --commandsfile the result is unspecified.args if commandsfile option was not specified
or the file cannot be read.java.lang.NullPointerException - if args is null, or any argument is nulljava.lang.IllegalArgumentException - if --commandsfile is specified as last optionjava.io.IOException - if I/O related error with temporary command line file occur