Package com.beust.jcommander
Class JCommander
- java.lang.Object
-
- com.beust.jcommander.JCommander
-
public class JCommander extends Object
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.- Author:
- Cedric Beust <cedric@beust.com>
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classJCommander.Builderstatic classJCommander.ProgramNameEncapsulation of either a main application or an individual command.
-
Field Summary
Fields Modifier and Type Field Description static StringDEBUG_PROPERTY
-
Constructor Summary
Constructors Constructor Description JCommander()Creates 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
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddCommand(Object object)voidaddCommand(String name, Object object)Add a command object.voidaddCommand(String name, Object object, String... aliases)Add a command object and its aliases.voidaddConverterFactory(IStringConverterFactory converterFactory)Adds a factory to lookup string converters.voidaddConverterInstanceFactory(IStringConverterInstanceFactory converterInstanceFactory)Adds a factory to lookup string converters.voidaddObject(Object object)Adds the provided arg object to the set of objects that this commander will parse arguments into.ObjectconvertValue(Parameterized parameterized, Class type, String optionName, String value)voidcreateDescriptions()Create the ParameterDescriptions for all the \@Parameter found.JCommanderfindCommandByAlias(String commandOrAlias)ResourceBundlegetBundle()intgetColumnSize()Map<String,JCommander>getCommands()ConsolegetConsole()Map<com.beust.jcommander.FuzzyMap.IKey,ParameterDescription>getDescriptions()Map<Parameterized,ParameterDescription>getFields()com.beust.jcommander.JCommander.MainParametergetMainParameter()StringgetMainParameterDescription()ParameterDescriptiongetMainParameterValue()List<Object>getObjects()com.beust.jcommander.JCommander.OptionsgetOptions()Comparator<? super ParameterDescription>getParameterDescriptionComparator()List<ParameterDescription>getParameters()StringgetParsedAlias()The name of the command or the alias in the form it was passed to the command line.StringgetParsedCommand()StringgetProgramDisplayName()Get the program display name (used only in the usage).StringgetProgramName()Get the program name (used only in the usage).Map<JCommander.ProgramName,JCommander>getRawCommands()List<String>getUnknownOptions()IUsageFormattergetUsageFormatter()Returns the usage formatter.booleanisParameterOverwritingAllowed()static JCommander.BuildernewBuilder()voidparse(String... args)Parse and validate the command line parameters.voidparseWithoutValidation(String... args)Parse the command line parameters without validating them.voidsetAcceptUnknownOptions(boolean b)voidsetAllowAbbreviatedOptions(boolean b)voidsetAllowParameterOverwriting(boolean b)voidsetAtFileCharset(Charset charset)Sets the charset used to expand@files.voidsetCaseSensitiveOptions(boolean b)voidsetColumnSize(int columnSize)voidsetConsole(Console console)voidsetDefaultProvider(IDefaultProvider defaultProvider)Define the default provider for this instance.voidsetDescriptionsBundle(ResourceBundle bundle)Sets theResourceBundleto use for looking up descriptions.voidsetExpandAtSign(boolean expandAtSign)Disables expanding@file.voidsetParameterDescriptionComparator(Comparator<? super ParameterDescription> c)voidsetProgramName(String name)Set the program name (used only in the usage).voidsetProgramName(String name, String... aliases)Set the program namevoidsetUsageFormatter(IUsageFormatter usageFormatter)Sets the usage formatter.voidsetVerbose(int verbose)voidusage()Prints the usage ongetConsole()using the underlyingusageFormatter.
-
-
-
Field Detail
-
DEBUG_PROPERTY
public static final String DEBUG_PROPERTY
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
JCommander
public JCommander()
Creates a new un-configured JCommander object.
-
JCommander
public JCommander(Object object)
- Parameters:
object- The arg object expected to containParameterannotations.
-
JCommander
public JCommander(Object object, ResourceBundle bundle)
- Parameters:
object- The arg object expected to containParameterannotations.bundle- The bundle to use for the descriptions. Can be null.
-
JCommander
public JCommander(Object object, ResourceBundle bundle, String... args)
- Parameters:
object- The arg object expected to containParameterannotations.bundle- The bundle to use for the descriptions. Can be null.args- The arguments to parse (optional).
-
JCommander
@Deprecated public JCommander(Object object, String... args)
Deprecated.Construct a JCommander instance first and then call parse() on it.- Parameters:
object- The arg object expected to containParameterannotations.args- The arguments to parse (optional).
-
-
Method Detail
-
setExpandAtSign
public void setExpandAtSign(boolean expandAtSign)
Disables expanding@file. JCommander supports the@filesyntax, which allows you to put all your options into a file and pass this file as parameter @param expandAtSign whether to expand@file.
-
setConsole
public void setConsole(Console console)
-
getConsole
public Console getConsole()
- Returns:
- a wrapper for a
PrintStream, typicallySystem.out.
-
addObject
public final void addObject(Object object)
Adds the provided arg object to the set of objects that this commander will parse arguments into.
-
setDescriptionsBundle
public final void setDescriptionsBundle(ResourceBundle bundle)
Sets theResourceBundleto use for looking up descriptions. Set this tonullto use description text directly.
-
parse
public void parse(String... args)
Parse and validate the command line parameters.
-
parseWithoutValidation
public void parseWithoutValidation(String... args)
Parse the command line parameters without validating them.
-
createDescriptions
public void createDescriptions()
Create the ParameterDescriptions for all the \@Parameter found.
-
getMainParameterDescription
public String getMainParameterDescription()
-
setProgramName
public void setProgramName(String name)
Set the program name (used only in the usage).
-
getProgramName
public String getProgramName()
Get the program name (used only in the usage).
-
getProgramDisplayName
public String getProgramDisplayName()
Get the program display name (used only in the usage).
-
setProgramName
public void setProgramName(String name, String... aliases)
Set the program name- Parameters:
name- program namealiases- aliases to the program name
-
usage
public void usage()
Prints the usage ongetConsole()using the underlyingusageFormatter.
-
setUsageFormatter
public void setUsageFormatter(IUsageFormatter usageFormatter)
Sets the usage formatter.- Parameters:
usageFormatter- the usage formatter- Throws:
IllegalArgumentException- if the argument is null
-
getUsageFormatter
public IUsageFormatter getUsageFormatter()
Returns the usage formatter.- Returns:
- the usage formatter
-
getOptions
public com.beust.jcommander.JCommander.Options getOptions()
-
getDescriptions
public Map<com.beust.jcommander.FuzzyMap.IKey,ParameterDescription> getDescriptions()
-
getMainParameter
public com.beust.jcommander.JCommander.MainParameter getMainParameter()
-
newBuilder
public static JCommander.Builder newBuilder()
-
getFields
public Map<Parameterized,ParameterDescription> getFields()
-
getParameterDescriptionComparator
public Comparator<? super ParameterDescription> getParameterDescriptionComparator()
-
setParameterDescriptionComparator
public void setParameterDescriptionComparator(Comparator<? super ParameterDescription> c)
-
setColumnSize
public void setColumnSize(int columnSize)
-
getColumnSize
public int getColumnSize()
-
getBundle
public ResourceBundle getBundle()
-
getParameters
public List<ParameterDescription> 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
public ParameterDescription getMainParameterValue()
- Returns:
- the main parameter description or null if none is defined.
-
setDefaultProvider
public void setDefaultProvider(IDefaultProvider defaultProvider)
Define the default provider for this instance.
-
addConverterFactory
public void addConverterFactory(IStringConverterFactory converterFactory)
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
public void addConverterInstanceFactory(IStringConverterInstanceFactory converterInstanceFactory)
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
public void addCommand(Object object)
-
addCommand
public void addCommand(String name, Object object, String... aliases)
Add a command object and its aliases.
-
getCommands
public Map<String,JCommander> getCommands()
-
getRawCommands
public Map<JCommander.ProgramName,JCommander> getRawCommands()
-
getParsedCommand
public String getParsedCommand()
-
getParsedAlias
public String getParsedAlias()
The name of the command or the alias in the form it was passed to the command line.nullif no command or alias was specified.- Returns:
- Name of command or alias passed to command line. If none passed:
null.
-
getObjects
public List<Object> getObjects()
- Returns:
- the objects that JCommander will fill with the result of parsing the command line.
-
findCommandByAlias
public JCommander findCommandByAlias(String commandOrAlias)
-
setVerbose
public void setVerbose(int verbose)
-
setCaseSensitiveOptions
public void setCaseSensitiveOptions(boolean b)
-
setAllowAbbreviatedOptions
public void setAllowAbbreviatedOptions(boolean b)
-
setAcceptUnknownOptions
public void setAcceptUnknownOptions(boolean b)
-
setAllowParameterOverwriting
public void setAllowParameterOverwriting(boolean b)
-
isParameterOverwritingAllowed
public boolean isParameterOverwritingAllowed()
-
setAtFileCharset
public void setAtFileCharset(Charset charset)
Sets the charset used to expand@files.- Parameters:
charset- the charset
-
-