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 UserAliasSourceBuilder<C> |
userAliasesBuilder |
| Constructor and Description |
|---|
ParserBuilder() |
| Modifier and Type | Method and Description |
|---|---|
ParserMetadata<C> |
build()
Builds the type
|
static <T> ParserMetadata<T> |
defaultConfiguration()
Gets the default configuration
|
AliasBuilder<C> |
getAlias(String name)
Retrieves an alias builder for the given alias
|
AliasBuilder<C> |
withAlias(String name)
Adds an alias
|
ParserBuilder<C> |
withAliasesChaining()
Sets that aliases may be defined in terms of other aliases
|
ParserBuilder<C> |
withAliasesOverridingBuiltIns()
Sets that aliases should override built-in commands
|
ParserBuilder<C> |
withAliasForceBuiltInPrefix(char prefix)
Sets a prefix character used in alias definitions to force use of a
built-in as opposed to a chained alias
|
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()
Sets that command abbreviation is enabled
|
ParserBuilder<C> |
withCommandFactory(CommandFactory<C> commandFactory)
Specifies the command factory to use
|
ParserBuilder<C> |
withDefaultCommandFactory()
Specifies that the default command factory should be used
|
ParserBuilder<C> |
withDefaultErrorHandler()
Sets that the default error handler should be used
|
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()
Sets that the default type converter should be used
|
ParserBuilder<C> |
withErrorHandler(ParserErrorHandler errorHandler)
Sets the error handler to use
|
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()
Sets that option abbreviation is enabled
|
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)
Sets the type converter for the parser
|
UserAliasSourceBuilder<C> |
withUserAliases()
Gets a builder that provides detailed control over building user aliases
|
ParserBuilder<C> |
withUserAliases(String programName)
Deprecated.
Use
withUserAliases() to access the user alias
builder directly instead |
ParserBuilder<C> |
withUserAliases(String programName,
String searchLocation)
Deprecated.
Use
withUserAliases() to access the user alias
builder directly instead |
ParserBuilder<C> |
withUserAliases(String filename,
String prefix,
List<ResourceLocator> locators,
String... searchLocations)
Deprecated.
Use
withUserAliases() to access the user alias
builder directly instead |
ParserBuilder<C> |
withUserAliases(String filename,
String prefix,
String... searchLocations)
Deprecated.
Use
withUserAliases() to access the user alias
builder directly instead |
checkNotBlank, checkNotEmpty, checkNotNullprotected 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 UserAliasSourceBuilder<C> userAliasesBuilder
protected ParserErrorHandler errorHandler
public static <T> ParserMetadata<T> defaultConfiguration()
T - Command type to parsepublic ParserBuilder<C> withCommandFactory(CommandFactory<C> commandFactory)
commandFactory - Command Factorypublic ParserBuilder<C> withDefaultCommandFactory()
public AliasBuilder<C> withAlias(String name)
name - Alias namepublic AliasBuilder<C> getAlias(String name)
name - Alias namepublic ParserBuilder<C> withAliasForceBuiltInPrefix(char prefix)
prefix - Prefic characterpublic UserAliasSourceBuilder<C> withUserAliases()
@Deprecated public ParserBuilder<C> withUserAliases(String programName)
withUserAliases() to access the user alias
builder directly instead
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
programName - Program Name@Deprecated public ParserBuilder<C> withUserAliases(String programName, String searchLocation)
withUserAliases() to access the user alias
builder directly instead
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
programName - Program namesearchLocation - Location to search@Deprecated public ParserBuilder<C> withUserAliases(String filename, String prefix, String... searchLocations)
withUserAliases() to access the user alias
builder directly instead
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 builderfilename - Filename to look forprefix - Prefix used to distinguish alias related properties from other
propertiessearchLocations - Search locations in order of preference@Deprecated public ParserBuilder<C> withUserAliases(String filename, String prefix, List<ResourceLocator> locators, String... searchLocations)
withUserAliases() to access the user alias
builder directly instead
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 builderfilename - Filename to look forprefix - Prefix used to distinguish alias related properties from other
propertieslocators - Locators used to resolve search locations to actual locations,
this is what enables things like ~/ to be used to
refer to the users home directory. If null then a
default set are used.searchLocations - Search locations in order of preferencepublic ParserBuilder<C> withAliasesOverridingBuiltIns()
public ParserBuilder<C> withAliasesChaining()
public ParserBuilder<C> withCommandAbbreviation()
public ParserBuilder<C> withOptionAbbreviation()
public ParserBuilder<C> withTypeConverter(TypeConverter converter)
converter - Type converterpublic ParserBuilder<C> withDefaultTypeConverter()
public ParserBuilder<C> withNumericTypeConverter(NumericTypeConverter converter)
converter - Numeric type converterpublic ParserBuilder<C> withDefaultNumericTypeConverter()
public ParserBuilder<C> withErrorHandler(ParserErrorHandler errorHandler)
errorHandler - Error handlerpublic 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
optionParser - Option parserpublic 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(OptionParser) or
withOptionParsers(OptionParser...) 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 - Arguments separatorpublic 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()
AbstractBuilderbuild in class AbstractBuilder<ParserMetadata<C>>Copyright © 2012–2022. All rights reserved.