Class Commandline

Class Commandline

java.lang.Object
   |
   +----Commandline

public class Commandline
extends Object
A class that does approximately what getopt() does in C. For use in Java applications. Now with longopt support!
Version:
2.0, 6/10/97 Added GNU-style longopt support, which is enough of a change to warrant a full-version increment. Rewrote the whole constructor from scratch.
Author:
Paul Jimenez

Variable Index

params
The list of parameters that weren't arguments to options

Constructor Index

Commandline(String[], String, String)
for backwards compatibility and those times when you don't want longopts
Commandline(String[], String, String, String[], String[])
we only want to work hard once, so we do all the parsing here

Method Index

getOption(Character)
getOption(String)
the 'value' of an option
hasOption(Character)
hasOption(String)
whether or not the commandline contains a specified option
test(String[], String, String, String[], String[])
A test routine that I used to verify everything was working correctly.

Variables

params
  public String params[]
The list of parameters that weren't arguments to options

Constructors

Commandline
  public Commandline(String argv[],
                     String validflags,
                     String validoptions,
                     String validlongflags[],
                     String validlongoptions[]) throws InvalidCommandlineArgument
we only want work hard once, so we do all the parsing here
Parameters:
argv[] - array of strings (usually argv)
validflags - a string of valid options that don't take arguments
validoptions - a string of valid options that take (optional) arguments
valinglongflags[] - array of strings that are valid options that don't take arguments
valinglongoptions[] - array of strings that are valid options that take (optional) arguments
Throws: InvalidCommandlineArgument
iff an invalid argument is found on the commandline
Commandline
  public Commandline(String argv[],
                     String validflags,
                     String validoptions) throws InvalidCommandlineArgument
for backwards compatibility and those times when you don't want longopts
Parameters:
argv[] - array of strings (usually argv)
validflags - a string of valid options that don't take arguments
validoptions - a string of valid options that take (optional) arguments
Throws: InvalidCommandlineArgument
iff an invalid argument is found on the commandline

Methods

hasOption
  public boolean hasOption(Character option)
hasOption
  public boolean hasOption(String option)
whether or not the commandline contains a specified option
Parameters:
option - a single-character string of the option you want to check on
Returns:
true if option was specified on the command line or false if option was NOT specified on the command line
getOption
  public String getOption(Character option)
getOption
  public String getOption(String option)
the 'value' of an option
Parameters:
option - a string (single-character for short options) of the option you want returned
Returns:
the argument specified for option on the commandline, or an empty string ("") if option wasn't specified or no argument was specified
test
  public static void test(String in[],
                          String flags,
                          String opts,
                          String longflags[],
                          String longopts[])
A test routine that I used to verify everything was working correctly. It sends output to STDOUT, when called from a program like
import Commandline;
class test {
  public static void main(String argv[]) {
    String in[] = { "-ac", "c_arg", "param-1", "--flaga", "--opta", "opta_arg" , "--", "-param-3", "param-4" };
    String longflags[] = { "flaga", "flagb" };
    String longopts[] = { "opta", "optb" };
    Commandline.test(in, "ab", "cdef", longflags, longopts);
  }
}
which should output
Valid short options are: cdef (with args) and ab (without args).
Valid long options are:
  opta
  optb
Valid long flags are:
  flaga
  flagb
Running with options: -ac c_arg param-1 --flaga --opta opta_arg -- -param-3 param-4  ...
... found option c with value c_arg
... didn't find option d
... didn't find option e
... didn't find option f
... found option a with value
... didn't find option b
... found option opta with value opta_arg
... didn't find option optb
... found option flaga with value
... didn't find option flagb
... Extra Parameters: param-1 -param-3 param-4
Done.
Parameters:
in - an array of strings which are arguments
flags - a string of options that don't take arguments
opts - a string of options that take (optional) arguments


Converted using doccnv by Thomas Wendt