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

Author:
cbeust

Field Summary
static String DEBUG_PROPERTY
           
 
Constructor Summary
JCommander(Object object)
           
JCommander(Object object, ResourceBundle bundle, String... args)
           
JCommander(Object object, String... args)
           
 
Method Summary
 void addCommand(String name, Object object)
          Add a command object.
 void addConverterFactory(IStringConverterFactory converterFactory)
           
 Object convertValue(Field field, Class type, String value)
           
 Object convertValue(ParameterDescription pd, String value)
           
<T> Class<? extends IStringConverter<T>>
findConverter(Class<T> cls)
           
 String getCommandDescription(String commandName)
           
 Map<String,JCommander> getCommands()
           
 List<Object> getObjects()
           
 List<ParameterDescription> getParameters()
           
 String getParsedCommand()
           
 void parse(String... args)
          Parse the command line parameters.
 void setDefaultProvider(IDefaultProvider defaultProvider)
          Define the default provider for this instance.
 void setProgramName(String name)
          Set the program name (used only in the usage).
 void usage()
          Display a 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(String commandName, StringBuilder out)
          Store the help for the command in the passed string builder.
 
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(Object object)
Parameters:
object - The arg object expected to contain @Parameter annotations.

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

parse

public void parse(String... args)
Parse the command line parameters.


setProgramName

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


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.


getCommandDescription

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

usage

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


usage

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


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

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(Field field,
                           Class type,
                           String value)
Parameters:
type - The class of the field
annotation - The annotation
value - The value to convert

addCommand

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


getCommands

public Map<String,JCommander> getCommands()

getParsedCommand

public String getParsedCommand()

getObjects

public List<Object> getObjects()
Returns:
the objects that JCommander will fill with the result of parsing the command line.


Copyright © 2010. All Rights Reserved.