Package org.refcodes.console
Class ArgsParserImpl
- java.lang.Object
-
- org.refcodes.console.ArgsParserImpl
-
- All Implemented Interfaces:
org.refcodes.component.Resetable
,ArgsParser
,ArgsParserMixin<ArgsParser>
,RootConditionAccessor
,org.refcodes.mixin.DescriptionAccessor.DescriptionBuilder<ArgsParser>
,org.refcodes.mixin.DescriptionAccessor.DescriptionMutator
,org.refcodes.mixin.NameAccessor.NameBuilder<ArgsParser>
,org.refcodes.mixin.NameAccessor.NameMutator
,org.refcodes.mixin.TitleAccessor.TitleBuilder<ArgsParser>
,org.refcodes.mixin.TitleAccessor.TitleMutator
public class ArgsParserImpl extends java.lang.Object implements ArgsParser
A straightforward implementation of theArgsParser
interface. The constructor only provides means to set the required attributes as the attributes to be adjusted optionally are already sufficiently pre-configured. For adjusting them, a flavor of the Builder-Pattern is provided with which you can easily chain the configuration of this instance; as them methods return the instance of this class being configured. This helps to prevent the telescoping constructor anti-pattern.The
SyntaxNotation
is pre-set with theSyntaxNotation.REFCODES
notation.The console width id pre-configured with the console's width as determined by the
SystemUtility.getTerminalWidth()
.The standard out
PrintStream
is pre-configured with theSystem.out
PrintStream
.The newline characters to be used for line breaks is "\r\n" on Windows machines and "\"n" on all other machines as of the
SystemUtility.getLineBreak()
.- See Also:
- "http://en.wikipedia.org/wiki/Builder_pattern"
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.refcodes.console.RootConditionAccessor
RootConditionAccessor.RootConditionBuilder<B extends RootConditionAccessor.RootConditionBuilder<B>>, RootConditionAccessor.RootConditionMutator, RootConditionAccessor.RootConditionProperty
-
-
Constructor Summary
Constructors Constructor Description ArgsParserImpl()
Constructs theArgsParser
instance without any restrictions to the parsed arguments.ArgsParserImpl(Condition aRootCondition)
Constructs theArgsParser
instance with the given rootCondition
and the defaultSyntaxNotation.REFCODES
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
errorLn(java.lang.String aLine)
Prints the given line to standard error with regards to to the console width as specified by theArgsParser.withConsoleWidth(int)
method.java.util.List<? extends Operand<?>>
evalArgs(java.lang.String[] aArgs)
Evaluates the provided command line arguments and determines the according values by evaluating the rootCondition
.protected static java.util.List<? extends Operand<?>>
fromArgs(java.lang.String[] aArgs, char aDelimiter)
Heuristically loads the arguments without any syntax required, e.g.java.lang.String
getCopyrightNote()
Retrieves the copyright.protected char
getDelimiter()
Returns the delimiter to be used by colliding command line args when creating non colliding arg's aliases (keys for key/value-pairs).java.lang.String
getDescription()
Retrieves the description.java.lang.String
getLicenseNote()
Retrieves the license.Condition
getRootCondition()
The root condition is the starting point node of aSyntaxable
hierarchy to be traversed when determining the syntax for command line arguments or when evaluating the command line arguments.void
printBanner()
Prints the banner; the banner most probably is an ASCII art text block which's look depends strongly on the taste of the author implementing this interface.void
printCopyrightNote()
Prints the copyright note as specified by theArgsParser.withCopyrightNote(String)
method; with regards to to the console width as specified by theArgsParser.withConsoleWidth(int)
method.void
printDescription()
Prints the description as set by theArgsParser.withDescription(String)
method with regards to the console width as specified by theArgsParser.withConsoleWidth(int)
method.void
printLicenseNote()
Prints the license note as specified by theArgsParser.withLicenseNote(String)
method; with regards to to the console width as specified by theArgsParser.withConsoleWidth(int)
method.void
printLn()
Prints an empty line / a line break.void
printLn(java.lang.String aLine)
Prints the given line to standard out with regards to to the console width as specified by theArgsParser.withConsoleWidth(int)
method.void
printOptions()
Prints theOption
s (short- and the long-options), theFlag
es and theOperand
and their description with regards to the console width as specified by theArgsParser.withConsoleWidth(int)
method.void
printSeparatorLn()
Prints a separator line using the separator character as specified by theArgsParser.withSeparatorChar(char)
method; with regards to to the console width as specified by theArgsParser.withConsoleWidth(int)
method.void
printUsage()
Prints the syntax as retrieved by the rootCondition
with regards to theSyntaxNotation
set by theArgsParser.withSyntaxNotation(SyntaxNotation)
method; making use of the usage label as passed by theArgsParser.withUsageLabel(String)
method.void
reset()
void
setBannerFont(org.refcodes.textual.Font aBannerFont)
Sets the banner font.void
setBannerFontPalette(char[] aColorPalette)
Sets the banner font palette.void
setConsoleWidth(int aConsoleWidth)
Set the console with.void
setCopyrightNote(java.lang.String aCopyrightNote)
Set the copyright note used by theArgsParser.printHelp()
method when writing out the copyright claim and make other adjustments with the result (with regards to the Builder-Pattern).void
setDescription(java.lang.String aDescription)
void
setErrorOut(java.io.PrintStream aErrorOut)
Set the error outPrintStream
and make other adjustments with the result (with regards to the Builder-Pattern).void
setLicenseNote(java.lang.String aLicenseNote)
Set the license note used by theArgsParser.printHelp()
method when writing out the licensing conditions.void
setLineBreak(java.lang.String aLineBreak)
Set the console's line break.void
setMaxConsoleWidth(int aMaxConsoleWidth)
Set the maximum console width to use in case the console width is greater than the maximum you want.void
setName(java.lang.String aName)
void
setSeparatorChar(char aSeparatorChar)
Set the character to be used when printing a separator line with theArgsParser.printSeparatorLn()
method.void
setStandardOut(java.io.PrintStream aStandardOut)
Set the standard outPrintStream
and make other adjustments with the result (with regards to the Builder-Pattern).void
setSyntaxNotation(SyntaxNotation aSyntaxNotation)
Set theSyntaxNotation
.void
setTitle(java.lang.String aTitle)
void
setUsageLabel(java.lang.String aUsageLabel)
Set the usage label used by theArgsParser.printHelp()
method when writing out the syntax.-
Methods inherited from interface org.refcodes.console.ArgsParser
evalArgs, printBody, printHelp, withBannerFont, withBannerFontPalette, withConsoleWidth, withCopyrightNote, withDescription, withErrorOut, withEvalArgs, withEvalArgs, withLicenseNote, withLineBreak, withMaxConsoleWidth, withName, withParseArgs, withSeparatorChar, withStandardOut, withSyntaxNotation, withTitle, withUsageLabel
-
Methods inherited from interface org.refcodes.console.ArgsParserMixin
withParseArgs
-
-
-
-
Constructor Detail
-
ArgsParserImpl
public ArgsParserImpl(Condition aRootCondition)
Constructs theArgsParser
instance with the given rootCondition
and the defaultSyntaxNotation.REFCODES
. The constructor only provides means to set the required attributes as the attributes to be adjusted optionally are already sufficiently pre-configured. For adjusting them, a flavor of the Builder-Pattern is provided with which you can easily chain the configuration of this instance; as them methods return the instance of this class being configured.- Parameters:
aRootCondition
- The root condition being the node from which parsing the command line arguments starts.
-
ArgsParserImpl
public ArgsParserImpl()
Constructs theArgsParser
instance without any restrictions to the parsed arguments. The constructor only provides means to set the required attributes as the attributes to be adjusted optionally are already sufficiently pre-configured. For adjusting them, a flavor of the Builder-Pattern is provided with which you can easily chain the configuration of this instance; as them methods return the instance of this class being configured.
-
-
Method Detail
-
getCopyrightNote
public java.lang.String getCopyrightNote()
Retrieves the copyright.- Specified by:
getCopyrightNote
in interfaceArgsParser
- Returns:
- The copyright note.
-
getDescription
public java.lang.String getDescription()
Retrieves the description.- Specified by:
getDescription
in interfaceArgsParser
- Returns:
- The description.
-
getLicenseNote
public java.lang.String getLicenseNote()
Retrieves the license.- Specified by:
getLicenseNote
in interfaceArgsParser
- Returns:
- The license note.
-
setSyntaxNotation
public void setSyntaxNotation(SyntaxNotation aSyntaxNotation)
Set theSyntaxNotation
.- Specified by:
setSyntaxNotation
in interfaceArgsParser
- Parameters:
aSyntaxNotation
- TheSyntaxNotation
to set.
-
setStandardOut
public void setStandardOut(java.io.PrintStream aStandardOut)
Set the standard outPrintStream
and make other adjustments with the result (with regards to the Builder-Pattern).- Specified by:
setStandardOut
in interfaceArgsParser
- Parameters:
aStandardOut
- The standard outPrintStream
to set.
-
setErrorOut
public void setErrorOut(java.io.PrintStream aErrorOut)
Set the error outPrintStream
and make other adjustments with the result (with regards to the Builder-Pattern).- Specified by:
setErrorOut
in interfaceArgsParser
- Parameters:
aErrorOut
- The error outPrintStream
to set.
-
setConsoleWidth
public void setConsoleWidth(int aConsoleWidth)
Set the console with. A setting of "-1" makes the instance use theSystemUtility.getTerminalWidth()
value, i.e. the console width is set automatically to be the width of your terminal.- Specified by:
setConsoleWidth
in interfaceArgsParser
- Parameters:
aConsoleWidth
- The width to set or -1 to let the parser automatically determine the console width.
-
setLineBreak
public void setLineBreak(java.lang.String aLineBreak)
Set the console's line break. A setting of null makes the instance use theSystemUtility.getLineBreak()
value.- Specified by:
setLineBreak
in interfaceArgsParser
- Parameters:
aLineBreak
- the new line break
-
setDescription
public void setDescription(java.lang.String aDescription)
- Specified by:
setDescription
in interfaceorg.refcodes.mixin.DescriptionAccessor.DescriptionMutator
-
setName
public void setName(java.lang.String aName)
- Specified by:
setName
in interfaceorg.refcodes.mixin.NameAccessor.NameMutator
-
setLicenseNote
public void setLicenseNote(java.lang.String aLicenseNote)
Set the license note used by theArgsParser.printHelp()
method when writing out the licensing conditions.- Specified by:
setLicenseNote
in interfaceArgsParser
- Parameters:
aLicenseNote
- The license note printed out by theArgsParser.printHelp()
method.
-
setUsageLabel
public void setUsageLabel(java.lang.String aUsageLabel)
Set the usage label used by theArgsParser.printHelp()
method when writing out the syntax.- Specified by:
setUsageLabel
in interfaceArgsParser
- Parameters:
aUsageLabel
- The usage label printed out by theArgsParser.printHelp()
method.
-
setCopyrightNote
public void setCopyrightNote(java.lang.String aCopyrightNote)
Set the copyright note used by theArgsParser.printHelp()
method when writing out the copyright claim and make other adjustments with the result (with regards to the Builder-Pattern).- Specified by:
setCopyrightNote
in interfaceArgsParser
- Parameters:
aCopyrightNote
- The license note printed out by theArgsParser.printHelp()
method.
-
setSeparatorChar
public void setSeparatorChar(char aSeparatorChar)
Set the character to be used when printing a separator line with theArgsParser.printSeparatorLn()
method.- Specified by:
setSeparatorChar
in interfaceArgsParser
- Parameters:
aSeparatorChar
- The character used by theArgsParser.printSeparatorLn()
method when printing out the line of characters..
-
setBannerFont
public void setBannerFont(org.refcodes.textual.Font aBannerFont)
Sets the banner font.- Specified by:
setBannerFont
in interfaceArgsParser
- Parameters:
aBannerFont
- the new banner font
-
setBannerFontPalette
public void setBannerFontPalette(char[] aColorPalette)
Sets the banner font palette.- Specified by:
setBannerFontPalette
in interfaceArgsParser
- Parameters:
aColorPalette
- the new banner font palette
-
setTitle
public void setTitle(java.lang.String aTitle)
- Specified by:
setTitle
in interfaceorg.refcodes.mixin.TitleAccessor.TitleMutator
-
setMaxConsoleWidth
public void setMaxConsoleWidth(int aMaxConsoleWidth)
Set the maximum console width to use in case the console width is greater than the maximum you want. This is most useful when the console width is determined automatically to be the width of your terminal.- Specified by:
setMaxConsoleWidth
in interfaceArgsParser
- Parameters:
aMaxConsoleWidth
- the new max console width
-
printLicenseNote
public void printLicenseNote()
Prints the license note as specified by theArgsParser.withLicenseNote(String)
method; with regards to to the console width as specified by theArgsParser.withConsoleWidth(int)
method.- Specified by:
printLicenseNote
in interfaceArgsParser
-
printCopyrightNote
public void printCopyrightNote()
Prints the copyright note as specified by theArgsParser.withCopyrightNote(String)
method; with regards to to the console width as specified by theArgsParser.withConsoleWidth(int)
method.- Specified by:
printCopyrightNote
in interfaceArgsParser
-
evalArgs
public java.util.List<? extends Operand<?>> evalArgs(java.lang.String[] aArgs) throws UnknownArgsException, AmbiguousArgsException, SuperfluousArgsException, ParseArgsException
Evaluates the provided command line arguments and determines the according values by evaluating the rootCondition
. In case of parsing failure, an according exception is thrown. ATTENTION: This method tests(!) for superfluous command line arguments being passed; e.g. command line arguments not being evaluated by any of theSyntaxable
instance being traversed starting at the rootCondition
will be reported. Business logic therefore should invoke this root node'sArgsParser.evalArgs(String[])
method instead of aCondition
'sSyntaxable.parseArgs(String[])
method; as ignoring superfluous command line arguments will cause unexpected behavior from the point of view of the invoker.- Specified by:
evalArgs
in interfaceArgsParser
- Parameters:
aArgs
- The command line arguments to be evaluated.- Returns:
- The list of evaluated command line arguments being instances of
the
Operand
interfaces or its sub-types. - Throws:
UnknownArgsException
- Thrown in case not one command line argument matched regarding the provided args vs. the expected args.AmbiguousArgsException
- Thrown in case at least one command line argument is ambiguous regarding expected args vs. provided args.SuperfluousArgsException
- Thrown in case there were arguments found not being used (superfluous arguments).ParseArgsException
- Thrown in case the provided command line arguments do not respect the required syntax or cannot be converted to the required type
-
printUsage
public void printUsage()
Prints the syntax as retrieved by the rootCondition
with regards to theSyntaxNotation
set by theArgsParser.withSyntaxNotation(SyntaxNotation)
method; making use of the usage label as passed by theArgsParser.withUsageLabel(String)
method.- Specified by:
printUsage
in interfaceArgsParser
-
printDescription
public void printDescription()
Prints the description as set by theArgsParser.withDescription(String)
method with regards to the console width as specified by theArgsParser.withConsoleWidth(int)
method.- Specified by:
printDescription
in interfaceArgsParser
-
printLn
public void printLn(java.lang.String aLine)
Prints the given line to standard out with regards to to the console width as specified by theArgsParser.withConsoleWidth(int)
method.- Specified by:
printLn
in interfaceArgsParser
- Parameters:
aLine
- The line to be printed.
-
errorLn
public void errorLn(java.lang.String aLine)
Prints the given line to standard error with regards to to the console width as specified by theArgsParser.withConsoleWidth(int)
method.- Specified by:
errorLn
in interfaceArgsParser
- Parameters:
aLine
- The line to be printed.
-
printLn
public void printLn()
Prints an empty line / a line break.- Specified by:
printLn
in interfaceArgsParser
-
printSeparatorLn
public void printSeparatorLn()
Prints a separator line using the separator character as specified by theArgsParser.withSeparatorChar(char)
method; with regards to to the console width as specified by theArgsParser.withConsoleWidth(int)
method.- Specified by:
printSeparatorLn
in interfaceArgsParser
-
printBanner
public void printBanner()
Prints the banner; the banner most probably is an ASCII art text block which's look depends strongly on the taste of the author implementing this interface.- Specified by:
printBanner
in interfaceArgsParser
-
printOptions
public void printOptions()
Prints theOption
s (short- and the long-options), theFlag
es and theOperand
and their description with regards to the console width as specified by theArgsParser.withConsoleWidth(int)
method.- Specified by:
printOptions
in interfaceArgsParser
-
getRootCondition
public Condition getRootCondition()
The root condition is the starting point node of aSyntaxable
hierarchy to be traversed when determining the syntax for command line arguments or when evaluating the command line arguments. Retrieves the rootCondition from the rootCondition property.- Specified by:
getRootCondition
in interfaceArgsParser
- Specified by:
getRootCondition
in interfaceRootConditionAccessor
- Returns:
- The root
Condition
in which's syntax this parser is based.
-
reset
public void reset()
- Specified by:
reset
in interfaceorg.refcodes.component.Resetable
-
getDelimiter
protected char getDelimiter()
Returns the delimiter to be used by colliding command line args when creating non colliding arg's aliases (keys for key/value-pairs).- Returns:
- The according delimiter.
-
fromArgs
protected static java.util.List<? extends Operand<?>> fromArgs(java.lang.String[] aArgs, char aDelimiter)
Heuristically loads the arguments without any syntax required, e.g. without any rootCondition
to be set.- Parameters:
aArgs
- The arguments to be loaded.aDelimiter
- The delimiter to resolve name clashes.- Returns:
- A list of heuristically determined
Flag
andStringOperand
instances.
-
-