KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > uni_hamburg > eggink > autojar > Getopt


1 package de.uni_hamburg.eggink.autojar;
2
3 /* This program is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU General Public License
5  * as published by the Free Software Foundation; either version 2
6  * of the License, or (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16  *
17  * Bernd.Eggink@rrz.uni-hamburg.de
18  */

19
20 import java.io.*;
21 import java.util.*;
22
23 /** Handles program arguments like Unix getopt(). Example: <pre>
24  * void main(String[] argv)
25  * {
26  * int opt;
27  * boolean x = false;
28  * Getopt getopt = new Getopt(argv, "a:hx");
29  * String argA = "";
30  *
31  * // get Options
32  *
33  * while ((opt = getopt.getOption()) != -1)
34  * {
35  * switch (opt)
36  * {
37  * case 'a': argA = getopt.getOptarg();
38  * break;
39  * case 'h': help();
40  * break;
41  * case 'x': x = true;
42  * break;
43  * default: System.out.println("wrong option: " + getOptopt());
44  * }
45  * }
46  *
47  * // handle non-option parameters
48  *
49  * String[] files = getopt.getParms();
50  *
51  * for (int i = 0; i < files.length; ++i)
52  * doSomethingWith(files[i]);
53  * }
54  *
55  * Legal calls:
56  * java Mainclass -hx file1 file2
57  * java Mainclass -xa blurp -h -- file3
58  *
59  * Illegal calls:
60  * java Mainclass -y f
61  * java Mainclass -a
62  *
63  * The special argument -- denotes the end of the options. All Arguments
64  * after this will be considered as non-options, even if starting with -.
65  *
66  * If any command line argument is of the form "@filename", this file
67  * is read and each line is considered a program parameter.
68  *
69  * </pre>
70  *
71  * @author Bernd Eggink, RRZ Uni Hamburg (Bernd.Eggink@rrz.uni-hamburg.de)
72  */

