Package com.beust.jcommander
Class JCommander
java.lang.Object
com.beust.jcommander.JCommander
The main class for JCommander. It's responsible for parsing the object that contains
all the annotated fields, parse the command line and assign the fields with the correct
values and a few other helper methods, such as usage().
The object(s) you pass in the constructor are expected to have one or more
\@Parameter annotations on them. You can pass either a single object, an array of objects
or an instance of Iterable. In the case of an array or Iterable, JCommander will collect
the \@Parameter annotations from all the objects passed in parameter.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
static final class
Encapsulation of either a main application or an individual command. -
Field Summary
-
Constructor Summary
ConstructorDescriptionCreates a new un-configured JCommander object.JCommander
(Object object) JCommander
(Object object, String... args) Deprecated.Construct a JCommander instance first and then call parse() on it.JCommander
(Object object, ResourceBundle bundle) JCommander
(Object object, ResourceBundle bundle, String... args) -
Method Summary
Modifier and TypeMethodDescriptionvoid
addCommand
(Object object) void
addCommand
(String name, Object object) Add a command object.void
addCommand
(String name, Object object, String... aliases) Add a command object and its aliases.void
addConverterFactory
(IStringConverterFactory converterFactory) Adds a factory to lookup string converters.void
addConverterInstanceFactory
(IStringConverterInstanceFactory converterInstanceFactory) Adds a factory to lookup string converters.final void
Adds the provided arg object to the set of objects that this commander will parse arguments into.convertValue
(Parameterized parameterized, Class type, String optionName, String value) void
Create the ParameterDescriptions for all the \@Parameter found.findCommandByAlias
(String commandOrAlias) int
Map
<com.beust.jcommander.FuzzyMap.IKey, ParameterDescription> com.beust.jcommander.JCommander.Options
Comparator
<? super ParameterDescription> The name of the command or the alias in the form it was passed to the command line.Get the program display name (used only in the usage).Get the program name (used only in the usage).Returns the usage formatter.boolean
static JCommander.Builder
void
Parse and validate the command line parameters.void
parseWithoutValidation
(String... args) Parse the command line parameters without validating them.void
setAcceptUnknownOptions
(boolean b) void
setAllowAbbreviatedOptions
(boolean b) void
setAllowParameterOverwriting
(boolean b) void
setAtFileCharset
(Charset charset) Sets the charset used to expand@files
.void
setCaseSensitiveOptions
(boolean b) void
setColumnSize
(int columnSize) void
setConsole
(Console console) void
setDefaultProvider
(IDefaultProvider defaultProvider) Define the default provider for this instance.final void
setDescriptionsBundle
(ResourceBundle bundle) Sets theResourceBundle
to use for looking up descriptions.void
setExpandAtSign
(boolean expandAtSign) Disables expanding@file
.void
void
setParameterizedParser
(IParameterizedParser parameterizedParser) void
setProgramName
(String name) Set the program name (used only in the usage).void
setProgramName
(String name, String... aliases) Set the program namevoid
setUsageFormatter
(IUsageFormatter usageFormatter) Sets the usage formatter.void
setVerbose
(int verbose) void
usage()
Prints the usage ongetConsole()
using the underlyingusageFormatter
.void
Display the usage for this command.void
usage
(StringBuilder out) Store the help in the passed string builder.void
usage
(StringBuilder out, String indent) void
usage
(String commandName, StringBuilder out) Store the help for the command in the passed string builder.void
usage
(String commandName, StringBuilder out, String indent) Store the help for the command in the passed string builder, indenting every line with "indent".
-
Field Details
-
DEBUG_PROPERTY
- See Also:
-
parameterizedParser
-
-
Constructor Details
-
JCommander
public JCommander()Creates a new un-configured JCommander object. -
JCommander
- Parameters:
object
- The arg object expected to containParameter
annotations.
-
JCommander
- Parameters:
object
- The arg object expected to containParameter
annotations.bundle
- The bundle to use for the descriptions. Can be null.
-
JCommander
- Parameters:
object
- The arg object expected to containParameter
annotations.bundle
- The bundle to use for the descriptions. Can be null.args
- The arguments to parse (optional).
-
JCommander
Deprecated.Construct a JCommander instance first and then call parse() on it.- Parameters:
object
- The arg object expected to containParameter
annotations.args
- The arguments to parse (optional).
-
-
Method Details
-
setParameterizedParser
-
setExpandAtSign
public void setExpandAtSign(boolean expandAtSign) Disables expanding@file
. JCommander supports the@file
syntax, which allows you to put all your options into a file and pass this file as parameter @param expandAtSign whether to expand@file
. -
setConsole
-
getConsole
- Returns:
- a wrapper for a
PrintStream
, typicallySystem.out
.
-
addObject
Adds the provided arg object to the set of objects that this commander will parse arguments into. -
setDescriptionsBundle
Sets theResourceBundle
to use for looking up descriptions. Set this tonull
to use description text directly. -
parse
Parse and validate the command line parameters. -
parseWithoutValidation
Parse the command line parameters without validating them. -
createDescriptions
public void createDescriptions()Create the ParameterDescriptions for all the \@Parameter found. -
getMainParameterDescription
-
setProgramName
Set the program name (used only in the usage). -
getProgramName
Get the program name (used only in the usage). -
getProgramDisplayName
Get the program display name (used only in the usage). -
setProgramName
Set the program name- Parameters:
name
- program namealiases
- aliases to the program name
-
usage
public void usage()Prints the usage ongetConsole()
using the underlyingusageFormatter
. -
usage
Display the usage for this command. -
usage
Store the help for the command in the passed string builder. -
usage
Store the help for the command in the passed string builder, indenting every line with "indent". -
usage
Store the help in the passed string builder. -
usage
-
setUsageFormatter
Sets the usage formatter.- Parameters:
usageFormatter
- the usage formatter- Throws:
IllegalArgumentException
- if the argument is null
-
getUsageFormatter
Returns the usage formatter.- Returns:
- the usage formatter
-
getOptions
public com.beust.jcommander.JCommander.Options getOptions() -
getDescriptions
-
getMainParameter
-
newBuilder
-
getFields
-
getParameterDescriptionComparator
-
setParameterDescriptionComparator
-
setColumnSize
public void setColumnSize(int columnSize) -
getColumnSize
public int getColumnSize() -
getBundle
-
getParameters
- Returns:
- a Collection of all the \@Parameter annotations found on the target class. This can be used to display the usage() in a different format (e.g. HTML).
-
getMainParameterValue
- Returns:
- the main parameter description or null if none is defined.
-
setDefaultProvider
Define the default provider for this instance. -
addConverterFactory
Adds a factory to lookup string converters. The added factory is used prior to previously added factories.- Parameters:
converterFactory
- the factory determining string converters
-
addConverterInstanceFactory
Adds a factory to lookup string converters. The added factory is used prior to previously added factories.- Parameters:
converterInstanceFactory
- the factory generating string converter instances
-
convertValue
public Object convertValue(Parameterized parameterized, Class type, String optionName, String value) - Parameters:
type
- The type of the actual parameteroptionName
-value
- The value to convert
-
addCommand
Add a command object. -
addCommand
-
addCommand
Add a command object and its aliases. -
getCommands
-
getRawCommands
-
getParsedCommand
-
getParsedAlias
The name of the command or the alias in the form it was passed to the command line.null
if no command or alias was specified.- Returns:
- Name of command or alias passed to command line. If none passed:
null
.
-
getObjects
- Returns:
- the objects that JCommander will fill with the result of parsing the command line.
-
findCommandByAlias
-
setVerbose
public void setVerbose(int verbose) -
setCaseSensitiveOptions
public void setCaseSensitiveOptions(boolean b) -
setAllowAbbreviatedOptions
public void setAllowAbbreviatedOptions(boolean b) -
setAcceptUnknownOptions
public void setAcceptUnknownOptions(boolean b) -
getUnknownOptions
-
setAllowParameterOverwriting
public void setAllowParameterOverwriting(boolean b) -
isParameterOverwritingAllowed
public boolean isParameterOverwritingAllowed() -
setAtFileCharset
Sets the charset used to expand@files
.- Parameters:
charset
- the charset
-