FreeHEP API
Version current

org.freehep.util.commandline
Class CommandLine

java.lang.Object
  extended byorg.freehep.util.commandline.CommandLine

public class CommandLine
extends Object

CommandLine parses a command line for parameters and options.

Version:
$Id: CommandLine.java,v 1.12 2004/01/19 21:23:58 duns Exp $
Author:
Mark Donszelmann (CommandLine)
Source Code:
CommandLine.java

Nested Class Summary
static class CommandLine.Option
          Keeps option names and types.
static class CommandLine.Parameter
          Keeps mandatory and optional parameter names.
 
Constructor Summary
CommandLine(String name, String description, int numberOfParameters)
          Creates a CommandLine object to be used for parsing a set of arguments
CommandLine(String name, String description, int numberOfParameters, boolean multiLevel)
          Creates a CommandLine object to be used for parsing a set of arguments
 
Method Summary
 void addBailOutOption(String longName, String shortName, String comment)
          Adds an option to be recognized by parse, without doing parameter or option checking.
 void addMultiOption(String name, String valueDescription, String comment)
          Adds an option to be recognized by parse.
 void addOption(String longName, String shortName, String comment)
          Adds an option to be recognized by parse.
 void addOption(String longName, String shortName, String[] qualifiers, String comment)
          Adds an option to be recognized by parse.
 void addOption(String longName, String shortName, String valueDescription, String comment)
          Adds an option to be recognized by parse.
 void addParameter(String name, String comment)
          Adds a parameter to be recognized by parse.
 void enableChecking(boolean check)
          enables checking of the options and parameters
 String getArgument(String name)
           
 String[] getArguments()
           
 String getDescription()
           
 CommandLineException getException()
           
 String getHelp()
          Get the help message, suitable for printing in response to a -help option
 Vector getMultiOption(String name)
           
 String getName()
           
 String getOption(String name)
           
 String getOption(String name, String defaultValue)
           
 Enumeration getOptions()
           
 Enumeration getParameters()
           
 String[] getUnparsedArguments()
           
 boolean hasOption(String name)
           
 boolean isLegalOption(String name)
           
 boolean parse(String[] args)
          Parses the supplied arguments.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

CommandLine

public CommandLine(String name,
                   String description,
                   int numberOfParameters,
                   boolean multiLevel)
Creates a CommandLine object to be used for parsing a set of arguments

Parameters:
name - name of the command
description - description of the command
numberOfParameters - minumum number of parameters
multiLevel - forces the command line to be multi-level (i.e. does not check for max number of parameters

CommandLine

public CommandLine(String name,
                   String description,
                   int numberOfParameters)
Creates a CommandLine object to be used for parsing a set of arguments

Parameters:
name - name of the command
description - description of the command
numberOfParameters - minumum number of parameters
Method Detail

enableChecking

public void enableChecking(boolean check)
enables checking of the options and parameters

Parameters:
check - sets checking

addOption

public void addOption(String longName,
                      String shortName,
                      String comment)
Adds an option to be recognized by parse.

Parameters:
longName - the full name of the option
shortName - the short name of the option (may be set to null)
comment - description of the arguments to the option

addOption

public void addOption(String longName,
                      String shortName,
                      String[] qualifiers,
                      String comment)
Adds an option to be recognized by parse.

Parameters:
longName - the full name of the option
shortName - the short name of the option (may be set to null)
qualifiers - gives a list of qualifiers you can use
comment - description of the arguments to the option

addOption

public void addOption(String longName,
                      String shortName,
                      String valueDescription,
                      String comment)
Adds an option to be recognized by parse.

Parameters:
longName - the full name of the option
shortName - the short name of the option (may be set to null)
valueDescription - description of the value
comment - description of the arguments to the option

addBailOutOption

public void addBailOutOption(String longName,
                             String shortName,
                             String comment)
Adds an option to be recognized by parse, without doing parameter or option checking. This is normally used for options such as -? -help and -version

Parameters:
longName - the full name of the option
shortName - the short name of the option (may be set to null)
comment - description of the arguments to the option

addMultiOption

public void addMultiOption(String name,
                           String valueDescription,
                           String comment)
Adds an option to be recognized by parse. This option can be specified multiple times on the commandline, and is immediately followed by its value. Usage (-Iincludedir)

Parameters:
name - the full name of the option (preferably one character)
valueDescription - what the value after the option means
comment - description of the argument to the option

addParameter

public void addParameter(String name,
                         String comment)
Adds a parameter to be recognized by parse.

Parameters:
name - name of the parameter
comment - comment describing the parameter

getName

public String getName()
Returns:
name of the command

getDescription

public String getDescription()
Returns:
description of the command

getOptions

public Enumeration getOptions()
Returns:
an enumeration of possible options (CommandLine.Option)

getParameters

public Enumeration getParameters()
Returns:
an enumeration of mandatory and optional parameters (CommandLine.Parameter)

isLegalOption

public boolean isLegalOption(String name)
Returns:
true if the specified option is legal

parse

public boolean parse(String[] args)
              throws CommandLineException
Parses the supplied arguments.

Parameters:
args - arguments for the command, including all options
Returns:
true if the arguments were parsed correctly. False if a bailOut option was specified.
Throws:
CommandLineException - in case the parsing failed.

getException

public CommandLineException getException()
Returns:
the last thrown exception

toString

public String toString()
Returns:
a textual representation of the parsed arguments

getOption

public String getOption(String name)
Parameters:
name - name of the option
Returns:
the value of a specified option. null is returned in case the option was not specified. true if this option was a flag.

getMultiOption

public Vector getMultiOption(String name)
Parameters:
name - name of the option
Returns:
the value of a specified option. An empty vector is returned in case the option was not specified.

getOption

public String getOption(String name,
                        String defaultValue)
Parameters:
name - name of the option
defaultValue - default value if option was not set
Returns:
the value of a specified option. null is returned in case the option was not specified. true if this option was a flag.

hasOption

public boolean hasOption(String name)
Parameters:
name - name of the option
Returns:
true if the option was specified.

getArgument

public String getArgument(String name)
Parameters:
name - name of the argument
Returns:
a named argument. null in case the named argument was not supplied.

getArguments

public String[] getArguments()
Returns:
string array of parameters

getUnparsedArguments

public String[] getUnparsedArguments()
Returns:
straing array of parameters which were not used by the command line

getHelp

public String getHelp()
Get the help message, suitable for printing in response to a -help option


FreeHEP API
Version current

Copyright © 2000-2004 FreeHEP, All Rights Reserved.