KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > GetoptDemo


1 import gnu.getopt.LongOpt;
2 import gnu.getopt.Getopt;
3
4 /*
5  * This sample code was written by Aaron M. Renn and is a demonstration
6  * of how to utilize some of the features of the GNU getopt package. This
7  * sample code is hereby placed into the public domain by the author and
8  * may be used without restriction.
9  */

10
11 public class GetoptDemo
12 {
13
14 public static void
15 main(String JavaDoc[] argv)
16 {
17  int c;
18  String JavaDoc arg;
19  LongOpt[] longopts = new LongOpt[3];
20  //
21
StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
22  longopts[0] = new LongOpt("help", LongOpt.NO_ARGUMENT, null, 'h');
23  longopts[1] = new LongOpt("outputdir", LongOpt.REQUIRED_ARGUMENT, sb, 'o');
24  longopts[2] = new LongOpt("maximum", LongOpt.OPTIONAL_ARGUMENT, null, 2);
25  //
26
Getopt g = new Getopt("testprog", argv, "-:bc::d:hW;", longopts);
27  g.setOpterr(false); // We'll do our own error handling
28
//
29
while ((c = g.getopt()) != -1)
30    switch (c)
31      {
32         case 0:
33           arg = g.getOptarg();
34           System.out.println("Got long option with value '" +
35                              (char)(new Integer JavaDoc(sb.toString())).intValue()
36                              + "' with argument " +
37                              ((arg != null) ? arg : "null"));
38           break;
39           //
40
case 1:
41           System.out.println("I see you have return in order set and that " +
42                              "a non-option argv element was just found " +
43                              "with the value '" + g.getOptarg() + "'");
44           break;
45           //
46
case 2:
47           arg = g.getOptarg();
48           System.out.println("I know this, but pretend I didn't");
49           System.out.println("We picked option " +
50                              longopts[g.getLongind()].getName() +
51                            " with value " +
52                            ((arg != null) ? arg : "null"));
53           break;
54           //
55
case 'b':
56           System.out.println("You picked plain old option " + (char)c);
57           break;
58           //
59
case 'c':
60         case 'd':
61           arg = g.getOptarg();
62           System.out.println("You picked option '" + (char)c +
63                              "' with argument " +
64                              ((arg != null) ? arg : "null"));
65           break;
66           //
67
case 'h':
68           System.out.println("I see you asked for help");
69           break;
70           //
71
case 'W':
72           System.out.println("Hmmm. You tried a -W with an incorrect long " +
73                              "option name");
74           break;
75           //
76
case ':':
77           System.out.println("Doh! You need an argument for option " +
78                              (char)g.getOptopt());
79           break;
80           //
81
case '?':
82           System.out.println("The option '" + (char)g.getOptopt() +
83                            "' is not valid");
84           break;
85           //
86
default:
87           System.out.println("getopt() returned " + c);
88           break;
89      }
90  //
91
for (int i = g.getOptind(); i < argv.length ; i++)
92    System.out.println("Non option argv element: " + argv[i] + "\n");
93 }
94
95 } // Class GetoptDemo
96

97
98
Popular Tags