Top Description Inners Fields Constructors Methods
com.beust.jcommander

public Class JCommander

extends Object
Class Inheritance
Imports
com.beust.jcommander.parser.DefaultParameterizedParser, com.beust.jcommander.FuzzyMap.IKey, com.beust.jcommander.converters.*, com.beust.jcommander.internal.*, java.io.BufferedReader, .IOException, java.lang.reflect.*, java.nio.charset.Charset, java.nio.file.Files, .Paths, java.util.*, .ResourceBundle, java.util.concurrent.CopyOnWriteArrayList

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 <[email protected]>

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
private class
pack-priv static class
JCommander.MainParameter

Description of a main parameter, which can be either a list of string or a single field.

private static class
JCommander.Options

Options shared with sub commands

public static class
JCommander.ProgramName

Encapsulation of either a main application or an individual command.

Field Summary

Modifier and TypeField and Description
private Map<FuzzyMap.IKey, JCommander.ProgramName>
aliasMap

Alias database for reverse lookup

private Map<JCommander.ProgramName, JCommander>
commands

List of commands and their instance.

private Console
public static final String
private final IVariableArity
private Map<FuzzyMap.IKey, ParameterDescription>
descriptions

A map to look up parameter description per option name.

private Map<Parameterized, ParameterDescription>
fields

A map of all the parameterized fields/methods.

private boolean
private JCommander.MainParameter
private List<Object>
objects

The objects that contain fields annotated with @Parameter.

private final JCommander.Options
protected IParameterizedParser
private String
parsedAlias

The name of command or alias as it was passed to the command line

private String
parsedCommand

The name of the command after the parsing has run.

private JCommander.ProgramName
private Map<Parameterized, ParameterDescription>
requiredFields

A set of all the parameterizeds that are required.

private List<String>
private IUsageFormatter
usageFormatter

The usage formatter to use in usage().

Constructor Summary

AccessConstructor and Description
private
public
JCommander()

Creates a new un-configured JCommander object.

public
JCommander(Object
The arg object expected to contain Parameter annotations.
object
)

public
JCommander(Object
The arg object expected to contain Parameter annotations.
object
,
ResourceBundle
The bundle to use for the descriptions. Can be null.
bundle
)

public
JCommander(Object
The arg object expected to contain Parameter annotations.
object
,
ResourceBundle
The bundle to use for the descriptions. Can be null.
bundle
,
String...
The arguments to parse (optional).
args
)

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

Deprecated Construct a JCommander instance first and then call parse() on it.

Method Summary

Modifier and TypeMethod and Description
public void
addCommand(String name, Object object)

Add a command object.

public void
public void
addCommand(String name, Object object, String... aliases)

Add a command object and its aliases.

public void
addConverterFactory(final IStringConverterFactory
the factory determining string converters
converterFactory
)

Adds a factory to lookup string converters.

public void
addConverterInstanceFactory(IStringConverterInstanceFactory
the factory generating string converter instances
converterInstanceFactory
)

Adds a factory to lookup string converters.

private void
public final void
addObject(Object
The arg object expected to contain Parameter annotations. If object is an array or is Iterable, the child objects will be added instead.
object
)

Adds the provided arg object to the set of objects that this commander will parse arguments into.

public Object
convertValue(final Parameterized parameterized, Class<T>
The type of the actual parameter
type
,
String optionName, String
The value to convert
value
)

public void
createDescriptions()

Create the ParameterDescriptions for all the \@Parameter found.

private final int
private String[]

Returns:

the new and enriched command line parameters
expandArgs
(String[]
the original command line parameters
originalArgv
)

Expand the command line parameters to take @ parameters into account.

private List<String>
private JCommander
public JCommander
findCommandByAlias(String commandOrAlias)

private IStringConverter<?>
findConverterInstance(Parameter parameter, Class<?> forType, String optionName)

private ParameterDescription
private JCommander.ProgramName
public ResourceBundle
public int
public Map<String, JCommander>
public synchronized Console

