C
- Command typepublic class ParserBuilder<C> extends AbstractBuilder<ParserMetadata<C>>
Modifier and Type | Field and Description |
---|---|
protected Map<String,AliasBuilder<C>> |
aliases |
protected boolean |
aliasesMayChain |
protected boolean |
aliasesOverrideBuiltIns |
protected boolean |
allowAbbreviatedCommands |
protected boolean |
allowAbbreviatedOptions |
protected String |
argsSeparator |
protected CommandFactory<C> |
commandFactory |
protected ParserErrorHandler |
errorHandler |
protected String |
flagNegationPrefix |
protected NumericTypeConverter |
numericTypeConverter |
protected List<OptionParser<C>> |
optionParsers |
protected TypeConverter |
typeConverter |
protected UserAliasesSource<C> |
userAliases |
Constructor and Description |
---|
ParserBuilder() |
Modifier and Type | Method and Description |
---|---|
ParserMetadata<C> |
build()
Builds the type
|
static <T> ParserMetadata<T> |
defaultConfiguration() |
AliasBuilder<C> |
getAlias(String name) |
AliasBuilder<C> |
withAlias(String name) |
ParserBuilder<C> |
withAliasesChaining() |
ParserBuilder<C> |
withAliasesOverridingBuiltIns() |
ParserBuilder<C> |
withArgumentsSeparator(String separator)
Sets the arguments separator, this is a token used to indicate the point
at which no further options will be seen and all further tokens should be
treated as arguments.
|
ParserBuilder<C> |
withCommandAbbreviation() |
ParserBuilder<C> |
withCommandFactory(CommandFactory<C> commandFactory) |
ParserBuilder<C> |
withDefaultCommandFactory() |
ParserBuilder<C> |
withDefaultErrorHandler() |
ParserBuilder<C> |
withDefaultNumericTypeConverter()
Indicates that default numeric type conversion should be used
|
ParserBuilder<C> |
withDefaultOptionParsers()
Configures the CLI to use the default set of option parsers in addition
to any previously registered
|
ParserBuilder<C> |
withDefaultTypeConverter() |
ParserBuilder<C> |
withErrorHandler(ParserErrorHandler errorHandler) |
ParserBuilder<C> |
withFlagNegationPrefix(String prefix)
Sets the flag negation prefix, this is used to determine whether to set a
flag option (a zero arity option) to
false rather than the usual
behaviour of setting it to true . |
ParserBuilder<C> |
withNumericTypeConverter(NumericTypeConverter converter)
Indicates the desired numeric type converter to use, this is passed as an
argument to the given type converter
|
ParserBuilder<C> |
withOnlyDefaultOptionParsers()
Configures the CLI to use only the default set of option parsers
|
ParserBuilder<C> |
withOptionAbbreviation() |
ParserBuilder<C> |
withOptionParser(OptionParser<C> optionParser)
Configures the CLI to use the given option parser
|
ParserBuilder<C> |
withOptionParsers(OptionParser<C>... optionParsers)
Configures the CLI to use the given option parsers
|
ParserBuilder<C> |
withTypeConverter(TypeConverter converter) |
ParserBuilder<C> |
withUserAliases(String programName)
Reads in user aliases from the default configuration file in the default
location.
|
ParserBuilder<C> |
withUserAliases(String programName,
String searchLocation)
Reads in user aliases from the default configuration file in the default
location
|
ParserBuilder<C> |
withUserAliases(String filename,
String prefix,
String... searchLocations)
Reads in user aliases from the default configuration file in the default
location
|
checkNotBlank, checkNotEmpty, checkNotNull
protected TypeConverter typeConverter
protected NumericTypeConverter numericTypeConverter
protected final Map<String,AliasBuilder<C>> aliases
protected CommandFactory<C> commandFactory
protected boolean allowAbbreviatedCommands
protected boolean allowAbbreviatedOptions
protected boolean aliasesOverrideBuiltIns
protected boolean aliasesMayChain
protected final List<OptionParser<C>> optionParsers
protected String argsSeparator
protected String flagNegationPrefix
protected UserAliasesSource<C> userAliases
protected ParserErrorHandler errorHandler
public static <T> ParserMetadata<T> defaultConfiguration()
public ParserBuilder<C> withCommandFactory(CommandFactory<C> commandFactory)
public ParserBuilder<C> withDefaultCommandFactory()
public AliasBuilder<C> withAlias(String name)
public AliasBuilder<C> getAlias(String name)
public ParserBuilder<C> withUserAliases(String programName)
The default configuration file name is constructed by appending the
.config
extension to the defined program name
The default search location is a .program
directory under the
users home directory where program
is the defined program name.
If you prefer to control these values explicitly and for more detail on
the configuration format please see the
withUserAliases(String, String, String...)
method
IOException
public ParserBuilder<C> withUserAliases(String programName, String searchLocation)
The default configuration file name is constructed by appending the
.config
extension to the defined program name
If you prefer to control this value explicitly and for more detail on the
configuration format please see the
withUserAliases(String, String, String...)
method
searchLocation
- Location to searchIOException
public ParserBuilder<C> withUserAliases(String filename, String prefix, String... searchLocations)
This file is in standard Java properties format with the key being the
alias and the value being the arguments for this alias. Arguments are
whitespace separated though quotes ("
) may be used to wrap
arguments that need to contain whitespace. Quotes may be escaped within
quoted arguments and whitespace may be escaped within unquoted arguments.
Note that since Java property values are interpreted as Java strings it
is necessary to double escape the backslash i.e. \\"
for this to
work properly.
example=command --option value quoted=command "long argument" escaped=command whitespace\\ escape "quote\\"escape"
The search locations should be given in order of preference, the file will be loaded from all search locations in which it exists such that values from the locations occurring first in the search locations list take precedence. This allows for having multiple locations for your configuration file and layering different sets of aliases over each other e.g. system, user and local aliases.
The prefix
is used to filter properties from the properties file
such that you can include aliases with other configuration settings in
your configuration files. When a prefix is used only properties that
start with the prefix are interpreted as alias definitions and the actual
alias is the property name with the prefix removed. For example if your
prefix was alias.
and you had a property alias.foo
the
resulting alias would be foo
.
withAliasesChaining()
} is specified and will result in errors
when used otherwise. Even when recursive aliases are enabled aliases
cannot use circular references.withAliasesOverridingBuiltIns()
on your builderIOException
public ParserBuilder<C> withAliasesOverridingBuiltIns()
public ParserBuilder<C> withAliasesChaining()
public ParserBuilder<C> withCommandAbbreviation()
public ParserBuilder<C> withOptionAbbreviation()
public ParserBuilder<C> withTypeConverter(TypeConverter converter)
public ParserBuilder<C> withDefaultTypeConverter()
public ParserBuilder<C> withNumericTypeConverter(NumericTypeConverter converter)
converter
- Numeric type converterpublic ParserBuilder<C> withDefaultNumericTypeConverter()
public ParserBuilder<C> withErrorHandler(ParserErrorHandler errorHandler)
public ParserBuilder<C> withDefaultErrorHandler()
public ParserBuilder<C> withOptionParser(OptionParser<C> optionParser)
Order of registration is important, if you have previously registered any parsers then those will be used prior to the one given here
optionParsers
- Option parserspublic ParserBuilder<C> withOptionParsers(OptionParser<C>... optionParsers)
Order of registration is important, if you have previously registered any parsers then those will be used prior to those given here
optionParsers
- Option parserspublic ParserBuilder<C> withOnlyDefaultOptionParsers()
This is the default behaviour so this need only be called if you have
previously configured some option parsers using the
#withOptionParser(Class)
or #withOptionParsers(Class...)
methods and wish to reset the configuration to the default.
If you wish to instead add the default parsers in addition to your custom
parsers you should instead call withDefaultOptionParsers()
public ParserBuilder<C> withDefaultOptionParsers()
Order of registration is important, if you have previously registered any parsers then those will be used prior to those in the default set.
public ParserBuilder<C> withArgumentsSeparator(String separator)
This is useful for disambiguating where arguments may be misinterpreted
as options. The default value of this is the standard --
used by
many command line tools.
separator
- public ParserBuilder<C> withFlagNegationPrefix(String prefix)
false
rather than the usual
behaviour of setting it to true
. Options must have appropriately
prefixed names defined for this prefix to have any effect i.e. setting it
does not automatically enable negation for flag options.prefix
- Flag negation prefixpublic ParserMetadata<C> build()
AbstractBuilder
build
in class AbstractBuilder<ParserMetadata<C>>
Copyright © 2012–2017. All rights reserved.