Previous | Next | Trail Map | Writing Java Programs | Setting Program Attributes


Parsing Command Line Arguments

This program, called ParseCmdLine, provides you with a basis from which you can build your own command line parser.
class ParseCmdLine {
    public static void main(String[] args) {

        int i = 0, j;
        String arg;
        char flag;
        boolean vflag = false;
        String outputfile = "";

        while (i < args.length && args[i].startsWith("-")) {
            arg = args[i++];

    // use this type of check for "wordy" arguments
            if (arg.equals("-verbose")) {
                System.out.println("verbose mode on");
                vflag = true;
            }

    // use this type of check for arguments that require arguments
            else if (arg.equals("-output")) {
                if (i < args.length)
                    outputfile = args[i++];
                else
                    System.err.println("-output requires a filename");
                if (vflag)
                    System.out.println("output file = " + outputfile);
            }

    // use this type of check for a series of flag arguments
            else {
                for (j = 1; j < arg.length(); j++) {
                    flag = arg.charAt(j);
                    switch (flag) {
                    case 'x':
                        if (vflag) System.out.println("Option x");
                        break;
                    case 'n':
                        if (vflag) System.out.println("Option n");
                        break;
                    default:
                        System.err.println("ParseCmdLine: illegal option " + flag);
                        break;
                    }
                }
            }
        }
        if (i == args.length)
            System.err.println("Usage: ParseCmdLine [-verbose] [-xn] [-output afile] filename");
        else
            System.out.println("Success!");
    }
}
It accepts one command line argument of each type: a word argument, an argument that requires an argument, and two flags. In addition, this program requires a filename. Here's the usage statement for this program:
usage: ParseCmdLine [-verbose] [-xn] [-output afile] filename
The arguments within square brackets are optional; the filename argument is required.

See Also

The example program relies heavily on the String and System classes. For more information about these two classes follow the links below.

The String and StringBuffer Classes(in the Writing Java Programs trail)

java.lang.String(in the API reference documentation)


Previous | Next | Trail Map | Writing Java Programs | Setting Program Attributes