001package 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.
002
003import gnu.getopt.*;
004import java.util.*;
005
006
007/** Getopt extensions.
008  */
009public final class GetoptX
010{
011
012    private GetoptX() {}
013
014
015
016    /** Returns "Try '<em>progname</em> --help' for more information.".
017      */
018    public static String createHelpPrompt( String progname )
019    {
020        return "Try '" + progname + " --help' for more information.";
021    }
022
023
024
025    /** Returns "<em>progname</em>: unexpected argument: <em>arg</em>".
026      */
027    public static String createUnexpectedArgWarning( String progname, String arg )
028    {
029        return progname + ": unexpected argument: " + arg;
030    }
031
032
033
034    /** Parses the command line arguments against a formal option map, {@linkplain
035      * Option#addOccurence registering actual occurences} in it.
036      *
037      *     @param progname The program name to display when printing errors.
038      *     @param argv The command line argument array.
039      *     @param optionMap The map against which to interpret argv.
040      *
041      *     @return the index of the first non-option argument, as returned by
042      *       Getopt.getOptind() after parsing.
043      */
044    public static int parse( final String progname, final String[] argv,
045      final Map<String,Option> optionMap )
046    {
047        // cf. votorola.a.voter.CommandResponder.Base.parse
048
049        final Option[] optionArray = optionMap.values().toArray( new Option[optionMap.size()] );
050        final Getopt getopt = new Getopt( progname, argv, ":", optionArray );
051        parse: for( ;; )
052        {
053            final int o = getopt.getopt();
054            switch( o )
055            {
056                case -1:
057                    break parse;
058
059                case 0:
060                    optionArray[getopt.getLongind()].addOccurence( getopt.getOptarg() );
061                    break;
062
063                case ':': // missing option=argument
064                case '?': // unrecognized option
065                    System.err.println( createHelpPrompt( progname ));
066                    System.exit( 1 );
067                    break; // prevent compiler warning
068
069                default:
070                    assert false;
071            }
072        }
073        return getopt.getOptind();
074    }
075
076
077}