73
74 public class Getopt
75 {
76     private String JavaDoc[] argv;
77     private ArrayList arglist;
78     private String JavaDoc optarg, opts;
79     private int ichr = 0, optind = 0;
80     private char optopt;
81     private boolean opterr = true;
82
83     //----------------------------------------------------------------------
84

85     /** Default constructor
86      *
87      * @param opts The possible options.
88      */

89
90     private Getopt(String JavaDoc opts)
91     {
92         this.opts = opts;
93         arglist = new ArrayList();
94     }
95     
96     //----------------------------------------------------------------------
97

98     /** Constructs a Getopt object by reading all arguments from a file.
99      * @param filename Name of the file to read.
100      * @param opts The possible options.
101      * @param opterr If true, an error message will be printed if an illegal option character
102      * is found.
103      */

104
105     public Getopt(String JavaDoc filename, String JavaDoc opts, boolean opterr)
106         throws IOException
107     {
108         this(filename, opts);
109         this.opterr = opterr;
110     }
111
112     //----------------------------------------------------------------------
113

114     /** Like Getopt(filename, opts, true) */
115
116     public Getopt(String JavaDoc filename, String JavaDoc opts)
117         throws IOException
118     {
119         this(opts);
120         addArgsFromFile(filename);
121     }
122
123     //----------------------------------------------------------------------
124

125     /** Constructs a Getopt object using the main() parameter list.
126      * @param argv The arguments of main()
127      * @param opts The possible options. Each option is a single character.
128      * If followed by a colon, the option given in the command line
129      * must be followed by an argument.
130      * @param opterr If true, an error message will be printed if an illegal option character
131      * is encountered.
132      */

133
134     public Getopt(String JavaDoc[] argv, String JavaDoc opts, boolean opterr)
135         throws IOException
136     {
137         this(argv, opts);
138         this.opterr = opterr;
139     }
140
141     //----------------------------------------------------------------------
142

143     /** Like Getopt(argv, opts, true). */
144
145     public Getopt(String JavaDoc[] argv, String JavaDoc opts)
146         throws IOException
147     {
148         this(opts);
149
150         for (int i = 0; i < argv.length; ++i)
151         {
152             String JavaDoc arg = argv[i];
153
154             if (arg.startsWith("@"))
155                 addArgsFromFile(arg.substring(1));
156             else
157                 arglist.add(arg);
158         }
159         
160         this.argv = (String JavaDoc[])arglist.toArray(new String JavaDoc[0]);
161     }
162
163     //----------------------------------------------------------------------
164

165     private void addArgsFromFile(String JavaDoc name)
166         throws IOException
167     {
168         BufferedReader reader = new BufferedReader(new FileReader(new File(name)));
169         String JavaDoc zeile;
170
171         while ((zeile = reader.readLine()) != null)
172             arglist.add(zeile);
173
174         reader.close();
175     }
176
177     //----------------------------------------------------------------------
178

179     /** Returns the current argument or null. */
180
181     public String JavaDoc getOptarg()
182     {
183         return optarg;
184     }
185
186     //----------------------------------------------------------------------
187

188     /** Returns the next option as int value,
189      * -1 if no more options are available, '?' if the option is illegal.
190      */

191
192     public int getOption()
193     {
194         char c;
195         int iopt;
196         
197         if (ichr == 0)
198         {
199             // beginning of word
200

201             if (optind >= argv.length || argv[optind].charAt(0) != '-')
202                 return -1;
203             
204             if (argv[optind].equals("-") || argv[optind].equals("--"))
205             {
206                 ++optind;
207                 return -1;
208             }
209         }
210
211         // had -
212

213         c = argv[optind].charAt(++ichr);
214
215         if (c == ':' || (iopt = opts.indexOf(c)) < 0)
216         {
217             if (opterr)
218                 System.err.println("+++ Illegal option: " + c);
219
220             if (++ichr >= argv[optind].length())
221             {
222                 ++optind;
223                 ichr = 0;
224             }
225
226             optopt = c;
227             optarg = null;
228             return '?';
229         }
230
231         if (iopt + 1 < opts.length() && opts.charAt(iopt + 1) == ':')
232         {
233             // must have optarg
234

235             if (++ichr < argv[optind].length())
236                 optarg = argv[optind++].substring(ichr);
237             else if (++optind >= argv.length)
238             {
239                 if (opterr)
240                     System.err.println("+++ Option " + c + " requires an argument");
241         
242                 ichr = 0;
243                 optopt = c;
244                 return '?';
245             }
246             else
247                 optarg = argv[optind++];
248
249             ichr = 0;
250         }
251         else
252         {
253             // no optarg
254

255             if (ichr + 1 >= argv[optind].length())
256             {
257                 ++optind;
258                 ichr = 0;
259             }
260
261             optarg = null;
262         }
263
264         return c;
265     }
266
267     //----------------------------------------------------------------------
268

269     /** Returns the unrecognized option character. */
270     
271     public char getOptopt()
272     {
273         return optopt;
274     }
275
276     //----------------------------------------------------------------------
277

278     /** Returns parameters not handled by getOption() as array of Strings. */
279     
280     public String JavaDoc[] getParms()
281     {
282         String JavaDoc[] parms = new String JavaDoc[argv.length - optind];
283
284         for (int i = 0; optind + i < argv.length; ++i)
285             parms[i] = argv[optind + i];
286
287         return parms;
288     }
289
290     //----------------------------------------------------------------------
291

292 /*
293     static public void main(String[] args)
294     {
295         try
296         {
297             Getopt g = new Getopt(args, "ab:c:d:e");
298
299             int opt;
300             
301             while ((opt = g.getOption()) >= 0)
302                 System.out.println("opt = " + (char)opt + ", optarg = " + g.getOptarg());
303
304             String[] words = g.getParms();
305
306             for (int i = 0; i < words.length; ++i)
307                 System.out.println(words[i]);
308         }
309         catch (IOException ex)
310         {
311             System.err.println(ex);
312         }
313     }
314 */

315
316 }
317
318
Popular Tags