KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jbet > MyGetopt


1 /*
2  * JBET - Java Binary Enhancement Tool
3  * Copyright (c) 2003 Networks Associates Technology, Inc.
4  *
5  * This software was developed under DARPA/SPAWAR contract
6  * N66001-00-C-8602 "SPMA" as part of the
7  * DARPA OASIS research program.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */

30
31 package jbet;
32
33 /**
34  * A class to provide simple getopt-like command-line argument processing.
35  * handles arguments of the forms:
36  * -zfpy (four options)
37  * -xglop (one option with argument)
38  * -x glop (one option with argument)
39  * --verbose (one long option)
40  * --xclude=glop (one long option with argument)
41  * --xclude glop (one long option with argument)
42  * @author Larry D'Anna
43  */

44
45 class MyGetopt {
46
47     /** values for the "long" table: whether an option takes an argument.
48      */

49     public static final int NO_ARG = 0; // not permitted
50
public static final int OPTIONAL_ARG = 1; // optional
51
public static final int REQUIRED_ARG = 2; // required
52

53     String JavaDoc [] args;
54     int argind;
55     String JavaDoc opts;
56
57     /** side effects of arg parsing visible to caller
58      */

59     public int optind; // first unparsed option
60
public String JavaDoc error; // error string if failure
61
public String JavaDoc optarg; // argument to the option
62

63     MyLongOpt [] longs;
64
65     /** Constructor with empty specifier table.
66      * @param _opts
67      * @param _args
68      */

69     public MyGetopt (String JavaDoc _opts, String JavaDoc [] _args) {
70     this(_opts, _args, new MyLongOpt [0] );
71     }
72
73     /** Constructor.
74      * @param _opts control options for one-letter options.
75      * option followed by one colon means required
76      * option followed by two colon means optional
77      * @param _args input array of argument strings to parse.
78      * @param _longs array of argument specifiers for long options.
79      */

80     public MyGetopt (String JavaDoc _opts, String JavaDoc [] _args, MyLongOpt [] _longs) {
81     opts = _opts;
82     args = _args;
83     longs = _longs;
84     optind = argind = 0;
85     }
86     
87     /** parse an option off of the args and return it.
88      * @return 1 if eof, '?' for invalid option,
89      * or a single char which is the option.
90      */

91     public int getopt() {
92     optarg = null;
93
94     if (optind == args.length)
95         return -1;
96
97     if ( argind == args[optind].length() ) {
98         argind = 0;
99         optind++;
100     }
101
102     if (optind == args.length) //yes, i meant to do this again
103
return -1;
104     
105     if ( argind == 0 && args[optind].startsWith("--") ) {
106         String JavaDoc longopt;
107         String JavaDoc arg = args[optind];
108         optind++; argind=0;
109         int eqind = arg.indexOf('=');
110         if (eqind == -1)
111         longopt = arg.substring(2);
112         else
113         longopt = arg.substring(2, eqind);
114
115         for (int i = 0; i < longs.length; i++)
116         if ( longopt.equals( longs[i].name ) ) {
117             switch (longs[i].type) {
118             case NO_ARG:
119             if (eqind != -1) {
120                 error = "unexpected arguement for --" + longopt;
121                 return '?';
122             } else
123                 return longs[i].ret;
124
125             case OPTIONAL_ARG:
126             if (eqind == -1)
127                 optarg = null;
128             else
129                 optarg = arg.substring(eqind+1);
130             return longs[i].ret;
131                 
132             case REQUIRED_ARG:
133             if (eqind == -1) {
134                 if (optind == args.length) {
135                 error = "option incomplete: " + longopt;
136                 return opts.charAt(0) == ':' ? ':' : '?' ;
137                 }
138                 optarg = args[optind];
139                 optind++;
140             } else
141                 optarg = arg.substring(eqind+1);
142             return longs[i].ret;
143             }
144         }
145         error = "invalid option: " + arg;
146         return '?';
147     }
148
149
150     if (argind == 0) {
151         if (args[optind].charAt(argind) == '-')
152         argind++;
153         else {
154         optarg = args[optind];
155         optind++;
156         return 1;
157         }
158     }
159
160     char opt = args[optind].charAt(argind);
161     argind++;
162     if ( !( ('a'<=opt && opt<='z') || ('A'<=opt && opt<='Z')) ) {
163         error = "invalid option: " + opt;
164         return '?';
165     }
166
167     for (int i = 0; i < opts.length(); i++)
168         if ( opts.charAt(i)==opt ) {
169         if ( i+1 < opts.length() && opts.charAt(i+1)==':' ) {
170             if ( i+2 < opts.length() && opts.charAt(i+2)==':' ) {
171             optarg = null;
172             if ( argind <= args[optind].length() ) {
173                 optarg = args[optind].substring(argind);
174                 argind = 0;
175                 optind++;
176             }
177             return opt;
178             }
179             if ( argind == args[optind].length() ) {
180             argind = 0;
181             optind++;
182             if (optind == args.length) {
183                 error = "option incomplete: " + opt;
184                 return opts.charAt(0) == ':' ? ':' : '?' ;
185             }
186             optarg = args[optind];
187             optind++;
188             return opt;
189             } else {
190             optarg = args[optind].substring(argind);
191             argind = 0;
192             optind++;
193             return opt;
194             }
195         } else {
196             return opt;
197         }
198         }
199
200     error = "invalid option: " + opt;
201     return '?';
202     } // getopt
203

204 }
205
206
Popular Tags