Package eu.bandm.tools.option.runtime
Class Model<M extends Model>
java.lang.Object
eu.bandm.tools.option.runtime.Model<M>
- Direct Known Subclasses:
BatchOptions
,CallXsltOptions
,InsertOverprintOptions
,JarLinkerOptions
,JWhichOptions
,Options
,Options
,Options
,Options
,Options
,Options
,Options
,ParameterExpanderOptions
,ToolOptions
The common base class of all models generated by the
the option compiler,
as implemented in
Compiler
.
It contains
- the basic parsing methods, which are called by the generated code,
- and some auxiliary methods callable by the user after successful parsing.
It is multi-lingual only indirectly, by the message pipeline
used in the parser process, and by the generated code for
usage()
and usage(String)
.
The fundamental control flow when parsing is a "ping-pong" between generated code and runtime library:
parse(String[], msgr) V parse(String, msgr) // blanks are escaped ! | V / main parsing loop ======================================= \ | \ (recover after error!) \ V \ -------------------------------------- GENERATED CODE | | parseName /parseAbbrev/parsePositionals | | V | | | parse_Please not that the lexer process = the tokenization (implemented by| | | | | | | | ---------------------------------|--------------------- | | | V | | | V parseEnum | | V parseInt | | parseInit | | | V V | TYPE_ERROR ERROR_UNKNOWN_NAME / \ \ / \_________\_________>THROW exception /
getNextToken()
) is
driven by the top-level parsing process:
A long-name option key and an argument token are all characters up to the
next whitespace (or end of input),
but a short-name option token is only the next character.-
Nested Class Summary
Modifier and TypeClassDescriptionprotected static class
Thrown if option appears more than once.protected static class
Thrown if the type of an argument menas to end a repetition group.protected static class
Thrown if argument is needed, but end of line reached.protected static class
Thrown if end of line reached when new positional option could start.protected static class
Thrown if "skip to next option" error recovery reaches end of input.protected static class
Thrown if option name / abbrev is unknown.protected static class
Thrown if argument is of wrong type.static class
An adapter which escapes blanks contained in an opt arg by a backslash.static class
An adapter which encloses empty string values or string values containing double double quotes or blanks in double quotes, The contained double quotes are escaped by backslashes.(package private) static interface
Adapter for different shell behaviour when passing options by command line. -
Field Summary
Modifier and TypeFieldDescriptionprotected int
Number of the argument currently being parsed.protected int
Number of the repeting group which is under parsing, or-1
in case the fixed part is currently being parsed.protected String
Human readable identification of the option currently being parsed.protected int
Position/column number which will be printed in error messages.Filled in by generated code.protected static final Rational
Dummy value returned in case of parsing error.static final String
boolean
Reflects whether there are any payload-options in the parsed command line.protected int
Length ofinputstring
.protected String
Input concatentated into one single string, only input data for the parsing process.protected Matcher
Re-used matcher for the input, once loaded with string data, then re-linked to different patterns.final Messages
Translation catalog for the generic = non-payload messages The payload messages (specific for the options) are indescriptions
.protected MessagePasser<SimpleMessage<String>>
Target of all error messages, warnings, etc.Argument token, written bygetNextToken()
, which uses theshellAdapter
, and read by type-specific scannersparseInt()
, etc., and cleared by these, when consuming.protected String
Set by the main loop when decoding an option name or abbrev, and only read for error messaging.(package private) final Pattern
Pattern to recognize the explict boolean parameter values.protected final Pattern
Pattern which matches any non-blank rest on the command line, to generate the corresponding error message.protected boolean
Set by runtime code for correct end-of-cmdline handling.protected boolean
Set by generated code, whenever a positional option has already been set explicitly.protected boolean
Set by generated code, whenever a positional option is set implicitly.protected StringBuilder
Text which reflects the parsing and interpretation process, for debugging only.protected Model.ShellAdapter
The instance used for parsing and for serialization. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected abstract void
Callback, overridden by compiled code.protected abstract String
Is overridden in the generated code and concatenates the "longest names" of all otions marked as required, but not present in the parsed input.protected abstract String
Callback, overridden by compiled code.protected void
Generate error message related to the current option, the argument number, and (possibly) the repetition group number.protected void
Generate error message related to the current option, the argument number, and (possibly) the repetition group number.protected boolean
Returns only true if the next consumable input is not an option key (i.e.protected abstract void
The generated code will evaluate all conditions to activate/deactive options in the GUI.static int
Convert the text string to a number, either hexadecimally, when starting with0x
, otherwise decimally.static String
Generated MuLi-Catalog key to store a (multilingual) comment field.static String
Generated MuLi-Catalog key to store the description of an enum type.static String
descriptionKeyForEnum
(Class<?> cl) Generated MuLi-Catalog key to store the description of an enum type.static String
descriptionKeyForEnumItem
(Class<?> cl, Object item) Generated MuLi-Catalog key to store the description of an enum item value.protected void
Only source of error messages.protected void
Generate the error message that the repeting group of "plus" type is empty.protected void
Called from compiled code and the end of the switch between short optipn names.protected void
Called from compiled code and the end of the switch between long optipn names.Get parsing input, maybe constructed from a String-array.Get String of missing parameters, is empty if none are missing.protected void
protected void
Get next token in a parsing situation where an argument is mandatory, according to the type declaration ofcurOption
.protected void
Only source of hint messages.protected String
Use the input text as its own document identifier.protected String
Allows to add a new translation tomessages
at the place of their usage.abstract M
Overriden by the generated code to make a constructor call of the actual Model (sub-)class.protected abstract int
The generated code will return the number of positional parameters.void
parse
(String[] args, MessageReceiver<SimpleMessage<String>> mr, @Opt String language) The main entry for setting all values according to some command line input, as reachingpublic void main(String[])
from someexec
system call.protected abstract void
Callback, overridden by compiled code, which realizes a switch over all one character option names.protected boolean
Parser function, only called when a boolean argument representation must necessarily be consumed, according to the current option's type grammar.protected boolean
Called by compiled code for "switch-type", argument-less options.Aux method which returns whether a boolean representation is parsed.protected <E extends Enum<E>>
EParser function, only called from the compiled code when an enumeration item of the given type must necessarily be consumed, according to the current option's type grammar.parseEnumSet
(Class<E> enumclass) Parser function, only called from the compiled code when a (possibly empty) set of enumeration items of the given type is expected according to the current option's type grammar.protected double
Parser function, only called from the compiled code when an float argument representation must necessarily be consumed, according to the current option's type grammar.protected void
Called from compiled code when starting the parsing of the arguments for a recognized/accepted option key.protected int
parseInt()
Parser function, only called from the compiled code when an integer argument representation must necessarily be consumed, according to the current option's type grammar.protected int
Parser function, only called from the compiled code when an integer argument representation must necessarily be consumed, according to the current option's type grammar.protected abstract void
Callback, overridden by compiled code, which realizes a switch over all long option names.void
Central entry point for parsing.void
parseOneString
(String s, MessageReceiver<SimpleMessage<String>> mr, boolean gnuVersion) Central entry point for parsing.void
parseOneString
(String s, MessageReceiver<SimpleMessage<String>> mr, @Opt String lang) Central entry point for parsing.Parser function, only called from the compiled code when a String argument representation must necessarily be consumed, according to the current option's type grammar.protected abstract void
Callback, overridden by compiled code, which calls one after the other the positional parameters.protected Rational
Parser function, only called from the compiled code when a rational argument representation must necessarily be consumed, according to the current option's type grammar.protected String
Parser function, only called from the compiled code when a String argument representation must necessarily be consumed, according to the current option's type grammar.protected void
Append a string to the parsing protocol.protected void
Append the name of the option and set parentheses to the parsing protocol.abstract String
Called to generate a String representation of the model's state.protected static String
serialize
(boolean value) Called by compiled code.protected static String
serialize
(char value) Called by compiled code.protected static String
serialize
(double value) Called by compiled code.protected static String
serialize
(int value) Called by compiled code.serialize
(E value) Called by compiled code.protected static String
Called by compiled code.protected String
Called by compiled code.Called by compiled code.protected static String
serialize_hex
(int value) Called by compiled code.protected void
Skip input to next option token.protected void
Generate error message on wrong argument type and throw the corresponding exception.protected void
The next token bo be parsed as an argument does not match the type expected according to the current options's declaration.protected void
TYPE_MISMATCH
(String text) The next token bo be parsed as an argument does not match the type expected according to the current options's declaration.void
usage()
Supply the standard printstreamSystem.err
and call the generated code.abstract void
usage
(PrintStream ps) The generated code will print the documentation text of all options, in the default language.abstract void
usage
(PrintStream ps, String language) The generated code will print the documentation text of all options, in the given language.void
Supply the standard printstreamSystem.err
and call the generated code.protected void
Only source of warning messages.
-
Field Details
-
GNU_VERSION_OPTION
- See Also:
-
shellAdapter
The instance used for parsing and for serialization. E.g. in error messages. Can be changed by subclassing. -
messages
Translation catalog for the generic = non-payload messages The payload messages (specific for the options) are indescriptions
. -
msg
Target of all error messages, warnings, etc. -
protocol
Text which reflects the parsing and interpretation process, for debugging only. Can be retrieved after successful and erroneous parsing bygetParsingProtocol()
. The entering the parsing of an option with name "xxx" is indicated by "xxx(" and the recognized argument types follow, like "int", "string", etc. -
descriptions
Filled in by generated code. Needed by the GUI for dynamic tool-tip translation. -
hasNonMetaOptions
public boolean hasNonMetaOptionsReflects whether there are any payload-options in the parsed command line. -
inputstring
Input concatentated into one single string, only input data for the parsing process. -
inputlength
protected transient int inputlengthLength ofinputstring
. For conveniece, because it is frequently used in Matcher set-ups. -
curOption
Human readable identification of the option currently being parsed. Used for error message generation. Set by the generated code when callingparseInit(String, boolean)
. -
curArg
protected transient int curArgNumber of the argument currently being parsed. This is relative to begin of the option's type list, or to the beginnig of the repeting group. Is initialized byparseInit(String,boolean)
to -1 and incremented by the generated code before each call to the primitive parser. -
curArgGroup
protected transient int curArgGroupNumber of the repeting group which is under parsing, or-1
in case the fixed part is currently being parsed. -
p_rest
Pattern which matches any non-blank rest on the command line, to generate the corresponding error message. Trying to detect such a rest simply bygetNextToken()
does not work, because eg."a
will not be accepted. -
matcher
Re-used matcher for the input, once loaded with string data, then re-linked to different patterns. -
nextToken
Argument token, written bygetNextToken()
, which uses theshellAdapter
, and read by type-specific scannersparseInt()
, etc., and cleared by these, when consuming. -
currentErrorPos
protected transient int currentErrorPosPosition/column number which will be printed in error messages. Is the start column ofnextToken
and set by theshellAdapter
. -
positionalsExplicit
protected boolean positionalsExplicitSet by generated code, whenever a positional option has already been set explicitly. -
positionalsFound
protected boolean positionalsFoundSet by generated code, whenever a positional option is set implicitly. -
positionalPhase
protected boolean positionalPhaseSet by runtime code for correct end-of-cmdline handling. -
optionDirector
Set by the main loop when decoding an option name or abbrev, and only read for error messaging. -
dummyRat
Dummy value returned in case of parsing error. -
p_bool
Pattern to recognize the explict boolean parameter values.
-
-
Constructor Details
-
Model
public Model()
-
-
Method Details
-
usage
The generated code will print the documentation text of all options, in the default language. For each option print its names, its argument type structure and the description in the selected language. -
usage
The generated code will print the documentation text of all options, in the given language. For each option print its names, its argument type structure and the description in the selected language. -
checkActive
protected abstract void checkActive()The generated code will evaluate all conditions to activate/deactive options in the GUI. -
numberOfPositionals
protected abstract int numberOfPositionals()The generated code will return the number of positional parameters. -
usage
public void usage()Supply the standard printstreamSystem.err
and call the generated code. -
usage
Supply the standard printstreamSystem.err
and call the generated code. -
INSITU
Allows to add a new translation tomessages
at the place of their usage. The return result is the un-translated key. -
input_as_its_own_id
Use the input text as its own document identifier. -
ERROR
Only source of error messages. -
HINT
Only source of hint messages. -
WARNING
Only source of warning messages. -
putprot
Append a string to the parsing protocol. -
putprot_option
Append the name of the option and set parentheses to the parsing protocol. -
getParsingProtocol
- Returns:
- the parsing protocol, for debugging.
-
descriptionKeyForEnum
Generated MuLi-Catalog key to store the description of an enum type. -
descriptionKeyForEnum
Generated MuLi-Catalog key to store the description of an enum type. -
descriptionKeyForEnumItem
Generated MuLi-Catalog key to store the description of an enum item value. -
descriptionKeyForCommentField
Generated MuLi-Catalog key to store a (multilingual) comment field. -
makeDefaultInstance
Overriden by the generated code to make a constructor call of the actual Model (sub-)class. -
_finalCheck
protected abstract void _finalCheck()Callback, overridden by compiled code. -
_getRedundantOptions
Callback, overridden by compiled code. -
parseAbbrev
Callback, overridden by compiled code, which realizes a switch over all one character option names. The generated code calls the argument parsers and stores the resulting values to the model's fields.- Parameters:
s
- the one-character option name, as parsed by this code.
-
parseName
Callback, overridden by compiled code, which realizes a switch over all long option names. The generated code calls the argument parsers and stores the resulting values to the model's fields.- Parameters:
s
- the multi-character option name, as parsed by this code.
-
parsePositionals
protected abstract void parsePositionals()Callback, overridden by compiled code, which calls one after the other the positional parameters. Each of them corresponds to an implicit option (with short names "-0 ", "-1 ", "-2 ", ...). These can also be defined explicitly, e.g. be given a long name, etc. Anyhow, they can be set explicitly by their short name. If one single positional option is given explicitly, the implicit parsing of the others is suppressed and this code will not be called. -
ARG_ERROR
Generate error message related to the current option, the argument number, and (possibly) the repetition group number.- Parameters:
msg
- must be marked for embedding translation
-
ARG_ERROR
Generate error message related to the current option, the argument number, and (possibly) the repetition group number.- Parameters:
msg
- is the context-dependend error messages, is thus marked for translation and expansion and has possibly arguments on its own.
-
ERROR_plus
protected void ERROR_plus()Generate the error message that the repeting group of "plus" type is empty. -
TYPE_MISMATCH
The next token bo be parsed as an argument does not match the type expected according to the current options's declaration. -
TYPE_MISMATCH
The next token bo be parsed as an argument does not match the type expected according to the current options's declaration. If this argument is the first in a repetiontion group, then this is allowed and simply means the end of this group. Otherwise an error is generateod by callingtype_error(Embed)
. -
type_error
Generate error message on wrong argument type and throw the corresponding exception. The next higher level will try to resume parsing with the next option.- Throws:
Model.ExcWrongType
-
skipToOption
protected void skipToOption()Skip input to next option token. Only called for error recovery, at end of main parsing loop.- Throws:
Model.ExcSkipReachesEnd
- if no option token can be found in the rest of the input.
-
canReenterRepetitionGroup
protected boolean canReenterRepetitionGroup()Returns only true if the next consumable input is not an option key (i.e. starting with "-"), AND there is more character data. Because of the first condition, repeting argument groups can unfortunately not start with a negative number. -
getNextToken
protected void getNextToken() -
getNextToken_required
protected void getNextToken_required()Get next token in a parsing situation where an argument is mandatory, according to the type declaration ofcurOption
. Therefore here errors can be generated and an exception be thrown to the option loop. -
parse
public void parse(String[] args, MessageReceiver<SimpleMessage<String>> mr, @Opt @Opt String language) The main entry for setting all values according to some command line input, as reachingpublic void main(String[])
from someexec
system call.- Parameters:
language
- if != null, all messages will be sent translated. Otherwise the translation must happen later in the message pipeline, usingmessages
explicitly.
-
getInputString
Get parsing input, maybe constructed from a String-array. -
parseOneString
public void parseOneString(String s, MessageReceiver<SimpleMessage<String>> mr, @Opt @Opt String lang) Central entry point for parsing. Here the command line data is already packed into one single String, with escaped blanks, and the user language for messages can be switched. If no language is provided, the translation must happen later in the message pipeline, usingMessages
explicitly. -
parseOneString
Central entry point for parsing. CallparseOneString(String, MessageReceiver, boolean)
with gnu mode set to true, -
parseOneString
Central entry point for parsing. Here the command line data is already packed into one single String, with escaped blanks. No translation is added to the pipeline, but this shall be done by the MessageReceiver.- Parameters:
gnuVersion
- whether "--version" shall be treated specially: no further error checks must be applied, according to https://www.gnu.org/prep/standards/html_node/_002d_002dversion.html
-
getMissingOptions
Get String of missing parameters, is empty if none are missing. Must be called after parsing. -
_getMissingOptions
Is overridden in the generated code and concatenates the "longest names" of all otions marked as required, but not present in the parsed input. Length==0 iff none is missing. Cf "sourcedtd required='yes'" in tools/tdom_withOptions/Options.xml -
parseInit
Called from compiled code when starting the parsing of the arguments for a recognized/accepted option key. Reset all flags and counters and stores the name for error message and protocol generation.- Parameters:
opt
- Name of option (both names, for error messages only)hasIt
- if this option has already occured in this parsing process
-
ERROR_UNKNOWN_NAME
protected void ERROR_UNKNOWN_NAME()Called from compiled code and the end of the switch between long optipn names. -
ERROR_UNKNOWN_ABBREV
protected void ERROR_UNKNOWN_ABBREV()Called from compiled code and the end of the switch between short optipn names. -
parseInt
protected int parseInt()Parser function, only called from the compiled code when an integer argument representation must necessarily be consumed, according to the current option's type grammar. Get the next argument token, convert it into a value, consume the input and return the value. If conversion is not possible, throw a type error and do not consume. -
convert_integer_10_or_16
Convert the text string to a number, either hexadecimally, when starting with0x
, otherwise decimally. Only the latter case allows explicit +/- signs. Is called by the verifiers in theGui
runtime.- Throws:
NumberFormatException
-
parseInt_16or10
protected int parseInt_16or10()Parser function, only called from the compiled code when an integer argument representation must necessarily be consumed, according to the current option's type grammar. Get the next argument token, convert it into a value, consume the input and return the value. If conversion is not possible, throw a type error and do not consume. Conversion is either hexadecimally, when starting with0x
, otherwise decimally. Only the latter case allows explicit +/- signs. -
parseFloat
protected double parseFloat()Parser function, only called from the compiled code when an float argument representation must necessarily be consumed, according to the current option's type grammar. Get the next argument token, convert it into a value, consume the input and return the value. If conversion is not possible, throw a type error and do not consume. -
parseRational
Parser function, only called from the compiled code when a rational argument representation must necessarily be consumed, according to the current option's type grammar. Get the next argument token, convert it into a value, consume the input and return the value. If conversion is not possible, throw a type error and do not consume. -
parseBoolTest
Aux method which returns whether a boolean representation is parsed. In this case it consumes the text and returns the value.- See Also:
-
parseBool
protected boolean parseBool()Parser function, only called when a boolean argument representation must necessarily be consumed, according to the current option's type grammar. Is called directly from generated code due to a normal type declaration. (For "switch type" options seeparseBool_optional()
. -
parseBool_optional
protected boolean parseBool_optional()Called by compiled code for "switch-type", argument-less options. A boolean argument may follow, and is greedily consumed, but if not, the value defaults to true. -
parseString
Parser function, only called from the compiled code when a String argument representation must necessarily be consumed, according to the current option's type grammar. Get the next argument token, take its text as the value, consume the input and return the value. -
parseOneUri
Parser function, only called from the compiled code when a String argument representation must necessarily be consumed, according to the current option's type grammar. Get the next argument token, convert it into a value, consume the input and return the value. If conversion is not possible, throw a type error and do not consume. -
parseEnum
Parser function, only called from the compiled code when an enumeration item of the given type must necessarily be consumed, according to the current option's type grammar. Get the next argument token, take its text as the value, consume the input and return the value. If conversion is not possible, throw a type error and do not consume. -
parseEnumSet
Parser function, only called from the compiled code when a (possibly empty) set of enumeration items of the given type is expected according to the current option's type grammar. Consume greedily all tokens which can be interpreted as enumeration items. -
serialize
Called to generate a String representation of the model's state. Code is overridden by the compiled code. -
serialize
Called by compiled code. Always PREpend a blank to the added string constant. -
serialize_hex
Called by compiled code. -
serialize
Called by compiled code. -
serialize
Called by compiled code. -
serialize
Called by compiled code. -
serialize
Called by compiled code. The fieldshellAdapter
is considered. -
serialize
Called by compiled code. -
serialize
Called by compiled code. -
serialize
Called by compiled code.
-