jargs.gnu
Class CommandLineParser

java.lang.Object
  extended by jargs.gnu.CommandLineParser

public class CommandLineParser
extends java.lang.Object

Largely GNU-compatible command-line options parser. Has short (-v) and long-form (--verbose) option support, and also allows options with associated values (-d 2, --debug 2, --debug=2). Option processing can be explicitly terminated by the argument '--'.

Author:
Philipp Eichhorn, All JArgs authors see JARGS_LICENCE

Constructor Summary
CommandLineParser()
           
 
Method Summary
 AbstractOption<java.lang.Void> addHelpOption()
          Adds the help option.
<E> AbstractOption<E>
addOption(AbstractOption<E> option)
          Add the specified Option to the list of accepted options
<E> E
getOptionValue(AbstractOption<E> option)
          Equivalent to getOptionValue(option, null).
<E> E
getOptionValue(AbstractOption<E> option, E def)
           
<E> java.util.List<E>
getOptionValues(AbstractOption<E> option)
           
 java.io.PrintStream getPrintStream()
          Returns the PrintStream used for printUsage().
 java.lang.String[] getRemainingArgs()
           
<E> boolean
hasValues(AbstractOption<E> option)
           
 void parse(java.lang.String[] args)
          Equivalent to parse(args, Locale.getDefault()).
 void parse(java.lang.String[] args, java.util.Locale locale)
          Extract the options and non-option arguments from the given list of command-line arguments.
 void printUsage()
          Prints the usage-message
 void setApplicationName(java.lang.String applicationName)
          Sets the application name used in printUsage().
 void setApplicationNameFormJar()
          Sets the application name by looking into the first entry of the classpath.
 void setPrintStream(java.io.PrintStream out)
          Sets the PrintStream used for printUsage().
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CommandLineParser

public CommandLineParser()
Method Detail

addOption

public <E> AbstractOption<E> addOption(AbstractOption<E> option)
Add the specified Option to the list of accepted options

Type Parameters:
E - Type of options value.
Parameters:
option - The specified Option.
Returns:
The specified Option itself.

addHelpOption

public AbstractOption<java.lang.Void> addHelpOption()
Adds the help option.
 usage:
 
 CommandLineParser parser;
 AbstractOption help = parser.addHelpOption();
 parser.parse(args);
 if (!hasValues(help)) {
   // do something
 }
 
 same as:
 
 CommandLineParser parser;
 AbstractOption help = parser.addOption(new BooleanOption('h', "help", "display help"));
 parser.parse(args);
 if (hasValues(help)) {
   parser.printUsage();
 } else {
   // do something
 }
 

Returns:
the AbstractOption representing the help option

getOptionValue

public <E> E getOptionValue(AbstractOption<E> option)
Equivalent to getOptionValue(option, null).


getOptionValue

public <E> E getOptionValue(AbstractOption<E> option,
                            E def)
Type Parameters:
E - Type of options value.
Parameters:
option - The specified Option.
def - The default value.
Returns:
The parsed value of the specified Option, or a default value if the option was not set.

getOptionValues

public <E> java.util.List<E> getOptionValues(AbstractOption<E> option)
Type Parameters:
E - Type of options value.
Parameters:
option - The specified Option.
Returns:
A List giving the parsed values of all the occurrences of the given Option, or an empty List if the option was not set.

hasValues

public <E> boolean hasValues(AbstractOption<E> option)
Type Parameters:
E - Type of options value.
Parameters:
option - The specified Option.
Returns:
Status flag which indicates whether there are values for a given options or not.

getRemainingArgs

public java.lang.String[] getRemainingArgs()
Returns:
The arguments no option was specified for.

parse

public void parse(java.lang.String[] args)
           throws IllegalOptionValueException,
                  UnknownOptionException
Equivalent to parse(args, Locale.getDefault()).

Throws:
IllegalOptionValueException
UnknownOptionException

parse

public void parse(java.lang.String[] args,
                  java.util.Locale locale)
           throws IllegalOptionValueException,
                  UnknownOptionException
Extract the options and non-option arguments from the given list of command-line arguments. The specified locale is used for parsing options whose values might be locale-specific.

Parameters:
args - List of command-line arguments.
Throws:
IllegalOptionValueException
UnknownOptionException

setApplicationName

public void setApplicationName(java.lang.String applicationName)
Sets the application name used in printUsage(). The default value is 'appname'.


setApplicationNameFormJar

public void setApplicationNameFormJar()
Sets the application name by looking into the first entry of the classpath.


setPrintStream

public void setPrintStream(java.io.PrintStream out)
Sets the PrintStream used for printUsage().


getPrintStream

public java.io.PrintStream getPrintStream()
Returns the PrintStream used for printUsage().


printUsage

public void printUsage()
Prints the usage-message