Package de.saar.getopt
Class ConvenientGetopt
java.lang.Object
de.saar.getopt.ConvenientGetopt
This class encapsulates the GNU Getopt library and makes it more convenient
to use from Java programs.
It hides the details of the parameter string syntax completely from the user,
and provides the automatic display of usage information.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intOption takes no arguments.static final intOption takes an optional argument.static final intOption takes a required argument. -
Constructor Summary
ConstructorsConstructorDescriptionConvenientGetopt(String progname, String howToCall, String docBelow) Create new getopt object. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdd an option that has no long name.voidAdd an option that has a long name.Retrieve those command-line arguments that don't belong to any option.getValue(char shortname) Retrieve the value of an option.Retrieve the value of an option.booleanhasOption(char shortname) Check whether an option was present on the command line.booleanCheck whether an option was present on the command line.voidParse a command line.
-
Field Details
-
NO_ARGUMENT
public static final int NO_ARGUMENTOption takes no arguments.- See Also:
-
OPTIONAL_ARGUMENT
public static final int OPTIONAL_ARGUMENTOption takes an optional argument.- See Also:
-
REQUIRED_ARGUMENT
public static final int REQUIRED_ARGUMENTOption takes a required argument.- See Also:
-
-
Constructor Details
-
ConvenientGetopt
Create new getopt object.- Parameters:
progname- name of the program (for error message)howToCall- description of program call syntax (or null)docBelow- documentation that goes below the options in the usage message (or null)
-
-
Method Details
-
addOption
Add an option that has no long name.- Parameters:
shortname- short name of the option ('s' for option -s)hasArg- does the option have arguments? (no, optional, required)defaultValue- default value for the argumentdescription- short description for the usage message.
-
addOption
public void addOption(char shortname, String longname, int hasArg, String description, String defaultValue) Add an option that has a long name.- Parameters:
shortname- short name of the option ('s' for option -s)longname- long name of the option ("server" for option --server)hasArg- does the option have arguments? (no, optional, required)defaultValue- default value for the argumentdescription- short description for the usage message.
-
parse
Parse a command line. If the command line was syntactically valid, this method will store the information about the command line internally; it can then be retrieved with the hasArgument, getValue, and getRemaining methods. If the command line was syntactically invalid, the method will print the usage message and terminate the programme.- Parameters:
args- the command-line array that the main method got.
-
hasOption
public boolean hasOption(char shortname) Check whether an option was present on the command line.- Parameters:
shortname- the short name of the option- Returns:
- true if the option was there.
-
hasOption
Check whether an option was present on the command line.- Parameters:
longname- the long name of the option- Returns:
- true if the option was there.
-
getValue
Retrieve the value of an option. If the option was not given on the command line, or the option takes an optional argument and the argument was not given, this method will return the default value for this option.- Parameters:
longname- the long name of the option- Returns:
- the value of the option; possibly the default value.
-
getValue
Retrieve the value of an option. If the option was not given on the command line, or the option takes an optional argument and the argument was not given, this method will return the default value for this option.- Parameters:
shortname- the short name of the option- Returns:
- the value of the option; possibly the default value.
-
getRemaining
Retrieve those command-line arguments that don't belong to any option.- Returns:
- the list of remaining command-line arguments, in order.
-