FileDocCategorySizeDatePackage
GetOpt.javaAPI DocExample2007Tue Oct 09 15:09:56 BST 2001com.darwinsys.util

GetOpt

public class GetOpt extends Object
A class to implement UNIX-style (single-character) command arguments
author
Ian F. Darwin, ian@darwinsys.com based on the standard UNIX getopt(3) program.
version
$Id: GetOpt.java,v 1.10 2001/10/09 18:09:56 ian Exp $

Fields Summary
protected String
pattern
The set of characters to look for
protected int
optind
Where we are in the options
public static final int
DONE
Public constant for "no more options" XXX should switch to hasNext()/next() pattern.
protected boolean
done
Internal flag - whether we are done all the options
protected String
optarg
The option argument, if there is one.
Constructors Summary
public GetOpt(String patt)

		pattern = patt;
		rewind();
	
Methods Summary
public intgetOptInd()
Retrieve the option index


	     
	   
		return optind;
	
public chargetopt(java.lang.String[] argv)
Return one argument.

		if (optind == (argv.length)) {
			done = true;
		}

		// Do not combine with previous if statement.
		if (done) {
			return DONE;
		}

		// Pick off the next command line argument, check if it starts "-".
		// If so look it up in the list.
		String thisArg = argv[optind++];
		if (thisArg.startsWith("-")) {
			optarg = null;
			for (int i=0; i<pattern.length(); i++) {
				char c = pattern.charAt(i);
				if (thisArg.equals("-"+c)) {	// we found it
					// If it needs an option argument, get it.
					if (i+1 < pattern.length() && 
						pattern.charAt(i+1)==':" &&
						optind < argv.length)
						optarg = argv[optind++]; 
					return c;
				}
			}
			// Still no match, and not used all args, so must be error.
			return '?";
		} else {
			// Found non-argument non-option word in argv: end of options.
			done = true;
			return DONE;
		}
	
public java.lang.Stringoptarg()
Retrieve the current option argument

		return optarg;
	
public voidrewind()

		done = false;
		optind = 0;