wyvern.util
Class Options

java.lang.Object
  extended bywyvern.util.Options

public final class Options
extends java.lang.Object

Parses unix-style command-line switches. Doesn't do validation on the switches - just grabs them, and their arguments if any, and stuffs them into a HashMap.

Version:
1.0, Jun 16, 2003
Author:
Steve Yegge

Constructor Summary
Options()
           
 
Method Summary
static java.util.HashMap getopts(java.lang.String[] argv, java.lang.String opts)
          Parses the desired switches from an argument array.
static java.util.HashMap getopts(java.lang.String args, java.lang.String opts)
          Parses the desired switches from an argument string.
static void main(java.lang.String[] argv)
          Tests it out.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Options

public Options()
Method Detail

getopts

public static java.util.HashMap getopts(java.lang.String args,
                                        java.lang.String opts)
Parses the desired switches from an argument string.

Parameters:
args - the argument array as a whitespace-delimited string, such as "-r 400 -bf -1". This method simply splits the args string into a string array and calls getopts(String[], String).
opts - a unix-style options string, such as "oif:" or "ab:c::d?". Each character is an switch we want to look for. If the character is followed by a colon, it requires an argument. If it's followed by a double-colon, it takes an optional argument.
Returns:
a HashMap containing the switches we found, and their values, if they had arguments. Any switches with no arguments will have keys in the map, but will have null values. The switches will not have their leading "-", so passing "-a" will put the key "a" in the map. For options that don't take an argument, you should use containsKey() to see if the option was present.

Any non-option arguments are placed in a String array under the key "@ARGV" in the HashMap. If no non-option args are present, the "@ARGV" key will not be present in the result set.

For instance, if you passed in the option string "ab:c::d", and the argument string "-a -b foo -cd rmdir /foo/bar", the HashMap would contain:

  • "a" => null
  • "b" => "foo"
  • "c" => null
  • "d" => null
  • "@ARGV" => [ "rmdir" "/foo/bar" ]

getopts

public static java.util.HashMap getopts(java.lang.String[] argv,
                                        java.lang.String opts)
Parses the desired switches from an argument array. Same as the getopts(String, String) version, but takes an array as the first param.


main

public static void main(java.lang.String[] argv)
Tests it out.