getopts.java – command line parser for Java

November 25, 2009

getopts.java is a simple parser for java. It is based on the Unix getopts shell utility of the same name.

Example usage: See main() below.

/* Name:        $Id$
 * Description: getopts is a simple command line parser based on the getopts shell parser.
 */

package HHjlib;

public class getopts
{
        private String argv[];
        private int argc;
        private int optind = 0;
        private int optind2 = 0;
        private String optarg;

        /** Initialize the getopts parser with the command line argument array */
        public getopts(String args[])
        {
                argv = args;
                argc = args.length;
                optind = 0;
                optind2 = 1;
        }


        /** The getOption() method parses positional parameters.
The optstring parameter contains the option characters to be recognized; If a character is followed by a colon, the option is required to have an argument.
Note: The colon and question mark characters may not be used as option characters. Each time it is invoked, getOption() returns the next option. If an option requires an argument, the argument may be retrieved using the getOptionArg() method after getOption() processing. If an invalid option is seen, getOption() returns a '?'. When the end of options is encountered, getopts returns the null character. */ public char getOption(String optstring) { if (optind >= argc || argv[optind].charAt(0) != '-') return ''; char argv_option = argv[optind].charAt(optind2); int optlen = optstring.length(); for (int indx = 0; indx < optlen; ++indx) { char opt = optstring.charAt(indx); if (argv_option == opt) { int argv_length = argv[optind].length(); if ((indx + 1 < optlen) && (optstring.charAt(indx + 1) == ':')) { if (optind2+1 < argv_length) { optarg = argv[optind].substring(optind2+1); optind++; optind2 = 1; } else if (optind+1 < argc) { optarg = argv[optind+1]; optind += 2; optind2 = 1; } else { optarg = Character.toString(opt); return ':'; } } else { if (optind2 + 1 < argv_length) { optind2++; } else { optind++; optind2 = 1; } } return opt; } } optarg = Character.toString(argv_option); return '?'; } /** Return a cmdline option argument or the option character if command line parsing failed */ public String getOptionArg() { return optarg; } /** Return the index of the next cmdline array element to be processed */ public int getOptionIndex() { return optind; } public static void main(String args[]) { getopts cmdline = new getopts(args); char option; while ((option = cmdline.getOption("abcd:e:")) != '') { switch(option) { case 'a': case 'b': case 'c': System.out.println("Option='" + option + "'"); break; case 'd': case 'e': System.out.println("Option='" + option + "', Argument='" + cmdline.getOptionArg() + "'"); break; case '?': System.err.println("Error: Invalid option '-" + cmdline.getOptionArg() + "'"); System.exit(1); case ':': System.err.println("Error: Missing option argument for '-" + cmdline.getOptionArg() + "'"); System.exit(1); } } for (int indx = cmdline.getOptionIndex(); indx < args.length; ++indx) { System.out.println(args[indx]); } } } /* :!javac % :!java `basename % .java` -a abc :!java `basename % .java` -a -b a b c :!java `basename % .java` -ab -c a b c :!java `basename % .java` -a -bc a b c :!java `basename % .java` -abc arg1 arg2 :!java `basename % .java` -ddarg arg1; # error: missing option argument :!java `basename % .java` -d darg arg1; # error: missing option argument :!java `basename % .java` -ddarg arg1 -e earg; # error: missing option argument :!java `basename % .java` -q; # Invalid option :!java `basename % .java` -d; # error: missing option argument */

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: