fr.umlv.tatoo.cc.common.main
Class CommandLineParser<D>

java.lang.Object
  extended by fr.umlv.tatoo.cc.common.main.CommandLineParser<D>
Type Parameters:
D - type of the user data.
All Implemented Interfaces:
OptionRegistry<D>

public class CommandLineParser<D>
extends Object
implements OptionRegistry<D>

The command line parser : - permits to add commands by using CommandLineParser(commands) or addCommand(commands). Each command contains a description and register itself as command for a set of options. When the command line parser find an option, it calls the associated command method execute(option,userData,argument) with the arguments of the option extracted from the command line. - permits to directly register an option with the associated command using registerOption(String, Command, int). Note : it's better to let the command register itself by adding command to the command line parser. - parses arguments using the command descriptions parse(userData,argumentList) or parse(userData,arguments. - returns usage by delegating to UsageFormatter

Author:
remi

Nested Class Summary
static interface CommandLineParser.Option
          Option information.
 
Constructor Summary
CommandLineParser(Command<? super D>... commands)
          Create a command line parser with some commands.
CommandLineParser(String prefix, Command<? super D>... commands)
          Create a command line parser with a default option prefix and some commands.
CommandLineParser(String prefix, UsageFormatter formatter, Command<? super D>... commands)
          Creates a command line parser with a default option prefix, a formatter and some commands.
CommandLineParser(UsageFormatter formatter, Command<? super D>... commands)
          Create a command line parser with a usage formatter and some commands.
 
Method Summary
 void addCommands(Command<? super D>... commands)
          Add commands to the current command line parser.
 List<? extends String> parse(D userData, List<? extends String> args)
          Parse a command line arguments and call the command corresponding to the registered option.
 List<? extends String> parse(D userData, String... args)
          Parse a command line arguments and call the command corresponding to the registered option.
 void registerOption(String optionName, Command<? super D> command, int numberOfArgument)
          register an option with a name a command to execute and a number of option argument.
 void registerOption(String prefix, String optionName, Command<? super D> command, int numberOfArgument)
          register an option with a specific prefix, a name a command to execute and a number of option argument.
 String usage(String comment)
          Returns the usage of the command line.
 String usage(String comment, int depth, int optionLength)
          Returns the usage of the command line.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CommandLineParser

public CommandLineParser(Command<? super D>... commands)
Create a command line parser with some commands.

Parameters:
commands - the commands

CommandLineParser

public CommandLineParser(String prefix,
                         Command<? super D>... commands)
Create a command line parser with a default option prefix and some commands.

Parameters:
prefix - default prefix for option that doesn't specify a prefix, see addOption(optionName,command,numberOfArgument).
commands - the commands

CommandLineParser

public CommandLineParser(UsageFormatter formatter,
                         Command<? super D>... commands)
Create a command line parser with a usage formatter and some commands. This constructor is equivalent to CommandLineParser("",formatter,commands.

Parameters:
formatter -
commands - the commands

CommandLineParser

public CommandLineParser(String prefix,
                         UsageFormatter formatter,
                         Command<? super D>... commands)
Creates a command line parser with a default option prefix, a formatter and some commands.

Parameters:
prefix - the default prefix for option
formatter - the usage formatter
commands - the commands
See Also:
addCommands(Command[])
Method Detail

addCommands

public void addCommands(Command<? super D>... commands)
Add commands to the current command line parser. This method delagates to each command the fact to register itself their options.

Parameters:
commands -

registerOption

public void registerOption(String optionName,
                           Command<? super D> command,
                           int numberOfArgument)
Description copied from interface: OptionRegistry
register an option with a name a command to execute and a number of option argument. The prefix of the option is the default prefix of the command line parser see CommandLineParser(defaultPrefix,usageFormatter,commands).

Specified by:
registerOption in interface OptionRegistry<D>
Parameters:
optionName - the name of the option.
command - the command associated with the option.
numberOfArgument - hte number of argument of the option.

registerOption

public void registerOption(String prefix,
                           String optionName,
                           Command<? super D> command,
                           int numberOfArgument)
Description copied from interface: OptionRegistry
register an option with a specific prefix, a name a command to execute and a number of option argument.

Specified by:
registerOption in interface OptionRegistry<D>
Parameters:
prefix - the specific prefix of the option.
optionName - the name of the option.
command - the command associated with the option.
numberOfArgument - hte number of argument of the option.
See Also:
OptionRegistry.registerOption(String, Command, int)

parse

public List<? extends String> parse(D userData,
                                    String... args)
                             throws IllegalCommandLineArgumentException
Parse a command line arguments and call the command corresponding to the registered option. When an option is found the method doIt(optionName,userData,optionArguments) of the associated command is called with the user data.

Parameters:
userData - the user data.
args - arguments received by the method main().
Returns:
a list of unrecognized arguments.
Throws:
IllegalCommandLineArgumentException - if an argument starts with a registered prefix or the default prefix and is not a registered option.

parse

public List<? extends String> parse(D userData,
                                    List<? extends String> args)
                             throws IllegalCommandLineArgumentException
Parse a command line arguments and call the command corresponding to the registered option. When an option is found the method doIt(optionName,userData,optionArguments) of the associated command is called with the user data.

Parameters:
userData - the user data.
args - arguments received by the method main().
Returns:
a list of unrecognized arguments.
Throws:
IllegalCommandLineArgumentException - if an argument starts with a registered prefix or the default prefix and is not a registered option.

usage

public String usage(String comment)
Returns the usage of the command line. The implementation of this method is equivalent to a call to usage(comment,DEFAULT_DEPTH,DEFAULT_LENGTH).

Parameters:
comment - general comment of the program.
Returns:
the formatted usage
See Also:
UsageFormatter.usage(String, Map, int, int), UsageFormatter.DEFAULT_DEPTH, UsageFormatter.DEFAULT_OPTION_LENGTH

usage

public String usage(String comment,
                    int depth,
                    int optionLength)
Returns the usage of the command line.

Parameters:
comment - general comment of the program.
depth - shift in character for each line of the usage
optionLength - size in caracter reserved for printing option information
Returns:
the formatted usage
See Also:
UsageFormatter.usage(String, Map, int, int)