public class ArgumentParser extends Object
ArgumentParser
parsers 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
The format specification for parameters is:
[-<parameter>[<type>[<<regex>>]{[=<default>]}]]... [+<optionalParameter>[<type>[<<regex>>]{[=<default>]}]]... [<..>]where
Modifier and Type | Class and Description |
---|---|
static class |
ArgumentParser.ArgumentFormat |
static class |
ArgumentParser.ArgumentParameter |
static class |
ArgumentParser.ArgumentParseException |
Modifier and Type | Field and Description |
---|---|
static int |
TYPE_INTEGER |
static int |
TYPE_LONG |
static int |
TYPE_OCTET_STRING |
static int |
TYPE_STRING |
static String[] |
TYPES |
Constructor and Description |
---|
ArgumentParser(String optionFormat,
String parameterFormat)
Creates an argument parser with the specified option and parameter formats.
|
Modifier and Type | Method and Description |
---|---|
protected void |
addValues2Option(String option,
List values,
Map<String,List<?>> options) |
static Object |
getFirstValue(List<? extends Object> optionValues)
Gets the first option value of a list of values - if available.
|
Map<String,ArgumentParser.ArgumentFormat> |
getOptionFormat() |
Map<? extends String,? extends ArgumentParser.ArgumentFormat> |
getParameterFormat() |
static Object |
getValue(Map args,
String name,
int index)
Gets the
n -th option value of a list of values - if available. |
static void |
main(String[] args)
Test application to try out patterns and command line parameters.
|
Map<String,List<?>> |
parse(String[] args)
Parses the given command line and returns a map of parameter/option names
to a
List of values. |
protected static Map<String,ArgumentParser.ArgumentFormat> |
parseFormat(String format,
boolean parameterFormat) |
protected Object |
parseParameterValue(ArgumentParser.ArgumentParameter type,
String value,
ArgumentParser.ArgumentFormat format,
int pos) |
protected List<?> |
parseValues(String[] args,
int offset,
ArgumentParser.ArgumentFormat format) |
static String[] |
selectCommand(String[] args,
String optionFormat,
String[][] commandSets)
Select a argument/parameter set from a given selection of sets by the
command contained in the supplied argument list.
|
public static final String[] TYPES
public static final int TYPE_INTEGER
public static final int TYPE_LONG
public static final int TYPE_STRING
public static final int TYPE_OCTET_STRING
public ArgumentParser(String optionFormat, String parameterFormat)
optionFormat
- the option format pattern to parse (see ArgumentParser
).parameterFormat
- the parameter format pattern to parse (see ArgumentParser
).public Map<String,ArgumentParser.ArgumentFormat> getOptionFormat()
public Map<? extends String,? extends ArgumentParser.ArgumentFormat> getParameterFormat()
protected static Map<String,ArgumentParser.ArgumentFormat> parseFormat(String format, boolean parameterFormat)
public Map<String,List<?>> parse(String[] args) throws ParseException
List
of values. Each value may be of type
Integer
, Long
, and String
.args
- the command line argument list.ParseException
- if the command line does not match the patterns for options and
parameters.protected void addValues2Option(String option, List values, Map<String,List<?>> options)
protected List<?> parseValues(String[] args, int offset, ArgumentParser.ArgumentFormat format) throws ParseException
ParseException
protected Object parseParameterValue(ArgumentParser.ArgumentParameter type, String value, ArgumentParser.ArgumentFormat format, int pos) throws ArgumentParser.ArgumentParseException
public static Object getFirstValue(List<? extends Object> optionValues)
optionValues
- a probably empty list of values - could be null
.optionValues
if it exists,
null
otherwise.public static Object getValue(Map args, String name, int index)
n
-th option value of a list of values - if available.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.n
-th (zero based) option value in
args.get(name)
if it exists, null
otherwise.public static void main(String[] args)
org.snmp4j.OptionFormat
and
org.snmp4j.ParameterFormat
respectively.
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}] ..
args
- the command line arguments to match with the specified format patterns.public static String[] selectCommand(String[] args, String optionFormat, String[][] commandSets) throws ParseException
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.ParseException
- if the command found in args
cannot be found in the
commandSets
, or null
if args
does not contain any command.Copyright © 2018 SNMP4J.org. All rights reserved.