Option Processor
An OptionProcessor serves primarily to support command-line argument processing, but may occasionally prove useful in other circumstances.
An option processor is parametric on the type of its option keys. The client provides this information for maximum type-safety and code-reuse.
The sole public protocol of an option processor is processOptions. It accepts an array of Strings which it treats as containing separate option strings. It extracts a keyword and an optional argument. The keyword is mapped to a client-specified action; the argument will be passed to this action upon positive identification of the option by keyword. null will be passed in lieu of an argument if the argument was omitted.
Option strings may have the following forms:
Begins with "-" (hyphen): Each subsequent character is treated as a distinct short option keyword. Short option keywords associate
nullarguments with their actions.Begins with "--" (double hyphen): The subsequent characters up to an "=" (equals) are treated collectively as a long option keyword. If no equals is discovered, then the action associated with the option is performed with a
nullargument; otherwise any characters following the equals are treated collectively as the argument of that action.Entirety is "--" (double hyphen): Disable special processing of hyphen prefixes. All further option strings are treated as unprefixed.
Does not begin with "-" (hyphen): It is treated as the argument of the action associated with the default option. An option processor supports a single default option. It is distinguished in that one of its keywords is the empty string.
A new option processor is obtainable via an appropriately parameterized factory. This allows incremental and arbitrary-order specification of the option processor independent of any runtime assembly constraints.
Author
Todd L Smith
Leslie Schultz
Parameters
The type of the option.
The type of option keys.
The complete mapping of recognizable keywords to option keys.
The complete collection of options.
The rules to run after all options have been processed, if each minimum Cardinality is satisfied.
Functions
If the specified key was encountered more times than allowed, then throw an OptionProcessingException. A key is considered encountered before running any user supplied action for it.
Treat the specified array as containing option strings. Process each option string separately, synchronously executing the action associated with it upon its discovery.
Answer the number of times that the option indicated by the specified key has been processed already.
Write the descriptions of the options defined by the OptionProcessor option processor into the specified Appendable.