KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > incava > jagol > OptionSet


1 package org.incava.jagol;
2
3 import java.io.*;
4 import java.util.*;
5
6
7 /**
8  * A group of options.
9  */

10 public class OptionSet
11 {
12     private List options = new ArrayList();
13
14     private List rcFiles = new ArrayList();
15
16     private String JavaDoc appName;
17     
18     private String JavaDoc description;
19     
20     public OptionSet(String JavaDoc appName, String JavaDoc description)
21     {
22         this.appName = appName;
23         this.description = description;
24     }
25     
26     /**
27      * Returns the application name.
28      */

29     public String JavaDoc getAppName()
30     {
31         return appName;
32     }
33
34     /**
35      * Returns the description.
36      */

37     public String JavaDoc getDescription()
38     {
39         return description;
40     }
41
42     /**
43      * Adds an options to this set.
44      */

45     public void add(Option opt)
46     {
47         options.add(opt);
48     }
49
50     /**
51      * Adds a run control file to be processed.
52      */

53     public void addRunControlFile(String JavaDoc name)
54     {
55         tr.Ace.log("adding rc file: " + name);
56         rcFiles.add(name);
57     }
58
59     /**
60      * Processes the run control files and command line arguments. Returns the
61      * arguments that were not consumed by option processing.
62      */

63     public String JavaDoc[] process(String JavaDoc[] args)
64     {
65         tr.Ace.log("args: " + args);
66
67         processRunControlFiles();
68
69         return processCommandLine(args);
70     }
71
72     /**
73      * Processes the run control files, if any.
74      */

75     protected void processRunControlFiles()
76     {
77         tr.Ace.log("");
78         Iterator it = rcFiles.iterator();
79         while (it.hasNext()) {
80             String JavaDoc rcFileName = (String JavaDoc)it.next();
81             tr.Ace.log("processing: " + rcFileName);
82             try {
83                 Properties props = new Properties();
84
85                 int tildePos = rcFileName.indexOf('~');
86                 if (tildePos != -1) {
87                     rcFileName = rcFileName.substring(0, tildePos) + System.getProperty("user.home") + rcFileName.substring(tildePos + 1);
88                 }
89
90                 props.load(new FileInputStream(rcFileName));
91
92                 tr.Ace.log("properties: " + props);
93                 Iterator pit = props.keySet().iterator();
94                 while (pit.hasNext()) {
95                     String JavaDoc key = (String JavaDoc)pit.next();
96                     String JavaDoc value = (String JavaDoc)props.get(key);
97                     tr.Ace.log(key + " => " + value);
98                     Iterator oit = options.iterator();
99                     boolean processed = false;
100                     while (!processed && oit.hasNext()) {
101                         Option opt = (Option)oit.next();
102                         tr.Ace.log("option: " + opt.getLongName());
103                         if (opt.getLongName().equals(key)) {
104                             tr.Ace.log("option matches: " + opt);
105                             processed = true;
106                             try {
107                                 opt.setValue(value);
108                             }
109                             catch (OptionException oe) {
110                                 tr.Ace.log("option exception: " + oe);
111                                 System.err.println("error: " + oe.getMessage());
112                             }
113                         }
114                     }
115                 }
116             }
117             catch (IOException ioe) {
118                 tr.Ace.log("exception: " + ioe);
119                 // ioe.printStackTrace();
120
}
121         }
122     }
123
124     /**
125      * Processes the command line arguments. Returns the arguments that were not
126      * consumed by option processing.
127      */

128     protected String JavaDoc[] processCommandLine(String JavaDoc[] args)
129     {
130         tr.Ace.log("args: " + args);
131         
132         List argList = new ArrayList();
133         for (int i = 0; i < args.length; ++i) {
134             argList.add(args[i]);
135         }
136
137         tr.Ace.log("arg list: " + argList);
138
139         while (argList.size() > 0) {
140             String JavaDoc arg = (String JavaDoc)argList.get(0);
141             
142             tr.Ace.log("arg: " + arg);
143             
144             if (arg.equals("--")) {
145                 argList.remove(0);
146                 break;
147             }
148             else if (arg.charAt(0) == '-') {
149                 tr.Ace.log("got leading dash");
150                 argList.remove(0);
151                 
152                 Iterator oit = options.iterator();
153                 boolean processed = false;
154                 while (!processed && oit.hasNext()) {
155                     Option opt = (Option)oit.next();
156                     tr.Ace.log("option: " + opt);
157                     try {
158                         processed = opt.set(arg, argList);
159                         tr.Ace.log("processed: " + processed);
160                     }
161                     catch (OptionException oe) {
162                         tr.Ace.log("option exception: " + oe);
163                         System.err.println("error: " + oe.getMessage());
164                     }
165                 }
166
167                 if (!processed) {
168                     tr.Ace.log("argument not processed: '" + arg + "'");
169                     if (arg.equals("--help") || arg.equals("-h")) {
170                         showUsage();
171                     }
172                     else if (rcFiles.size() > 0 && arg.equals("--help-config")) {
173                         showConfig();
174                     }
175                     else {
176                         System.err.println("invalid option: " + arg + " (-h will show valid options)");
177                     }
178                     
179                     break;
180                 }
181             }
182             else {
183                 break;
184             }
185         }
186
187         String JavaDoc[] unprocessed = (String JavaDoc[])argList.toArray(new String JavaDoc[0]);
188         
189         tr.Ace.log("args", args);
190         tr.Ace.log("unprocessed", unprocessed);
191
192         return unprocessed;
193     }
194
195     protected void showUsage()
196     {
197         tr.Ace.log("generating help");
198
199         System.out.println("Usage: " + appName + " [options] file...");
200         System.out.println(description);
201         System.out.println();
202         System.out.println("Options:");
203
204         tr.Ace.log("options: " + options);
205
206         List tags = new ArrayList();
207
208         Iterator it = options.iterator();
209         while (it.hasNext()) {
210             Option opt = (Option)it.next();
211             tr.Ace.log("opt: " + opt);
212             StringBuffer JavaDoc buf = new StringBuffer JavaDoc(" ");
213
214             if (opt.getShortName() == 0) {
215                 buf.append(" ");
216             }
217             else {
218                 buf.append("-" + opt.getShortName() + ", ");
219             }
220                             
221             buf.append("--" + opt.getLongName());
222                             
223             tags.add(buf.toString());
224         }
225                         
226         int widest = -1;
227         Iterator tit = tags.iterator();
228         while (tit.hasNext()) {
229             String JavaDoc tag = (String JavaDoc)tit.next();
230             widest = Math.max(tag.length(), widest);
231         }
232
233         it = options.iterator();
234         tit = tags.iterator();
235         while (it.hasNext()) {
236             Option opt = (Option)it.next();
237             String JavaDoc tag = (String JavaDoc)tit.next();
238             tr.Ace.log("opt: " + opt);
239
240             System.out.print(tag);
241             for (int i = tag.length(); i < widest + 2; ++i) {
242                 System.out.print(" ");
243             }
244
245             System.out.println(opt.getDescription());
246         }
247
248         if (rcFiles.size() > 0) {
249             System.out.println("For an example configure file, run --help-config");
250             System.out.println();
251             System.out.println("Configuration File" + (rcFiles.size() > 1 ? "s" : "") + ":");
252             Iterator rit = rcFiles.iterator();
253             while (rit.hasNext()) {
254                 String JavaDoc rcFileName = (String JavaDoc)rit.next();
255                 System.out.println(" " + rcFileName);
256             }
257         }
258     }
259
260     protected void showConfig()
261     {
262         tr.Ace.log("generating config");
263
264         Iterator it = options.iterator();
265         while (it.hasNext()) {
266             Option opt = (Option)it.next();
267             System.out.println("# " + opt.getDescription());
268             System.out.println(opt.getLongName() + " = " + opt.toString());
269             System.out.println();
270         }
271     }
272 }
273
Popular Tags