Class GetOpt
- All Implemented Interfaces:
Serializable
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.
-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
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiongetOptionParam
(char c) Gets the argument specified with an option.Gets a list of any additional parameters specified (not including the arguments for any options).boolean
hasOption
(char c) Determines if an option was specified.
-
Constructor Details
-
GetOpt
Constructs aGetOpt
object.- Parameters:
strControl
- a string specifying the letters of all available options. If an option requires an argument (for example, "-h"), use a colon after the letter for that option (for example, "h:p:D:w:H"). args
- an array of strings representing the list of arguments to parse (for example, the array passed into Main).
-
-
Method Details
-
hasOption
public boolean hasOption(char c) Determines if an option was specified. For example,hasOption( 'H' )
checks if the -H option was specified.- Parameters:
c
- letter of the option to check- Returns:
true
if the option was specified.
-
getOptionParam
Gets the argument specified with an option. For example,getOptionParameter( 'h' )
gets the value of the argument specified with the -h option (such as "localhost" in "-h localhost").- Parameters:
c
- the letter of the option to check- Returns:
- the argument specified for this option.
-
getParameters
Gets a list of any additional parameters specified (not including the arguments for any options).- Returns:
- a list of the additional parameters.
-