/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
package netscape.ldap.util;
import java.util.*;
/**
* This class is similar to the getopt() function in
* UNIX System V. You can use this class to parse command-line
* arguments.
*
*
* When you create an object of this class, you specify a string
* containing the command-line options that you want to check for.
* The string should contain the letters of these options. If an
* option requires an argument (for example, "-h
*
* For example, in the following string, the
*
* If an option not specified in the string is passed in as an
* argument, the
*
* Note that you are still responsible for verifying that any
* required arguments have been specified.
*
*
* The following example parses the command-line arguments for
* the hostname, port number, DN, and password to use when
* connecting and authenticating to an LDAP server.
*
*
* @param c Letter of the option that you want to check.
* @return true if the option was specified.
*/
public boolean hasOption(char c) {
boolean fReturn = false;
char cOption[]=new char[1];
cOption[0]=c;
String s = new String(cOption);
if (m_optionHashTable.get(s)=="1") {
fReturn = true;
}
return(fReturn);
}
/**
* Gets the argument specified with an option.
* For example,
*
* @param c The letter of the option that you want to check.
* @return The argument specified for this option.
*/
public String getOptionParam(char c) {
char cOption[] = new char[1];
cOption[0]=c;
String s = new String(cOption);
String sReturn=(String)m_optionParamHashTable.get(s);
return(sReturn);
}
/**
* Gets a list of any additional parameters specified
* (not including the arguments for any options).
* @return A list of the additional parameters.
*/
public Vector getParameters() {
return(m_ParameterList);
}
}
-h,
* -p, -D,, and -w options
* all require arguments. The -H option does not
* require any arguments.
*
* "h:p:D:w:H"
*
*
* You can use the hasOption method to determine if
* an option has been specified and the getOptionParam
* method to get the argument specified after a particular option.
* GetOpt object prints out an error
* message. Note that the object does not throw an exception or
* exit the application if an invalid option is specified.
*
* import netscape.ldap.*;
* import netscape.ldap.controls.*;
* import netscape.ldap.util.*;
* import java.util.*;
*
* public class SearchDirectory {
*
* public static void main( String[] args )
* {
*
* String usage = "Usage: java SearchDirectory -h
*
* @version 1.0
*/
public class GetOpt {
/**
* Internal variables
*/
private int m_pos;
private String optarg;
private String m_control;
private Vector m_option;
private Vector m_ParameterList;
private Hashtable m_optionHashTable;
private Hashtable m_optionParamHashTable;
/**
* Constructs a GetOpt object.
* @param strControl A string specifying the letters of
* all available options. If an option requires an argument
* (for example, "-h hasOption( 'H' ) checks if the -H option
* was specified.
* getOptionParameter( 'h' )
* gets the value of the argument specified with
* the -h option (such as "localhost" in "-h localhost").
*