Getopt.java revision 111
0N/A/*
0N/A * CDDL HEADER START
0N/A *
0N/A * The contents of this file are subject to the terms of the
0N/A * Common Development and Distribution License (the "License").
0N/A * You may not use this file except in compliance with the License.
0N/A *
0N/A * See LICENSE.txt included in this distribution for the specific
0N/A * language governing permissions and limitations under the License.
0N/A *
0N/A * When distributing Covered Code, include this CDDL HEADER in each
0N/A * file and include the License file at LICENSE.txt.
0N/A * If applicable, add the following below this CDDL HEADER, with the
0N/A * fields enclosed by brackets "[]" replaced with your own identifying
0N/A * information: Portions Copyright [yyyy] [name of copyright owner]
0N/A *
0N/A * CDDL HEADER END
0N/A */
0N/A
0N/A/*
0N/A * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
0N/A * Use is subject to license terms.
0N/A */
0N/Apackage org.opensolaris.opengrok.util;
0N/A
0N/Aimport java.text.ParseException;
0N/Aimport java.util.ArrayList;
0N/Aimport java.util.logging.Level;
0N/Aimport java.util.logging.Logger;
0N/A
0N/A/**
0N/A * A simple implementation of the getopt(3c). It does just implement what I
0N/A * need ;-) Please note that I dislike the way GNU getopt allows mixing of
0N/A * options and arguments, so this version will stop processing options as soon
0N/A * as it encounters an argument.
0N/A *
0N/A */
0N/Apublic class Getopt {
0N/A class Option {
0N/A char option;
0N/A String argument;
0N/A };
0N/A
0N/A private ArrayList<Option> options;
0N/A private int current;
0N/A private int optind;
0N/A private String[] argv;
0N/A private String opts;
0N/A
0N/A /**
0N/A * Creates a new instance of Getopt
0N/A * @param argv argument vector
0N/A * @param opts the list of allowed options
0N/A */
0N/A public Getopt(String[] argv, String opts) {
0N/A options = new ArrayList<Option>();
0N/A current = -1;
0N/A optind = -1;
this.argv = argv;
this.opts = opts;
}
/**
* Parse the command lineoptions
* @throws ParseException if an illegal argument is passed
*/
public void parse() throws ParseException {
int ii = 0;
while (ii < argv.length) {
char[] chars = argv[ii].toCharArray();
if (chars.length > 0) {
if (chars[0] == '-') {
if (argv[ii].equals("--")) {
// End of command line options ;)
optind = ii + 1;
break;
}
for (int jj = 1; jj < chars.length; ++jj) {
int idx = opts.indexOf(chars[jj]);
if (idx == -1) {
throw new ParseException("Unknown argument: " + argv[ii].substring(jj), ii);
}
Option option = new Option();
option.option = chars[jj];
options.add(option);
// does this option take an argument?
if ((idx + 1) < opts.length() && (opts.charAt(idx + 1) ==':')) {
// next should be an argument
if ((jj + 1) < chars.length) {
// Rest of this is the argument
option.argument = argv[ii].substring(jj + 1);
break;
} else {
// next argument vector contains the argument
++ii;
if (ii < argv.length) {
option.argument = argv[ii];
} else {
throw new ParseException("Option " + chars[jj] + " requires an argument", ii);
}
}
}
}
++ii;
} else {
// End of options
optind = ii;
break;
}
}
}
}
/**
* Get the next option in the options string.
* @return the next valid option, or -1 if all options are processed
*/
public int getOpt() {
int ret = -1;
++current;
if (current < options.size()) {
ret = options.get(current).option;
}
return ret;
}
/**
* Reset the current pointer so we may traverse all the options again..
*/
public void reset() {
current = -1;
}
/**
* Get the argument to the current option
* @return the argument or null if none present (or allowed)
*/
public String getOptarg() {
String ret = null;
if (current < options.size()) {
ret = options.get(current).argument;
}
return ret;
}
/**
* Get the index of the first argument
* @return the index of the first argument in the original array
*/
public int getOptind() {
return optind;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Getopt opts = new Getopt(args, "a:b:c");
try {
opts.parse();
} catch (ParseException ex) {
Logger.getLogger("global").log(Level.SEVERE, null, ex);
System.exit(1);
}
System.out.println("Options and their arguments");
int cmd;
while ((cmd = opts.getOpt()) != -1) {
String arg = opts.getOptarg();
if (arg != null) {
System.out.println("" + (char)cmd + " " + arg);
} else {
System.out.println((char)cmd);
}
}
System.out.println("Arguments");
for (int ii = opts.getOptind(); ii < args.length; ++ii) {
System.out.println(args[ii]);
}
}
}