public abstract class CommandLineParser
extends java.lang.Object
java de.esprit.demo.SampleTool -outfile c:/Temp/out.txt -verbose c:/Temp/in.txtIn this example '-outfile' is a option with an associated argument 'c:/Temp/out.txt' whereas '-verbose' is a boolean option with no associated argument and 'c:/Temp/in.txt' is a pure argument which would be returned by
getArguments()
.String[] restArgs = getArguments(); // returns both file args as a String array String outFile = clp.getArgument(0); // returns the first argument 'c:/Temp/out.txt' boolean isVerbose = clp.isVerbose(); // in this case returns true (no associated argument).Note: Before asking for arguments you must have called
parse()
in order to
read all options from the command line.
The following standard command line options are checked in any case:
Modifier and Type | Field and Description |
---|---|
protected Version |
espritVersion |
protected CommandLineOption |
optDebug |
protected CommandLineOption |
optDemo |
protected CommandLineOption |
optHelp |
protected CommandLineOption |
optLocale |
protected OptionLogDir |
optLogDir |
protected CommandLineOption |
optLogToFile |
protected CommandLineOption |
optNoConsole |
protected CommandLineOption |
optTest |
protected CommandLineOption |
optVerbose |
protected CommandLineOption |
optVersion |
Constructor and Description |
---|
CommandLineParser(java.lang.Class mainClass,
java.lang.String... args) |
Modifier and Type | Method and Description |
---|---|
<O extends CommandLineOption> |
addOption(CommandLineOption option)
Adds a new option always to the top of already existing options.
|
<O extends CommandLineOption> |
addOption(int positionIndex,
CommandLineOption option) |
protected Version |
addSoftwareVersion(Version version) |
protected void |
checkDependencies()
Is called after all options have been parsed.
|
java.lang.String |
createHelpText(boolean isPrintAll) |
java.lang.String |
createHelpText(java.lang.Exception e)
Creates the exception trace message as well as the help-text
|
protected java.lang.String |
createOptionList() |
protected java.lang.String |
createSyntaxLine() |
protected void |
ensureParsed() |
<T extends CommandLineOption> |
fetchDefinedOptionFor(java.lang.String arg) |
<T extends CommandLineOption> |
findDefinedOptionFor(java.lang.String arg) |
java.lang.String |
getArgument(int index) |
int |
getArgumentCount() |
java.lang.String[] |
getArguments() |
Version |
getCustomVersion()
Returns the last version added, which is the version of the customers application.
|
Version |
getEspritVersion()
Returns either the current
Version of the esprit software. |
java.io.File |
getLogDir() |
protected java.lang.Class |
getMainClass() |
java.util.List<Version> |
getSoftwareVersions()
Returns the software versions this application builds on.
|
LogLevel |
getStartLogLevel()
Evaluates the -debug or -verbose option and returns the according
LogLevel constant. |
protected void |
handleParsingError(java.lang.Exception error) |
boolean |
hasArguments() |
boolean |
isDebug() |
boolean |
isDemoMode() |
boolean |
isExitAfterParsing() |
boolean |
isLogToFile() |
boolean |
isNoConsoleLog() |
protected boolean |
isParsed()
Tells whether
parse() yet has been called. |
boolean |
isTestMode() |
boolean |
isVerbose() |
void |
parse()
Parses the command line and fill all options with values.
|
void |
removeOption(CommandLineOption option)
Removes the given option.
|
void |
removeOption(java.lang.String option)
Removes the given option by name.
|
protected Version espritVersion
protected final CommandLineOption optVersion
protected final CommandLineOption optHelp
protected final CommandLineOption optDebug
protected final CommandLineOption optVerbose
protected final CommandLineOption optTest
protected final CommandLineOption optDemo
protected final CommandLineOption optNoConsole
protected final CommandLineOption optLogToFile
protected final CommandLineOption optLocale
protected final OptionLogDir optLogDir
public CommandLineParser(java.lang.Class mainClass, java.lang.String... args)
protected final java.lang.Class getMainClass()
public final Version getEspritVersion()
Version
of the esprit software.public final Version getCustomVersion()
public final java.util.List<Version> getSoftwareVersions()
public final <O extends CommandLineOption> O addOption(CommandLineOption option)
public final <O extends CommandLineOption> O addOption(int positionIndex, CommandLineOption option)
public final void removeOption(java.lang.String option)
public final void removeOption(CommandLineOption option)
public final java.lang.String[] getArguments()
public final java.lang.String getArgument(int index)
public final boolean hasArguments()
public final int getArgumentCount()
public java.lang.String createHelpText(boolean isPrintAll)
protected java.lang.String createSyntaxLine()
protected java.lang.String createOptionList()
public void parse()
protected void handleParsingError(java.lang.Exception error) throws CommandLineParsingException
CommandLineParsingException
public boolean isExitAfterParsing()
protected final void ensureParsed()
protected final boolean isParsed()
parse()
yet has been called.
Note that it needs to be called before accessing any options.public java.lang.String createHelpText(java.lang.Exception e)
protected void checkDependencies() throws java.lang.Exception
java.lang.Exception
public final <T extends CommandLineOption> T fetchDefinedOptionFor(java.lang.String arg)
public final <T extends CommandLineOption> T findDefinedOptionFor(java.lang.String arg)
public final java.io.File getLogDir()
public final boolean isLogToFile()
public final boolean isTestMode()
public final boolean isDemoMode()
public final boolean isNoConsoleLog()
public final boolean isDebug()
public final boolean isVerbose()
Copyright © 2014 EsprIT-Systems. All Rights Reserved.