GetSubOpt.java revision 7c478bd95313f5f23a4c958a745db2134aa03244
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* ident "%Z%%M% %I% %E% SMI"
*
* Copyright 1998-2002 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/**
* This class provides the functionality for parsing command line
* arguments (similar to getsubopt(3C)).
* <br>
* After constructing an instance of it, getNextSubOption() can be used
* to get the next suboption. getSubOptionArg() can be used to get the argument
* for that option.
*
*/
public class GetSubOpt {
/**
* Delimiter between suboptions.
*/
/**
* Delimiter between suboption and suboption argument.
*/
/**
* List of suboptions.
*/
protected String subOptions;
/**
* The length of the suboptions.
*/
private final int subOptionsLen;
/**
* Current position with in suboptions.
*/
protected int index;
/**
* Last suboption found.
*/
/**
* Last suboption argument found.
*/
/**
* Prepare a GetSubOpt object.
*
* @param subOptions
* String containing the list of suboptions. This value was most likely
* returned as an option argument by <i>GetOpt</i>.
*/
if (subOptions == null) {
this.subOptions = null;
} else {
}
index = 0;
}
/**
* Get the next suboption. The suboptions arguement is available from
* calling <i>getSubOptionArg()</i>.
*
* @return
* The next suboption.
* @exception IllegalArgumentException
* Thrown when no more suboptions remain.
*/
if (!hasMoreSubOptions()) {
throw new IllegalArgumentException(msg);
}
// Last suboption and no value.
} else if (valueIndex == -1 ||
// Suboption has no value.
} else {
// Suboption with value.
boolean quoted = false;
int endIndex;
if (index < subOptionsLen &&
// Value is quoted.
endIndex =
// Missing close quote.
if (endIndex == -1) {
"getsubopt_missing_close_quote");
throw new IllegalArgumentException(msg);
}
quoted = true;
index++;
} else {
// Value is not quoted.
if (endIndex == -1) {
}
}
// Skip closing quote.
if (quoted) {
index++;
}
/*
* Ensure that either the end of the suboptions has been
* reached or the next suboption is ready for parsing. For
* example, quoted values must not contain characters between
* the closing quote and OPTION_DELIM.
*/
if (optionIndex >= 0) {
if (index < subOptionsLen &&
"getsubopt_malformed_value");
throw new IllegalArgumentException(msg);
}
}
}
}
return subOption;
}
/**
* Indicates whether more suboptions exist.
*
* @return
* True if at least one more suboption exists, otherwise false.
*/
public boolean hasMoreSubOptions() throws IllegalArgumentException {
if (subOptions == null) {
return false;
}
// Skip over leading OPTION_DELIMs.
while (index < subOptionsLen &&
}
/*
* Ensure that there really is a suboption present. If a
* VALUE_DELIM has been found the suboption string is missing.
*/
if (index < subOptionsLen &&
"getsubopt_value_without_suboption");
throw new IllegalArgumentException(msg);
}
return (index < subOptionsLen);
}
/**
* Get the current suboptions argument, or null if no argument is present.
*
* @return
* String containing the current suboptions argument, or null if the
* no argument is present.
*/
public String getSubOptionArg() {
return value;
}
}