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