com.beust.jcommander
Class JCommander

java.lang.Object
  extended by 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:
cbeust

Field Summary
static String DEBUG_PROPERTY
           
 
Constructor Summary
JCommander()
          Creates a new un-configured JCommander object.
JCommander(Object object)
           
JCommander(Object object, ResourceBundle bundle)
           
JCommander(Object object, ResourceBundle bundle, String... args)
           
JCommander(Object object, String... args)
           
 
Method Summary
 void 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)
           
 void addObject(Object object)
          Adds the provided arg object to the set of objects that this commander will parse arguments into.
 Object convertValue(ParameterDescription pd, String value)
           
 Object convertValue(Parameterized parameterized, Class type, String value)
           
<T> Class<? extends IStringConverter<T>>
findConverter(Class<T> cls)
           
 int getColumnSize()
           
 String getCommandDescription(String commandName)
           
 Map<String,JCommander> getCommands()
           
static com.beust.jcommander.internal.Console getConsole()
           
 ParameterDescription getMainParameter()
           
 String getMainParameterDescription()
           
 List<Object> getObjects()
           
 List<ParameterDescription> getParameters()
           
 String getParsedAlias()
          The name of the command or the alias in the form it was passed to the command line.
 String getParsedCommand()
           
 void parse(String... args)
          Parse and validate the command line parameters.
 void parseWithoutValidation(String... args)
          Parse the command line parameters without validating them.
 void setColumnSize(int columnSize)
           
 void setDefaultProvider(IDefaultProvider defaultProvider)
          Define the default provider for this instance.
 void setDescriptionsBundle(ResourceBundle bundle)
          Sets the ResourceBundle to use for looking up descriptions.
 void setParameterDescriptionComparator(Comparator<? super ParameterDescription> c)
           
 void setProgramName(String name)
          Set the program name (used only in the usage).
 void setProgramName(String name, String... aliases)
          Set the program name
 void usage()
          Display the help on System.out.
 void usage(String commandName)
          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".
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

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 contain Parameter annotations.

JCommander

public JCommander(Object object,
                  ResourceBundle bundle)
Parameters:
object - The arg object expected to contain Parameter annotations.
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 contain Parameter annotations.
bundle - The bundle to use for the descriptions. Can be null.
args - The arguments to parse (optional).

JCommander

public JCommander(Object object,
                  String... args)
Parameters:
object - The arg object expected to contain Parameter annotations.
args - The arguments to parse (optional).
Method Detail

getConsole

public static com.beust.jcommander.internal.Console getConsole()

addObject

public final void addObject(Object object)
Adds the provided arg object to the set of objects that this commander will parse arguments into.

Parameters:
object - The arg object expected to contain Parameter annotations. If object is an array or is Iterable, the child objects will be added instead.

setDescriptionsBundle

public final void setDescriptionsBundle(ResourceBundle bundle)
Sets the ResourceBundle to use for looking up descriptions. Set this to null to 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.


getMainParameterDescription

public String getMainParameterDescription()

setProgramName

public void setProgramName(String name)
Set the program name (used only in the usage).


setProgramName

public void setProgramName(String name,
                           String... aliases)
Set the program name

Parameters:
name - program name
aliases - aliases to the program name

usage

public void usage(String commandName)
Display the usage for this command.


usage

public void usage(String commandName,
                  StringBuilder out)
Store the help for the command in the passed string builder.


usage

public void usage(String commandName,
                  StringBuilder out,
                  String indent)
Store the help for the command in the passed string builder, indenting every line with "indent".


getCommandDescription

public String getCommandDescription(String commandName)
Returns:
the description of the command.

usage

public void usage()
Display the help on System.out.


usage

public void usage(StringBuilder out)
Store the help in the passed string builder.


usage

public void usage(StringBuilder out,
                  String indent)

setParameterDescriptionComparator

public void setParameterDescriptionComparator(Comparator<? super ParameterDescription> c)

setColumnSize

public void setColumnSize(int columnSize)

getColumnSize

public int getColumnSize()

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).

getMainParameter

public ParameterDescription getMainParameter()
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)

findConverter

public <T> Class<? extends IStringConverter<T>> findConverter(Class<T> cls)

convertValue

public Object convertValue(ParameterDescription pd,
                           String value)

convertValue

public Object convertValue(Parameterized parameterized,
                           Class type,
                           String value)
Parameters:
field - The field
type - The type of the actual parameter
value - The value to convert

addCommand

public void addCommand(String name,
                       Object object)
Add a command object.


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()

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. null if 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.


Copyright © 2012. All Rights Reserved.