org.apache.commons.cli

Class CommandLine

public class CommandLine extends Object

Represents list of arguments parsed against a {@link Options} descriptor.

It allows querying of a boolean {@link #hasOption(String opt)}, in addition to retrieving the {@link #getOptionValue(String opt)} for options requiring arguments.

Additionally, any left-over or unrecognized arguments, are available for further processing.

Author: bob mcwhirter (bob @ werken.com) James Strachan John Keyes (john at integralsource.com)

Field Summary
Listargs
the unrecognised options/arguments
MaphashcodeMap
Map of unique options for ease to get complete list of options
Mapoptions
the processed options
Option[]optionsArray
the processed options
Constructor Summary
CommandLine()

Creates a command line.

Method Summary
voidaddArg(String arg)

Add left-over unrecognized option/argument.

voidaddOption(Option opt)

Add an option to the command line.

ListgetArgList()

Retrieve any left-over non-recognized options and arguments

String[]getArgs()

Retrieve any left-over non-recognized options and arguments

ObjectgetOptionObject(String opt)

Return the Object type of this Option.

ObjectgetOptionObject(char opt)

Return the Object type of this Option.

Option[]getOptions()

Returns an array of the processed {@link Option}s.

StringgetOptionValue(String opt)

Retrieve the argument, if any, of this option.

StringgetOptionValue(char opt)

Retrieve the argument, if any, of this option.

StringgetOptionValue(String opt, String defaultValue)

Retrieve the argument, if any, of an option.

StringgetOptionValue(char opt, String defaultValue)

Retrieve the argument, if any, of an option.

String[]getOptionValues(String opt)

Retrieves the array of values, if any, of an option.

String[]getOptionValues(char opt)

Retrieves the array of values, if any, of an option.

booleanhasOption(String opt)

Query to see if an option has been set.

booleanhasOption(char opt)

Query to see if an option has been set.

Iteratoriterator()

Returns an iterator over the Option members of CommandLine.

Field Detail

args

private List args
the unrecognised options/arguments

hashcodeMap

private Map hashcodeMap
Map of unique options for ease to get complete list of options

options

private Map options
the processed options

optionsArray

private Option[] optionsArray
the processed options

Constructor Detail

CommandLine

CommandLine()

Creates a command line.

Method Detail

addArg

void addArg(String arg)

Add left-over unrecognized option/argument.

Parameters: arg the unrecognised option/argument.

addOption

void addOption(Option opt)

Add an option to the command line. The values of the option are stored.

Parameters: opt the processed option

getArgList

public List getArgList()

Retrieve any left-over non-recognized options and arguments

Returns: remaining items passed in but not parsed as a List.

getArgs

public String[] getArgs()

Retrieve any left-over non-recognized options and arguments

Returns: remaining items passed in but not parsed as an array

getOptionObject

public Object getOptionObject(String opt)

Return the Object type of this Option.

Parameters: opt the name of the option

Returns: the type of this Option

getOptionObject

public Object getOptionObject(char opt)

Return the Object type of this Option.

Parameters: opt the name of the option

Returns: the type of opt

getOptions

public Option[] getOptions()

Returns an array of the processed {@link Option}s.

Returns: an array of the processed {@link Option}s.

getOptionValue

public String getOptionValue(String opt)

Retrieve the argument, if any, of this option.

Parameters: opt the name of the option

Returns: Value of the argument if option is set, and has an argument, otherwise null.

getOptionValue

public String getOptionValue(char opt)

Retrieve the argument, if any, of this option.

Parameters: opt the character name of the option

Returns: Value of the argument if option is set, and has an argument, otherwise null.

getOptionValue

public String getOptionValue(String opt, String defaultValue)

Retrieve the argument, if any, of an option.

Parameters: opt name of the option defaultValue is the default value to be returned if the option is not specified

Returns: Value of the argument if option is set, and has an argument, otherwise defaultValue.

getOptionValue

public String getOptionValue(char opt, String defaultValue)

Retrieve the argument, if any, of an option.

Parameters: opt character name of the option defaultValue is the default value to be returned if the option is not specified

Returns: Value of the argument if option is set, and has an argument, otherwise defaultValue.

getOptionValues

public String[] getOptionValues(String opt)

Retrieves the array of values, if any, of an option.

Parameters: opt string name of the option

Returns: Values of the argument if option is set, and has an argument, otherwise null.

getOptionValues

public String[] getOptionValues(char opt)

Retrieves the array of values, if any, of an option.

Parameters: opt character name of the option

Returns: Values of the argument if option is set, and has an argument, otherwise null.

hasOption

public boolean hasOption(String opt)

Query to see if an option has been set.

Parameters: opt Short name of the option

Returns: true if set, false if not

hasOption

public boolean hasOption(char opt)

Query to see if an option has been set.

Parameters: opt character name of the option

Returns: true if set, false if not

iterator

public Iterator iterator()

Returns an iterator over the Option members of CommandLine.

Returns: an Iterator over the processed {@link Option} members of this {@link CommandLine}