Returns:

a wrapper for a java.io.PrintStream, typically System#out.
getConsole
()

private ParameterDescription
getDescriptionFor(String arg)

If arg is an option, we can look it up directly, but if it's a value, we need to find the description for the option that precedes it.

public Map<FuzzyMap.IKey, ParameterDescription>
public Map<Parameterized, ParameterDescription>
public JCommander.MainParameter
public String
public ParameterDescription

Returns:

the main parameter description or null if none is defined.
getMainParameterValue
()

public List<Object>

Returns:

the objects that JCommander will fill with the result of parsing the command line.
getObjects
()

public JCommander.Options
public Comparator<? super ParameterDescription>
public List<ParameterDescription>

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

public String

Returns:

Name of command or alias passed to command line. If none passed: null.
getParsedAlias
()

The name of the command or the alias in the form it was passed to the command line.

public String
private ParameterDescription
public String
getProgramDisplayName()

Get the program display name (used only in the usage).

public String
getProgramName()

Get the program name (used only in the usage).

public Map<JCommander.ProgramName, JCommander>
private String
public List<String>
public IUsageFormatter

Returns:

the usage formatter
getUsageFormatter
()

Returns the usage formatter.

private void
private void
private void
private void
initMainParameterValue(String arg)

Init the main parameter with the given arg.

private static <T> T
instantiateConverter(String optionName, Class<? extends T> converterClass)

private boolean
isBooleanType(Class<?> fieldType)

private boolean
isOption(String passedArg)

public boolean
private boolean
public static JCommander.Builder
private void
p(String string)

public void
parse(String... args)

Parse and validate the command line parameters.

private void
parse(boolean validate, String... args)

private void
parseValues(String[] args, boolean validate)

Main method that parses the values and initializes the fields accordingly.

public void
parseWithoutValidation(String... args)

Parse the command line parameters without validating them.

private static String
pluralize(int quantity, String singular, String plural)

private int
processFixedArity(String[] args, int index, ParameterDescription pd, boolean validate, Class<?> fieldType)

private int
processFixedArity(String[] args, int originalIndex, ParameterDescription pd, boolean validate, Class<?> fieldType, int arity)

private int

Returns:

the number of options that were processed.
processPassword
(String[] args, int index, ParameterDescription pd, boolean validate)

private int

Returns:

the number of options that were processed.
processVariableArity
(String[] args, int index, ParameterDescription pd, boolean validate)

private List<String>

Returns:

the file content as a string.
readFile
(String
the command line filename
fileName
)

Reads the file specified by filename and returns the file content as a string.

private char[]
readPassword(String description, boolean echoInput)

Invoke Console.readPassword through reflection to avoid depending on Java 6.

private String

Returns:

n spaces
s
(int count)

public void
public void
public void
public void
setAtFileCharset(Charset
the charset
charset
)

Sets the charset used to expand @files.

public void
public void
setColumnSize(int columnSize)

public void
public void
setDefaultProvider(IDefaultProvider defaultProvider)

Define the default provider for this instance.

public final void
setDescriptionsBundle(ResourceBundle bundle)

Sets the ResourceBundle to use for looking up descriptions.

public void
setExpandAtSign(boolean expandAtSign)

Disables expanding @file.

public void
public void
public void
setProgramName(String name)

Set the program name (used only in the usage).

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

Set the program name

public void
setUsageFormatter(IUsageFormatter
the usage formatter
usageFormatter
)

Sets the usage formatter.

public void
setVerbose(int verbose)

private String[]
subArray(String[] args, int index)

private static String
trim(String string)

Remove spaces at both ends and handle double quotes.

private static <T> T
tryInstantiateConverter(String optionName, Class<T> converterClass)

public void
usage()

Prints the usage on getConsole() using the underlying usageFormatter.

private void
validateOptions()

Make sure that all the required parameters have received a value.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait