Class GetOpt

java.lang.Object
netscape.ldap.util.GetOpt
All Implemented Interfaces:
Serializable

public class GetOpt extends Object implements Serializable
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 "), you should add a colon after the letter in this string.

For example, in the following string, the -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.

If an option not specified in the string is passed in as an argument, the 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.

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.

 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  -p  "
                      + "[-D ] [-w ]"

         int portnumber = LDAPv2.DEFAULT_PORT;

         // Check for these options. -H means to print out a usage message.
         GetOpt options = new GetOpt( "h:p:D:w:H", args );

         // Get the arguments specified for each option.
         String hostname = options.getOptionParam( 'h' );
         String port = options.getOptionParam( 'p' );
         String bindDN = options.getOptionParam( 'D' );
         String bindPW = options.getOptionParam( 'w' );

         // Check to see if the hostname (which is mandatory)
         // is not specified or if the user simply wants to
         // see the usage message (-H).
         if ( hostname == null || options.hasOption( 'H' ) ) {
             System.out.println( usage );
             System.exit( 1 );
         }

         // If a port number was specified, convert the port value
         //  to an integer.
         if ( port != null ) {
             try {
                 portnumber = java.lang.Integer.parseInt( port );
             } catch ( java.lang.Exception e ) {
                 System.out.println( "Invalid port number: " + port );
                 System.out.println( usage );
                 System.exit( 1 );
             }
         }

         // Create a new connection.
         LDAPConnection ld = new LDAPConnection();

         try {
             // Connect and authenticate to server.
             ld.connect( 3, hostname, portnumber, bindDN, bindPW );
             ...
         } catch ( LDAPException e ) {
             System.out.println( "Error: " + e.toString() );
         }
         ...
     }
 }
 
Version:
1.0
See Also: