package votorola.g.option; // Copyright 2007, Michael Allan. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Votorola Software"), to deal in the Votorola Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Votorola Software, and to permit persons to whom the Votorola Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Votorola Software. THE VOTOROLA SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE VOTOROLA SOFTWARE OR THE USE OR OTHER DEALINGS IN THE VOTOROLA SOFTWARE.
import gnu.getopt.*;
import java.util.*;
/** Getopt extensions.
*/
public final class GetoptX
{
private GetoptX() {}
/** Returns "Try 'progname --help' for more information.".
*/
public static String createHelpPrompt( String progname )
{
return "Try '" + progname + " --help' for more information.";
}
/** Returns "progname: unexpected argument: arg".
*/
public static String createUnexpectedArgWarning( String progname, String arg )
{
return progname + ": unexpected argument: " + arg;
}
/** Parses the command line arguments against a formal option map, {@linkplain
* Option#addOccurence registering actual occurences} in it.
*
* @param progname The program name to display when printing errors.
* @param argv The command line argument array.
* @param optionMap The map against which to interpret argv.
*
* @return the index of the first non-option argument, as returned by
* Getopt.getOptind() after parsing.
*/
public static int parse( final String progname, final String[] argv,
final Map optionMap )
{
// cf. votorola.a.voter.CommandResponder.Base.parse
final Option[] optionArray = optionMap.values().toArray( new Option[optionMap.size()] );
final Getopt getopt = new Getopt( progname, argv, ":", optionArray );
parse: for( ;; )
{
final int o = getopt.getopt();
switch( o )
{
case -1:
break parse;
case 0:
optionArray[getopt.getLongind()].addOccurence( getopt.getOptarg() );
break;
case ':': // missing option=argument
case '?': // unrecognized option
System.err.println( createHelpPrompt( progname ));
System.exit( 1 );
break; // prevent compiler warning
default:
assert false;
}
}
return getopt.getOptind();
}
}