Setting Program Attributes |
This program, called ParseCmdLine, provides you with a basis from which you can build your own command-line parser.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: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!"); } }usage: ParseCmdLine [-verbose] [-xn] [-output afile] filenameThe arguments within square brackets are optional; the filename argument is required.The
ParseCmdLine
program relies heavily on theString
andSystem
classes. For information aboutString
s, see the The String and StringBuffer Classes lesson, or refer to the String class. For information about theSystem
class, go on to the next lesson, Using System Resources, or refer to the System class documentation.
Setting Program Attributes |