PROTOTYPE
Not reviewed yet: use at your own risk!

satin.common
Class CommandLine

java.lang.Object
  |
  +--satin.common.CommandLine

public class CommandLine
extends java.lang.Object

A simple command line parser for use with the string array passed to main methods.

Author:
Ed Stauff

Inner Class Summary
static class CommandLine.SyntaxError
          Indicates a syntax error in a command line.
 
Constructor Summary
CommandLine(java.lang.String[] mainArgv)
          Constructs a command line object from the argument list from a main method.
 
Method Summary
int findArg(java.lang.String name)
          Searches the command line for the first occurrence of name and returns its index or -1.
void finish()
          Call this when argument parsing is finished.
boolean getArg(java.lang.String name)
          Gets an unvalued argument from the command line.
java.lang.String getArgValue(java.lang.String name, java.lang.String descr)
          Searches the command line for the first occurrence of name and a following value.
boolean getHelpArg()
          Searches the command line for all the common "help" switches: "-h", "-?", "/h", "/?", "-help", "/help".
int getIntArg(java.lang.String name, java.lang.String descr, int dflt)
          Gets an argument from the command line that has an integer value.
java.lang.String[] getRemaining()
          Returns the remaining (unparsed) arguments in the command line.
java.lang.String getStringArg(java.lang.String name, java.lang.String descr, java.lang.String dflt)
          Gets an argument from the command line that has a string value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CommandLine

public CommandLine(java.lang.String[] mainArgv)
Constructs a command line object from the argument list from a main method.
Method Detail

getRemaining

public java.lang.String[] getRemaining()
Returns the remaining (unparsed) arguments in the command line.

finish

public void finish()
            throws CommandLine.SyntaxError
Call this when argument parsing is finished. Throws a SyntaxError if there are any leftover arguments.

getArg

public boolean getArg(java.lang.String name)
Gets an unvalued argument from the command line. The command line is searched for the first occurrence of name. If it is found, it is removed from the command line and true is returned. Otherwise false is returned.

Parameters:
name
the argument to search for.
Returns:
true if found (and removed), false if not found.

getStringArg

public java.lang.String getStringArg(java.lang.String name,
                                     java.lang.String descr,
                                     java.lang.String dflt)
                              throws CommandLine.SyntaxError
Gets an argument from the command line that has a string value. The command line is searched for the first occurrence of name. If name is not found, dflt is returned. If name is found, the argument immediately following it is returned (without modification), and the argument and its value are removed from the command line. If there is no argument immediately following it, a SyntaxError is thrown.

Parameters:
name
the named argument to be searched for.
descr
a very short description of what the argument is, e.g. "file name", "search key", etc. This is used only for error reporting.
dflt
the default value to be returned if the argument is not found (may be null).
Returns:
the value of the argument if found, or the default value.

getIntArg

public int getIntArg(java.lang.String name,
                     java.lang.String descr,
                     int dflt)
              throws CommandLine.SyntaxError
Gets an argument from the command line that has an integer value. The command line is searched for the first occurrence of name. If name is not found, dflt is returned. If name is found, the argument immediately following it is returned (without modification), and the argument and its value are removed from the command line.. If there is no argument immediately following it, or if the value is not an integer, SyntaxError is thrown.

Parameters:
name
the named argument to be searched for.
descr
a very short description of what the argument is, e.g. "port number", "repetitions", etc. This is used only for error reporting.
dflt
the default value to be returned if the argument is not found.
Returns:
the value of the argument if found, or the default value.

findArg

public int findArg(java.lang.String name)
Searches the command line for the first occurrence of name and returns its index or -1. The argument is not removed from the command line.

Parameters:
name
the argument to search for.
Returns:
the position of the argument, or -1 if not found.

getArgValue

public java.lang.String getArgValue(java.lang.String name,
                                    java.lang.String descr)
                             throws CommandLine.SyntaxError
Searches the command line for the first occurrence of name and a following value. If found, removes the argument and its value from the command line and returns the value. If name is not found, returns null. If name is found without a value, a SyntaxError is thrown.

Parameters:
name
the argument to search for.
descr
a very brief description of the argument (for error handling only).
Returns:
the argument immediately following the found one, or null if not found.

getHelpArg

public boolean getHelpArg()
Searches the command line for all the common "help" switches: "-h", "-?", "/h", "/?", "-help", "/help". If one is found, it is removed from the command line and true is returned. Otherwise false is returned.

Returns:
true if found (and removed), false if not found.
Revision History



Generated on 5/9/2001 19:37 from CommandLine.java (PRIVATE SOURCES)