Package jargs.gnu
Class CmdLineParser
java.lang.Object
jargs.gnu.CmdLineParser
- Direct Known Subclasses:
AutoHelpParser
public class CmdLineParser
extends java.lang.Object
Largely GNU-compatible command-line options parser. Has short (-v) and
long-form (--verbose) option support, and also allows options with
associated values (-d 2, --debug 2, --debug=2). Option processing
can be explicitly terminated by the argument '--'.
- Author:
- Steve Purcell
- See Also:
jargs.examples.gnu.OptionTest
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classCmdLineParser.IllegalOptionValueExceptionThrown when an illegal or missing value is given by the user for an option that takes a value.static classCmdLineParser.NotFlagExceptionThrown when the parsed commandline contains multiple concatenated short options, such as -abcd, where one or more requires a value.static classCmdLineParser.OptionRepresentation of a command-line optionstatic classCmdLineParser.OptionExceptionBase class for exceptions that may be thrown when options are parsedstatic classCmdLineParser.UnknownOptionExceptionThrown when the parsed command-line contains an option that is not recognised.static classCmdLineParser.UnknownSuboptionExceptionThrown when the parsed commandline contains multiple concatenated short options, such as -abcd, where one is unknown. -
Constructor Summary
Constructors Constructor Description CmdLineParser() -
Method Summary
Modifier and Type Method Description CmdLineParser.OptionaddBooleanOption(char shortForm, java.lang.String longForm)Convenience method for adding a boolean option.CmdLineParser.OptionaddBooleanOption(java.lang.String longForm)Convenience method for adding a boolean option.CmdLineParser.OptionaddDoubleOption(char shortForm, java.lang.String longForm)Convenience method for adding a double option.CmdLineParser.OptionaddDoubleOption(java.lang.String longForm)Convenience method for adding a double option.CmdLineParser.OptionaddIntegerOption(char shortForm, java.lang.String longForm)Convenience method for adding an integer option.CmdLineParser.OptionaddIntegerOption(java.lang.String longForm)Convenience method for adding an integer option.CmdLineParser.OptionaddLongOption(char shortForm, java.lang.String longForm)Convenience method for adding a long integer option.CmdLineParser.OptionaddLongOption(java.lang.String longForm)Convenience method for adding a long integer option.CmdLineParser.OptionaddOption(CmdLineParser.Option opt)Add the specified Option to the list of accepted optionsCmdLineParser.OptionaddStringOption(char shortForm, java.lang.String longForm)Convenience method for adding a string option.CmdLineParser.OptionaddStringOption(java.lang.String longForm)Convenience method for adding a string option.java.lang.ObjectgetOptionValue(CmdLineParser.Option o)Equivalent togetOptionValue(o, null).java.lang.ObjectgetOptionValue(CmdLineParser.Option o, java.lang.Object def)java.util.VectorgetOptionValues(CmdLineParser.Option option)java.lang.String[]getRemainingArgs()voidparse(java.lang.String[] argv)Extract the options and non-option arguments from the given list of command-line arguments.voidparse(java.lang.String[] argv, java.util.Locale locale)Extract the options and non-option arguments from the given list of command-line arguments.
-
Constructor Details
-
CmdLineParser
public CmdLineParser()
-
-
Method Details
-
addOption
Add the specified Option to the list of accepted options -
addStringOption
Convenience method for adding a string option.- Returns:
- the new Option
-
addStringOption
Convenience method for adding a string option.- Returns:
- the new Option
-
addIntegerOption
Convenience method for adding an integer option.- Returns:
- the new Option
-
addIntegerOption
Convenience method for adding an integer option.- Returns:
- the new Option
-
addLongOption
Convenience method for adding a long integer option.- Returns:
- the new Option
-
addLongOption
Convenience method for adding a long integer option.- Returns:
- the new Option
-
addDoubleOption
Convenience method for adding a double option.- Returns:
- the new Option
-
addDoubleOption
Convenience method for adding a double option.- Returns:
- the new Option
-
addBooleanOption
Convenience method for adding a boolean option.- Returns:
- the new Option
-
addBooleanOption
Convenience method for adding a boolean option.- Returns:
- the new Option
-
getOptionValue
Equivalent togetOptionValue(o, null). -
getOptionValue
- Returns:
- the parsed value of the given Option, or null if the option was not set
-
getOptionValues
- Returns:
- A Vector giving the parsed values of all the occurrences of the given Option, or an empty Vector if the option was not set.
-
getRemainingArgs
public final java.lang.String[] getRemainingArgs()- Returns:
- the non-option arguments
-
parse
public final void parse(java.lang.String[] argv) throws CmdLineParser.IllegalOptionValueException, CmdLineParser.UnknownOptionExceptionExtract the options and non-option arguments from the given list of command-line arguments. The default locale is used for parsing options whose values might be locale-specific. -
parse
public final void parse(java.lang.String[] argv, java.util.Locale locale) throws CmdLineParser.IllegalOptionValueException, CmdLineParser.UnknownOptionExceptionExtract the options and non-option arguments from the given list of command-line arguments. The specified locale is used for parsing options whose values might be locale-specific.
-