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