Getopt.java revision 1269
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 (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A */
0N/Apackage org.opensolaris.opengrok.util;
0N/A
0N/Aimport java.text.ParseException;
0N/Aimport java.util.ArrayList;
0N/Aimport java.util.List;
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 private static class Option {
0N/A char option;
0N/A String argument;
0N/A }
0N/A
0N/A private final List<Option> options;
0N/A private int current;
0N/A private int optind;
0N/A private final String[] argv;
0N/A private final 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;
0N/A this.argv = argv.clone();
0N/A this.opts = opts;
0N/A }
0N/A
0N/A /**
0N/A * Parse the command line options
0N/A * @throws ParseException if an illegal argument is passed
0N/A */
0N/A public void parse() throws ParseException {
0N/A
0N/A int ii = 0;
0N/A while (ii < argv.length) {
0N/A char[] chars = argv[ii].toCharArray();
0N/A if (chars.length > 0 && chars[0] == '-') {
0N/A if (argv[ii].equals("--")) {
0N/A // End of command line options ;)
0N/A optind = ii + 1;
0N/A break;
0N/A }
0N/A
0N/A for (int jj = 1; jj < chars.length; ++jj) {
0N/A int idx = opts.indexOf(chars[jj]);
0N/A if (idx == -1) {
0N/A throw new ParseException("Unknown argument: " + argv[ii].substring(jj), ii);
0N/A }
0N/A
0N/A Option option = new Option();
0N/A option.option = chars[jj];
0N/A options.add(option);
0N/A // does this option take an argument?
0N/A if ((idx + 1) < opts.length() && (opts.charAt(idx + 1) ==':')) {
0N/A // next should be an argument
0N/A if ((jj + 1) < chars.length) {
0N/A // Rest of this is the argument
0N/A option.argument = argv[ii].substring(jj + 1);
0N/A break;
0N/A }
0N/A // next argument vector contains the argument
0N/A ++ii;
0N/A if (ii < argv.length) {
0N/A option.argument = argv[ii];
0N/A } else {
0N/A throw new ParseException("Option " + chars[jj] + " requires an argument", ii);
0N/A }
0N/A }
0N/A }
0N/A ++ii;
0N/A } else {
0N/A // End of options
0N/A optind = ii;
0N/A break;
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Get the next option in the options string.
0N/A * @return the next valid option, or -1 if all options are processed
0N/A */
0N/A public int getOpt() {
0N/A int ret = -1;
0N/A
0N/A ++current;
0N/A if (current < options.size()) {
0N/A ret = options.get(current).option;
0N/A }
0N/A
0N/A return ret;
0N/A }
0N/A
0N/A /**
0N/A * Reset the current pointer so we may traverse all the options again..
0N/A */
0N/A public void reset() {
0N/A current = -1;
0N/A }
0N/A
0N/A /**
0N/A * Get the argument to the current option
0N/A * @return the argument or null if none present (or allowed)
0N/A */
0N/A public String getOptarg() {
0N/A String ret = null;
0N/A
0N/A if (current < options.size()) {
0N/A ret = options.get(current).argument;
0N/A }
0N/A return ret;
0N/A }
0N/A
0N/A /**
0N/A * Get the index of the first argument
0N/A * @return the index of the first argument in the original array
0N/A */
0N/A public int getOptind() {
0N/A return optind;
0N/A }
0N/A}
0N/A