org.jcon.util
Class CommandLineSyntax

java.lang.Object
  extended by org.jcon.util.CommandLineSyntax
All Implemented Interfaces:
java.io.Serializable

public class CommandLineSyntax
extends java.lang.Object
implements java.io.Serializable

Describes the syntax for a command-line tool.

Supports the definition of a command-line syntax much like that provided by the GNU command-line utilities found through the UNIX/Linux world.

Interprets these for your program, substituting defaults and such, and spares the annoyance of doing lots of processing in your main() to figure out what the user wants.

Author:
Paul Reavis
See Also:
Serialized Form

Constructor Summary
CommandLineSyntax()
           
CommandLineSyntax(java.util.ArrayList arguments, java.util.ArrayList options)
           
 
Method Summary
 java.lang.String buildHelpMessage()
          Builds a help message describing the syntax.
 void die(java.lang.String reason)
          Exits with a handy information message.
 java.util.ArrayList getArguments()
           
 PropMap getDefaultProperties()
           
 CommandLineOption getOptionFor(java.lang.String flag)
          Looks up the option specified by the given String (which has a "-" or "--" prefix)
 java.util.ArrayList getOptions()
           
 java.lang.String getSynopsis()
           
 java.lang.String getVarargsName()
          If a list of args appears at the end of the syntax, this is the name that describes it.
 PropMap parse(java.lang.String[] commandLine)
          This is where it all happens.
 PropMap parseOrDie(java.lang.String[] commandLine)
          Alternately, this is where it all happens.
 void setArguments(java.util.ArrayList newArguments)
           
 void setOptions(java.util.ArrayList newOptions)
           
 void setSynopsis(java.lang.String newSynopsis)
           
 void setVarargsName(java.lang.String newVarargsName)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CommandLineSyntax

public CommandLineSyntax()

CommandLineSyntax

public CommandLineSyntax(java.util.ArrayList arguments,
                         java.util.ArrayList options)
Method Detail

parse

public PropMap parse(java.lang.String[] commandLine)
              throws CommandLineParseException
This is where it all happens.

Parses a command line argument array using the syntax; returns a Map keyed by option or argument name containing the value (boolean for switches, String for others) obtained or defaulted for each.

Throws:
CommandLineParseException

parseOrDie

public PropMap parseOrDie(java.lang.String[] commandLine)
Alternately, this is where it all happens.

This is a convenience method that takes care of the quite common case where you want to simply exit if the command line is unparseable; printing out a nice help message so the user knows better next time.

Also, if you have defined a -h, --help option, it will print the nice help message as well.

Variable-length args (e.g. a list of things at the end of the command) are provided in a property called "remainingArgs".


die

public void die(java.lang.String reason)
Exits with a handy information message.


getOptionFor

public CommandLineOption getOptionFor(java.lang.String flag)
Looks up the option specified by the given String (which has a "-" or "--" prefix)


buildHelpMessage

public java.lang.String buildHelpMessage()
Builds a help message describing the syntax.


getArguments

public java.util.ArrayList getArguments()

setArguments

public void setArguments(java.util.ArrayList newArguments)

getOptions

public java.util.ArrayList getOptions()

setOptions

public void setOptions(java.util.ArrayList newOptions)

getSynopsis

public java.lang.String getSynopsis()

setSynopsis

public void setSynopsis(java.lang.String newSynopsis)

getVarargsName

public java.lang.String getVarargsName()
If a list of args appears at the end of the syntax, this is the name that describes it.


setVarargsName

public void setVarargsName(java.lang.String newVarargsName)

getDefaultProperties

public PropMap getDefaultProperties()