Debian Codegraph @

java-sdk/ietfldap/org/ietf/ldap/util/GetOpt.java

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * The Original Code is mozilla.org code.
17 *
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 1999
21 * the Initial Developer. All Rights Reserved.
22 *
23 * Contributor(s):
24 *
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
36 *
37 * ***** END LICENSE BLOCK ***** */
38 package org.ietf.ldap.util;
39 import java.util.Hashtable;
40 import java.util.Vector;
41
42 /**
43 * This class is similar to the <CODE>getopt()</CODE> function in
44 * UNIX System V. You can use this class to parse command-line
45 * arguments.
46 * <P>
47 *
48 * When you create an object of this class, you specify a string
49 * containing the command-line options that you want to check for.
50 * The string should contain the letters of these options. If an
51 * option requires an argument (for example, "-h <hostname>"),
52 * you should add a colon after the letter in this string.
53 * <P>
54 *
55 * For example, in the following string, the <CODE>-h</CODE>,
56 * <CODE>-p</CODE>, <CODE>-D,</CODE>, and <CODE>-w</CODE> options
57 * all require arguments. The <CODE>-H</CODE> option does not
58 * require any arguments.
59 * <PRE>
60 * "h:p:D:w:H"
61 * </PRE>
62 *
63 * You can use the <CODE>hasOption</CODE> method to determine if
64 * an option has been specified and the <CODE>getOptionParam</CODE>
65 * method to get the argument specified after a particular option.
66 * <P>
67 *
68 * If an option not specified in the string is passed in as an
69 * argument, the <CODE>GetOpt</CODE> object prints out an error
70 * message. Note that the object does not throw an exception or
71 * exit the application if an invalid option is specified.
72 * <P>
73 *
74 * Note that you are still responsible for verifying that any
75 * required arguments have been specified.
76 * <P>
77 *
78 * The following example parses the command-line arguments for
79 * the hostname, port number, DN, and password to use when
80 * connecting and authenticating to an LDAP server.
81 * <PRE>
82 * import org.ietf.ldap.*;
83 * import org.ietf.ldap.controls.*;
84 * import org.ietf.ldap.util.*;
85 * import java.util.*;
86 *
87 * public class SearchDirectory {
88 *
89 * public static void main( String[] args )
90 * {
91 *
92 * String usage = "Usage: java SearchDirectory -h <host> -p <port> "
93 * + "[-D <bind dn>] [-w <password>]"
94 *
95 * int portnumber = LDAPConnection.DEFAULT_PORT;
96 *
97 * // Check for these options. -H means to print out a usage message.
98 * GetOpt options = new GetOpt( "h:p:D:w:H", args );
99 *
100 * // Get the arguments specified for each option.
101 * String hostname = options.getOptionParam( 'h' );
102 * String port = options.getOptionParam( 'p' );
103 * String bindDN = options.getOptionParam( 'D' );
104 * String bindPW = options.getOptionParam( 'w' );
105 *
106 * // Check to see if the hostname (which is mandatory)
107 * // is not specified or if the user simply wants to
108 * // see the usage message (-H).
109 * if ( hostname == null || options.hasOption( 'H' ) ) {
110 * System.out.println( usage );
111 * System.exit( 1 );
112 * }
113 *
114 * // If a port number was specified, convert the port value
115 * // to an integer.
116 * if ( port != null ) {
117 * try {
118 * portnumber = java.lang.Integer.parseInt( port );
119 * } catch ( java.lang.Exception e ) {
120 * System.out.println( "Invalid port number: " + port );
121 * System.out.println( usage );
122 * System.exit( 1 );
123 * }
124 * }
125 *
126 * // Create a new connection.
127 * LDAPConnection ld = new LDAPConnection();
128 *
129 * try {
130 * // Connect and authenticate to server.
131 * ld.connect( 3, hostname, portnumber, bindDN, bindPW );
132 * ...
133 * } catch ( LDAPException e ) {
134 * System.out.println( "Error: " + e.toString() );
135 * }
136 * ...
137 * }
138 * }
139 * </PRE>
140 *
141 * @version 1.0
142 */
143 public class GetOpt implements java.io.Serializable {
144 /**
145 * Internal variables
146 */
147 private int m_pos;
148 private String optarg;
149 private String m_control;
150 private Vector<String> m_option;
151 private Vector<String> m_ParameterList;
152 private Hashtable<String, String> m_optionHashTable;
153 private Hashtable<String, String> m_optionParamHashTable;
154 static final long serialVersionUID = -2570196909939660248L;
155
156 /**
157 * Constructs a <CODE>GetOpt</CODE> object.
158 * @param strControl a string specifying the letters of
159 * all available options. If an option requires an argument
160 * (for example, "-h <hostname>"), use a colon after the
161 * letter for that option (for example, "h:p:D:w:H").
162 * @param args an array of strings representing the list
163 * of arguments to parse (for example, the
164 * array passed into Main).
165 */
166 public GetOpt(String strControl, String args[]) {
167 m_option = new Vector<>();
168 m_control = strControl;
169 m_optionHashTable = new Hashtable<>();
170 m_optionParamHashTable = new Hashtable<>();
171 m_ParameterList = new Vector<>();
172
173 for (int i=0;i<args.length ;i++ ) {
174 String sOpt = args[i];
175 if (sOpt.length()>0) {
176 if ((sOpt.charAt(0)=='-') ||
177 (sOpt.charAt(0)=='/')) {
178 if (sOpt.length()>1) {
179 int nIndex = m_control.indexOf(sOpt.charAt(1));
180 if (nIndex == (-1)) {
181 System.err.println("Invalid usage. No option -" +
182 sOpt.charAt(1));
183 } else {
184 char cOpt[]= new char[1];
185 cOpt[0]= sOpt.charAt(1);
186 String sName = new String(cOpt);
187 m_optionHashTable.put(sName,"1");
188 if ((m_control != null) && (m_control.length() > (nIndex+1))) {
189 if (m_control.charAt(nIndex+1)==':') {
190 i++;
191 if (i < args.length)
192 m_optionParamHashTable.put(sName,args[i]);
193 else
194 System.err.println("Missing argument for option "+
195 sOpt);
196 }
197 }
198 }
199 } else {
200 System.err.println("Invalid usage.");
201 }
202 } else {
203 // probably parameters
204 m_ParameterList.addElement(args[i]);
205 }
206 }
207 }
208 }
209
210 /**
211 * Determines if an option was specified. For example,
212 * <CODE>hasOption( 'H' )</CODE> checks if the -H option
213 * was specified.
214 * <P>
215 *
216 * @param c letter of the option to check
217 * @return <code>true</code> if the option was specified.
218 */
219 public boolean hasOption(char c) {
220 boolean fReturn = false;
221 char cOption[]=new char[1];
222 cOption[0]=c;
223 String s = new String(cOption);
224 if (m_optionHashTable.get(s)=="1") {
225 fReturn = true;
226 }
227 return(fReturn);
228 }
229
230 /**
231 * Gets the argument specified with an option.
232 * For example, <CODE>getOptionParameter( 'h' )</CODE>
233 * gets the value of the argument specified with
234 * the -h option (such as "localhost" in "-h localhost").
235 * <P>
236 *
237 * @param c the letter of the option to check
238 * @return the argument specified for this option.
239 */
240 public String getOptionParam(char c) {
241 char cOption[] = new char[1];
242 cOption[0]=c;
243 String s = new String(cOption);
244 String sReturn=m_optionParamHashTable.get(s);
245 return(sReturn);
246 }
247
248 /**
249 * Gets a list of any additional parameters specified
250 * (not including the arguments for any options).
251 * @return a list of the additional parameters.
252 */
253 public Vector<String> getParameters() {
254 return(m_ParameterList);
255 }
256 }