Interface Term
- All Superinterfaces:
org.refcodes.mixin.DescriptionAccessor,org.refcodes.exception.ExceptionAccessor<ArgsSyntaxException>,MatchCountAccessor,org.refcodes.mixin.Resetable,org.refcodes.schema.Schemable,Synopsisable,org.refcodes.graphical.VisibleAccessor,org.refcodes.graphical.VisibleAccessor.VisibleBuilder<Term>,org.refcodes.graphical.VisibleAccessor.VisibleMutator,org.refcodes.graphical.VisibleAccessor.VisibleProperty
- All Known Implementing Classes:
AbstractCondition,AbstractDirective,AbstractOperand,AbstractOption,AbstractProperty,AllCondition,AndCondition,AnyCondition,ArrayDirective,ArrayOperand,ArrayOption,BooleanDirective,BooleanOption,BooleanProperty,CasesCondition,CharDirective,CharOption,CharProperty,CleanFlag,ConfigOption,ConfigProperty,DaemonFlag,DebugFlag,DoubleDirective,DoubleOption,DoubleProperty,EnumDirective,EnumOption,EnumProperty,FileDirective,FileOption,FileProperty,Flag,FloatDirective,FloatOption,FloatProperty,ForceFlag,HelpFlag,InitFlag,IntDirective,IntOption,IntProperty,LongDirective,LongOption,LongProperty,NoneOperand,Operation,OrCondition,QuietFlag,StringDirective,StringOperand,StringOption,StringProperty,SysInfoFlag,VerboseFlag,XorCondition
Term represents a node within a command line argument syntax tree.
The syntax tree is used for two closely related purposes:
- Parsing command line arguments and extracting evaluated values
- Generating a human readable representation of the command line syntax
Concrete Term implementations such as Operand,
Option, Flag or Condition define the structure and
semantics of the command line interface. By composing these terms, complex
syntaxes including optional branches and conditional argument groups can be
expressed.
A Term may either be a leaf node that directly evaluates arguments or
an inner node that delegates parsing and syntax generation to its child
Term instances.
Parsing is performed by traversing the syntax tree and matching command line
arguments against the defined terms. Successfully evaluated arguments are
returned as Operand instances or subclasses thereof. Parsing errors
are reported via ArgsSyntaxException.
A Term also supports:
- Resetting internal state between parsing runs
- Collecting match statistics for ambiguity resolution
- Producing schema representations for documentation or tooling
- Controlling visibility for help and usage output
The root Term typically exposes higher level entry points such as
ArgsParser.evalArgs(String[]) which build upon the methods defined by
this interface.
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.refcodes.mixin.DescriptionAccessor
org.refcodes.mixin.DescriptionAccessor.DescriptionBuilder<B extends org.refcodes.mixin.DescriptionAccessor.DescriptionBuilder<B>>, org.refcodes.mixin.DescriptionAccessor.DescriptionMutator, org.refcodes.mixin.DescriptionAccessor.DescriptionPropertyNested classes/interfaces inherited from interface org.refcodes.exception.ExceptionAccessor
org.refcodes.exception.ExceptionAccessor.ExceptionBuilder<EXC extends Throwable,B extends org.refcodes.exception.ExceptionAccessor.ExceptionBuilder<EXC, B>>, org.refcodes.exception.ExceptionAccessor.ExceptionMutator<EXC extends Throwable>, org.refcodes.exception.ExceptionAccessor.ExceptionProperty<EXC extends Throwable> Nested classes/interfaces inherited from interface org.refcodes.cli.MatchCountAccessor
MatchCountAccessor.MatchCountBuilder<B extends MatchCountAccessor.MatchCountBuilder<B>>, MatchCountAccessor.MatchCountMutator, MatchCountAccessor.MatchCountPropertyNested classes/interfaces inherited from interface org.refcodes.graphical.VisibleAccessor
org.refcodes.graphical.VisibleAccessor.VisibleBuilder<B extends org.refcodes.graphical.VisibleAccessor.VisibleBuilder<B>>, org.refcodes.graphical.VisibleAccessor.VisibleMutator, org.refcodes.graphical.VisibleAccessor.VisibleProperty -
Method Summary
Modifier and TypeMethodDescriptiondefault Operand<?>[]argsParser(String[] aArgs) Parses the given command line arguments using thisTermas the root of the syntax tree.Retrieves theArgsSyntaxExceptionexception in case invokingparseArgs(String[], CliContext)failed.intDetermines the number of args being matched by theparseArgs(String[], CliContext)(and similar) method(s) of the according syntax.booleanOperand<?>[]parseArgs(String[] aArgs, String[] aKeywords, CliContext aCliCtx) Parses the given command line arguments using the provided keyword set and CLI context.default Operand<?>[]parseArgs(String[] aArgs, CliContext aCliCtx) Parses the given command line arguments using the provided CLI context deriving its keyword.voidsetVisible(boolean isVisible) toSchema()default StringtoSynopsis(CliContext aCliCtx) Creates the synopsis for the syntax hierarchy.default StringtoSyntax()Generates a human readable representation of thisTerm's syntax.default StringCreates a syntax including its arguments.toSyntax(CliContext aCliCtx) Creates a syntax including its arguments.default StringtoSyntax(SyntaxNotation aSyntaxNotation) Creates a syntax including its arguments.default StringtoSyntax(SyntaxNotation aSyntaxNotation, String aArgumentEscapeCode, String aKeywordEscapeCode, String aResetEscapeCode) Creates a syntax including its arguments.<V> VRetrieves the value associated with the given alias after parsing.Methods inherited from interface org.refcodes.mixin.DescriptionAccessor
getDescriptionMethods inherited from interface org.refcodes.mixin.Resetable
resetMethods inherited from interface org.refcodes.schema.Schemable
toSchemaMethods inherited from interface org.refcodes.cli.Synopsisable
toSynopsis, toSynopsis, toSynopsis, toSynopsisMethods inherited from interface org.refcodes.graphical.VisibleAccessor.VisibleBuilder
withHide, withShow, withVisibleMethods inherited from interface org.refcodes.graphical.VisibleAccessor.VisibleMutator
hide, showMethods inherited from interface org.refcodes.graphical.VisibleAccessor.VisibleProperty
letVisible
-
Method Details
-
argsParser
Parses the given command line arguments using thisTermas the root of the syntax tree.This is a convenience method that creates a default
CliContextand delegates toparseArgs(String[], CliContext).The method evaluates this
Termand, if applicable, recursively traverses its child terms. Successfully evaluated arguments are returned asOperandinstances.This method does not detect superfluous arguments. Callers that require strict validation should use a root level method such as
ArgsParser.evalArgs(String[]).- Parameters:
aArgs- The command line arguments to parse- Returns:
- The evaluated operands
- Throws:
ArgsSyntaxException- If the arguments do not match the syntax
-
parseArgs
Parses the given command line arguments using the provided CLI context deriving its keyword.This is a convenience method that creates a default
CliContext, derives its keywords and delegates toparseArgs(String[], String[], CliContext).The
aKeywordsparameter defines reserved tokens such as option names that must not be interpreted as operand values.This method does not validate whether all arguments have been consumed. Superfluous arguments must be handled by the caller.
- Parameters:
aArgs- The command line arguments to parseaCliCtx- the cli ctx- Returns:
- The evaluated operands
- Throws:
ArgsSyntaxException- If the arguments do not match the syntax
-
parseArgs
Operand<?>[] parseArgs(String[] aArgs, String[] aKeywords, CliContext aCliCtx) throws ArgsSyntaxException Parses the given command line arguments using the provided keyword set and CLI context.This method represents the core parsing operation. It evaluates this
Termor, if this term is an inner node, delegates parsing to its child terms.The
aKeywordsparameter defines reserved tokens such as option names that must not be interpreted as operand values.This method does not validate whether all arguments have been consumed. Superfluous arguments must be handled by the caller.
- Parameters:
aArgs- The command line arguments to parseaKeywords- Reserved option and keyword tokensaCliCtx- TheCliContextcontrolling parsing and rendering- Returns:
- The evaluated operands
- Throws:
ArgsSyntaxException- If the arguments do not match the syntax
-
toSchema
CliSchema toSchema()- Specified by:
toSchemain interfaceorg.refcodes.schema.Schemable
-
toSynopsis
Creates the synopsis for the syntax hierarchy.The difference between a synopsis and a syntax is the most outer parenthesis settings for some notations: The
toSyntax(CliContext)method is called from inside a syntax tree whereas theSynopsisable.toSynopsis(CliContext)method is called from the hierarchy's root, as for some notations require the creation of the most outer braces.- Specified by:
toSynopsisin interfaceSynopsisable- Parameters:
aCliCtx- TheCliContextfor which the syntax is being generated.- Returns:
- The human readable (verbose) command line arguments synopsis.
-
toSyntax
Generates a human readable representation of thisTerm's syntax.This method is intended to be called from within the
Termhierarchy. When generating syntax from the root term, useSynopsisable.toSynopsis(SyntaxNotation)instead.The returned syntax includes this term and, if applicable, the syntax of its child terms according to the active
SyntaxNotation.- Returns:
- The human readable command line syntax
-
toSyntax
default String toSyntax(String aArgumentEscapeCode, String aKeywordEscapeCode, String aResetEscapeCode) Creates a syntax including its arguments.A specification differs from a syntax in that the specification provides the full definition not omitting any choices whereas the syntax only provides a syntactically correct variant. For example, having an option with a short option and a long option, the specification will show the short option and the long option whereas the syntax will only show either the short option or, if no short option is set, the long option.
Note: This method uses the
SyntaxNotation.LOGICALbe default!- Parameters:
aArgumentEscapeCode- The escape code to use when rendering (option's) argumentsaKeywordEscapeCode- The escape code to be used when rendering keywords (options.)aResetEscapeCode- The escape code resetting any previously applied escape codes.- Returns:
- The human readable command line syntax
-
toSyntax
Creates a syntax including its arguments.A specification differs from a syntax in that the specification provides the full definition not omitting any choices whereas the syntax only provides a syntactically correct variant. For example, having an option with a short option and a long option, the specification will show the short option and the long option whereas the syntax will only show either the short option or, if no short option is set, the long option.
Note: This method uses the default escape codes for syntax highlighting!
- Parameters:
aSyntaxNotation- The syntax notation used for generating the command line arguments syntax.- Returns:
- The human readable command line syntax
-
toSyntax
default String toSyntax(SyntaxNotation aSyntaxNotation, String aArgumentEscapeCode, String aKeywordEscapeCode, String aResetEscapeCode) Creates a syntax including its arguments.A specification differs from a syntax in that the specification provides the full definition not omitting any choices whereas the syntax only provides a syntactically correct variant. For example, having an option with a short option and a long option, the specification will show the short option and the long option whereas the syntax will only show either the short option or, if no short option is set, the long option.
- Parameters:
aSyntaxNotation- The syntax notation used for generating the command line arguments syntax.aArgumentEscapeCode- The escape code to use when rendering (option's) argumentsaKeywordEscapeCode- The escape code to be used when rendering keywords (options.)aResetEscapeCode- The escape code resetting any previously applied escape codes.- Returns:
- The human readable command line syntax
-
toSyntax
Creates a syntax including its arguments.A specification differs from a syntax in that the specification provides the full definition not omitting any choices whereas the syntax only provides a syntactically correct variant. For example, having an option with a short option and a long option, the specification will show the short option and the long option whereas the syntax will only show either the short option or, if no short option is set, the long option.
- Parameters:
aCliCtx- The CLI context controlling notation and formatting- Returns:
- The human readable command line syntax
-
toValue
Retrieves the value associated with the given alias after parsing.The syntax tree is searched for a
Termthat has produced a value for the specified alias.- Type Parameters:
V- The expected value type- Parameters:
aAlias- The alias to resolve- Returns:
- The resolved value or
nullif not set
-
getMatchCount
int getMatchCount()Determines the number of args being matched by theparseArgs(String[], CliContext)(and similar) method(s) of the according syntax. This way we can determine which part of the syntax (tree) had most matches.- Specified by:
getMatchCountin interfaceMatchCountAccessor- Returns:
- The number of args matching the according syntax (tree).
-
getException
ArgsSyntaxException getException()Retrieves theArgsSyntaxExceptionexception in case invokingparseArgs(String[], CliContext)failed. The exception (and the according suppressed ones alongside the causes) does not necessarily have been thrown by theparseArgs(String[], CliContext)method depending on the semantics of theTermhanding the exception (theAnyConditionprovides any exceptions it caught even though it did not throw them as of its semantics).- Specified by:
getExceptionin interfaceorg.refcodes.exception.ExceptionAccessor<ArgsSyntaxException>- Returns:
- The
ArgsSyntaxExceptionexception occurring while parsing the arguments.
-
setVisible
void setVisible(boolean isVisible) Visibility in this context means displaying or hiding this
Term's existence to the user (defaults totrue).- Specified by:
setVisiblein interfaceorg.refcodes.graphical.VisibleAccessor.VisibleMutator
-
isVisible
boolean isVisible()Visibility in this context means displaying or hiding this
Term's existence to the user (defaults totrue).- Specified by:
isVisiblein interfaceorg.refcodes.graphical.VisibleAccessor
-