home *** CD-ROM | disk | FTP | other *** search
/ Java 1996 August / Java - Summer 1996.iso / rockridge / java / cmdLineArgs / example / ParseCmdLine.java < prev    next >
Encoding:
Java Source  |  1995-11-13  |  1.7 KB  |  53 lines

  1. class ParseCmdLine {
  2.     public static void main(String args[]) {
  3.  
  4.         int i = 0, j;
  5.         String arg;
  6.         char flag;
  7.         boolean vflag = false;
  8.         String outputfile = "";
  9.  
  10.         while (i < args.length && args[i].startsWith("-")) {
  11.             arg = args[i++];
  12.  
  13.     // use this type of check for "wordy" arguments
  14.             if (arg.equals("-verbose")) {
  15.                 System.out.println("verbose mode on");
  16.                 vflag = true;
  17.             }
  18.  
  19.     // use this type of check for arguments that require arguments
  20.             else if (arg.equals("-output")) {
  21.                 if (i < args.length)
  22.                     outputfile = args[i++];
  23.                 else
  24.                     System.err.println("-output requires a filename");
  25.                 if (vflag)
  26.                     System.out.println("output file = " + outputfile);
  27.             }
  28.  
  29.     // use this type of check for a series of flag arguments
  30.             else {
  31.                 for (j = 1; j < arg.length(); j++) {
  32.                     flag = arg.charAt(j);
  33.                     switch (flag) {
  34.                     case 'x':
  35.                         if (vflag) System.out.println("Option x");
  36.                         break;
  37.                     case 'n':
  38.                         if (vflag) System.out.println("Option n");
  39.                         break;
  40.                     default:
  41.                         System.err.println("ParseCmdLine: illegal option " + flag);
  42.                         break;
  43.                     }
  44.                 }
  45.             }
  46.         }
  47.         if (i == args.length)
  48.             System.err.println("Usage: ParseCmdLine [-verbose] [-xn] [-output afile] filename");
  49.         else
  50.             System.out.println("Success!");
  51.     }
  52. }
  53.