util
Class CommandLineParser

java.lang.Object
  extended by util.CommandLineParser
Direct Known Subclasses:
AdapterReduce, AduHorizontalFit, AmplifierCrosstalk.Simul, BeamSplitterFit.FitsFile, ClearRegistry.Now, ClusterAccess, ColumnCalculator, CommandLineTarget, DailyDumpRepository.Parser, DataStreamConvert, FitsTools.Stitcher, GenericAccess, GenericInvoke, HistoryDatabase.Logfile, IsoMaker, JGuiderOffsets.GuiderFile, JTrackingFrequencies.GuiderFile, ParameterClient, PoissonRandom.SimulFits, PropertyParser, RaDeParser, SchedulerAccess, Seeing.Motion, SensorTest.Parser, SimulateFits.Generate, SineError.Parse, SolarHeightCalculator, StatusAccess, SwitchSkeleton.Turn, SyslogAdmin, TelescopeAxis.Randomize, TelescopeError.Parse, Toc, WcsFit.Parser, WeatherStation.Manage

public class CommandLineParser
extends Object

A class to parse command line arguments. All command line switches identified are stored in the appropriate fields, regardless of their appearance within the command line. Command line arguments that can not be identified as command line switches or their arguments are stored in their order of appearance in a separate list. Note that prior to parsing a list of command line switches must be registered.


Nested Class Summary
static class CommandLineParser.Test
          Test purpose only.
 
Field Summary
private  List<String> argument
          The list of unmatched command line arguments.
private  Map<Integer,Set<String>> countToSwitch
          A table linking command line switches to their element numbers.
private  String line
          The concated command line arguments, available after parsing.
private  Map<String,List<String>> switchFound
          The table of found command line switches.
 
Constructor Summary
CommandLineParser()
          Constructs a new command line argument parser.
 
Method Summary
 List<String> getArguments(String toswitch)
          Returns the arguments associated with the given command line switch.
protected  String getCommandLine()
          Returns the original command line.
 List<String> getCommands()
          Returns the true arguements of the command lines.
protected  Set<String> getRegisteredSwitches(int num)
          Returns all registered switches to the given argument count.
 boolean hasAnySwitch(List<String> all)
          Checks, if we have any of these switches.
 boolean hasSwitch()
          Returns true if at least one of the command line switches has been found.
 boolean hasSwitch(String switchname)
          Returns true if at least one of the command line switches has been found.
 boolean haveAllSwitches(List<String> all)
          Checks, if we have all switches.
 void parse(String[] commandline)
          Parses the command line arguments.
 void printCommandLine(PrintStream out, String[] arg)
          Prints the command line as a hashed string to the specified stream.
 void registerLine(String line, int count)
          Convenience method to add command line switches gather together on a single string.
 void registerSwitches(Set<String> switches, int count)
          Register a set of command line switches to this parser.
 void setCommands(List<String> set)
          Sets the non-switches of the command lines, i.e.
static void staticUsage(Class parser, Map<Integer,Set<String>> switches, Map<String,String> detail, String footer)
          For classes that don't want to instantiate a command line parser, but still want to print the standardized usage.
 void usage(Map<String,String> detail, String footer)
          Dumps a usage message to System.out.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

countToSwitch

private Map<Integer,Set<String>> countToSwitch
A table linking command line switches to their element numbers.


switchFound

private Map<String,List<String>> switchFound
The table of found command line switches.


argument

private List<String> argument
The list of unmatched command line arguments.


line

private String line
The concated command line arguments, available after parsing.

Constructor Detail

CommandLineParser

public CommandLineParser()
Constructs a new command line argument parser. Prior to use, at least one set of switches must be registered.

Method Detail

registerLine

public void registerLine(String line,
                         int count)
Convenience method to add command line switches gather together on a single string.


registerSwitches

public void registerSwitches(Set<String> switches,
                             int count)
Register a set of command line switches to this parser. The number of parameters that are required by these limit switches is also given. If a set of limit switches with the same number of command line arguments was already registered, the new set is added to the previously given one.


parse

public void parse(String[] commandline)
           throws ParseException
Parses the command line arguments.

Throws:
ParseException

printCommandLine

public void printCommandLine(PrintStream out,
                             String[] arg)
Prints the command line as a hashed string to the specified stream.


getArguments

public List<String> getArguments(String toswitch)
Returns the arguments associated with the given command line switch. If the switch takes no arguments, an empty list is returned. If the switch is not present in the command line, null is returned.


hasSwitch

public boolean hasSwitch()
Returns true if at least one of the command line switches has been found. This can only be done after parsing, which must be called explicetly before a call to this method.


hasSwitch

public boolean hasSwitch(String switchname)
Returns true if at least one of the command line switches has been found. This can only be done after parsing, which must be called explicetly before a call to this method.


haveAllSwitches

public boolean haveAllSwitches(List<String> all)
Checks, if we have all switches.


hasAnySwitch

public boolean hasAnySwitch(List<String> all)
Checks, if we have any of these switches.


getCommands

public List<String> getCommands()
Returns the true arguements of the command lines. These are arguements that could not be assigned to any switch. Note that this method never returns null, if no commands are given, an empty list is returned.


setCommands

public void setCommands(List<String> set)
Sets the non-switches of the command lines, i.e. the true arguments.


usage

public void usage(Map<String,String> detail,
                  String footer)
Dumps a usage message to System.out. If detailed information to a limit switch is found in the argumental hash-map it is additionally dumped.


staticUsage

public static void staticUsage(Class parser,
                               Map<Integer,Set<String>> switches,
                               Map<String,String> detail,
                               String footer)
For classes that don't want to instantiate a command line parser, but still want to print the standardized usage.

Parameters:
parser - The class to print the usage for.
switches - A mapping of argument numbers to sets of switches.
detail - Details of usage of switches
footer - What the commands mean.

getRegisteredSwitches

protected Set<String> getRegisteredSwitches(int num)
Returns all registered switches to the given argument count.


getCommandLine

protected String getCommandLine()
Returns the original command line.