- java.lang.Object
-
- org.snmp4j.util.ArgumentParser
-
public class ArgumentParser extends java.lang.ObjectTheArgumentParserparsers a command line array into Java objects and associates each object with the corresponding command line option according to predefined schemes for options and parameters.The format specification for options is:
[-<option>\[<type>[\<<regex>\>]{<parameter>[=<default>]}\]] ...where- '-' indicates a mandatory option ('+' would indicate an optional option)
- <option> is the name of the option, for example 'h' for 'help'
- <type> is one of 'i' (integer), 'l' (long), 'o' (octet string), and 's' (string)
- <regex> is a regular expression pattern that describes valid values
- <default> is a default value. If a default value is given, then a mandatory option is in fact optional
The format specification for parameters is:
[-<parameter>[<type>[<<regex>>]{[=<default>]}]]... [+<optionalParameter>[<type>[<<regex>>]{[=<default>]}]]... [<..>]where- '-' indicates a mandatory parameter whereas '+' would indicate an optional parameter which must not be followed by a mandatory parameter
- <parameter> is the name of the parameter, for example 'port'
- <type> is one of 'i' (integer), 'l' (long), and 's' (string)
- <regex> is a regular expression pattern that describes valid values
- <default> is a default value
- <..> (two consecutive dots after a space at the end of the pattern) indicate that the last parameter may occur more than once
- Since:
- 1.9
- Version:
- 1.10
- Author:
- Frank Fock
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classArgumentParser.ArgumentFormatstatic classArgumentParser.ArgumentParameterstatic classArgumentParser.ArgumentParseException
-
Field Summary
Fields Modifier and Type Field Description static intTYPE_INTEGERstatic intTYPE_LONGstatic intTYPE_OCTET_STRINGstatic intTYPE_STRINGstatic java.lang.String[]TYPES
-
Constructor Summary
Constructors Constructor Description ArgumentParser(java.lang.String optionFormat, java.lang.String parameterFormat)Creates an argument parser with the specified option and parameter formats.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidaddValues2Option(java.lang.String option, java.util.List<java.lang.Object> values, java.util.Map<java.lang.String,java.util.List<java.lang.Object>> options)static java.lang.ObjectgetFirstValue(java.util.List<? extends java.lang.Object> optionValues)Gets the first option value of a list of values - if available.java.util.Map<java.lang.String,ArgumentParser.ArgumentFormat>getOptionFormat()java.util.Map<? extends java.lang.String,? extends ArgumentParser.ArgumentFormat>getParameterFormat()static java.lang.ObjectgetValue(java.util.Map<java.lang.String,java.util.List<java.lang.Object>> args, java.lang.String name, int index)Gets then-th option value of a list of values - if available.static voidmain(java.lang.String[] args)Test application to try out patterns and command line parameters.java.util.Map<java.lang.String,java.util.List<java.lang.Object>>parse(java.lang.String[] args)Parses the given command line and returns a map of parameter/option names to aListof values.protected static java.util.Map<java.lang.String,ArgumentParser.ArgumentFormat>parseFormat(java.lang.String format, boolean parameterFormat)protected java.lang.ObjectparseParameterValue(ArgumentParser.ArgumentParameter type, java.lang.String value, ArgumentParser.ArgumentFormat format, int pos)protected java.util.List<java.lang.Object>parseValues(java.lang.String[] args, int offset, ArgumentParser.ArgumentFormat format)static java.lang.String[]selectCommand(java.lang.String[] args, java.lang.String optionFormat, java.lang.String[][] commandSets)Select a argument/parameter set from a given selection of sets by the command contained in the supplied argument list.
-
-
-
Field Detail
-
TYPES
public static final java.lang.String[] TYPES
-
TYPE_INTEGER
public static final int TYPE_INTEGER
- See Also:
- Constant Field Values
-
TYPE_LONG
public static final int TYPE_LONG
- See Also:
- Constant Field Values
-
TYPE_STRING
public static final int TYPE_STRING
- See Also:
- Constant Field Values
-
TYPE_OCTET_STRING
public static final int TYPE_OCTET_STRING
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
ArgumentParser
public ArgumentParser(java.lang.String optionFormat, java.lang.String parameterFormat)Creates an argument parser with the specified option and parameter formats.- Parameters:
optionFormat- the option format pattern to parse (seeArgumentParser).parameterFormat- the parameter format pattern to parse (seeArgumentParser).
-
-
Method Detail
-
getOptionFormat
public java.util.Map<java.lang.String,ArgumentParser.ArgumentFormat> getOptionFormat()
-
getParameterFormat
public java.util.Map<? extends java.lang.String,? extends ArgumentParser.ArgumentFormat> getParameterFormat()
-
parseFormat
protected static java.util.Map<java.lang.String,ArgumentParser.ArgumentFormat> parseFormat(java.lang.String format, boolean parameterFormat)
-
parse
public java.util.Map<java.lang.String,java.util.List<java.lang.Object>> parse(java.lang.String[] args) throws java.text.ParseExceptionParses the given command line and returns a map of parameter/option names to aListof values. Each value may be of typeInteger,Long, andString.- Parameters:
args- the command line argument list.- Returns:
- Map a map that returns options and parameters in the order they have been parsed, where each map entry has the option/parameter name as key and the value as value.
- Throws:
java.text.ParseException- if the command line does not match the patterns for options and parameters.
-
addValues2Option
protected void addValues2Option(java.lang.String option, java.util.List<java.lang.Object> values, java.util.Map<java.lang.String,java.util.List<java.lang.Object>> options)
-
parseValues
protected java.util.List<java.lang.Object> parseValues(java.lang.String[] args, int offset, ArgumentParser.ArgumentFormat format) throws java.text.ParseException- Throws:
java.text.ParseException
-
parseParameterValue
protected java.lang.Object parseParameterValue(ArgumentParser.ArgumentParameter type, java.lang.String value, ArgumentParser.ArgumentFormat format, int pos) throws ArgumentParser.ArgumentParseException
-
getFirstValue
public static java.lang.Object getFirstValue(java.util.List<? extends java.lang.Object> optionValues)
Gets the first option value of a list of values - if available.- Parameters:
optionValues- a probably empty list of values - could benull.- Returns:
- the first option value in
optionValuesif it exists,nullotherwise. - Since:
- 1.9.2
-
getValue
public static java.lang.Object getValue(java.util.Map<java.lang.String,java.util.List<java.lang.Object>> args, java.lang.String name, int index)Gets then-th option value of a list of values - if available.- Parameters:
args- a parameter and options list.name- the option or parameter name to returnindex- the index (zero based) of the option/parameter value to return.- Returns:
- the
n-th (zero based) option value inargs.get(name)if it exists,nullotherwise. - Since:
- 1.10
-
main
public static void main(java.lang.String[] args)
Test application to try out patterns and command line parameters. The default option and parameter patterns can be overridden by setting the system propertiesorg.snmp4j.OptionFormatandorg.snmp4j.ParameterFormatrespectively.The given command line is parsed using the specified patterns and the parsed values are returned on the console output.
The default option pattern is
-o1[i{parameter1}] -o2[s,l]and the default parameter pattern is-param1[i] -param2[s<(udp|tcp):.*[/[0-9]+]?>] +optParam1[l{=-100}] ..- Parameters:
args- the command line arguments to match with the specified format patterns.
-
selectCommand
public static java.lang.String[] selectCommand(java.lang.String[] args, java.lang.String optionFormat, java.lang.String[][] commandSets) throws java.text.ParseExceptionSelect a argument/parameter set from a given selection of sets by the command contained in the supplied argument list. The command is the first argument not starting with "-".- Parameters:
args- the argument list to scan.optionFormat- the option format which is common to all commands (or their super set)commandSets- the possible command sets, where each set is identified by its command string in the first element of the command set.- Returns:
- the command set matching the command in the argument list.
- Throws:
java.text.ParseException- if the command found inargscannot be found in thecommandSets, ornullifargsdoes not contain any command.- Since:
- 1.10
-
-