org.snmp4j.util
Class ArgumentParser

java.lang.Object
  extended by org.snmp4j.util.ArgumentParser

public class ArgumentParser
extends java.lang.Object

The 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

Version:
1.9
Author:
Frank Fock

Nested Class Summary
static class ArgumentParser.ArgumentFormat
           
static class ArgumentParser.ArgumentParameter
           
static class ArgumentParser.ArgumentParseException
           
 
Field Summary
static int TYPE_INTEGER
           
static int TYPE_LONG
           
static int TYPE_STRING
           
static java.lang.String[] TYPES
           
 
Constructor Summary
ArgumentParser(java.lang.String optionFormat, java.lang.String parameterFormat)
          Creates an argument parser with the specified option and parameter formats.
 
Method Summary
protected  void addValues2Option(java.lang.String option, java.util.List values, java.util.Map options)
           
 java.util.Map getOptionFormat()
           
 java.util.Map getParameterFormat()
           
static void main(java.lang.String[] args)
          Test application to try out patterns and command line parameters.
 java.util.Map parse(java.lang.String[] args)
          Parses the given command line and returns a map of parameter/option names to a List of values.
protected static java.util.Map parseFormat(java.lang.String format, boolean parameterFormat)
           
protected  java.lang.Object parseParameterValue(ArgumentParser.ArgumentParameter type, java.lang.String value, ArgumentParser.ArgumentFormat format, int pos)
           
protected  java.util.List parseValues(java.lang.String[] args, int offset, ArgumentParser.ArgumentFormat format)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

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
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 (see ArgumentParser).
parameterFormat - the parameter format pattern to parse (see ArgumentParser).
Method Detail

getOptionFormat

public java.util.Map getOptionFormat()

getParameterFormat

public java.util.Map getParameterFormat()

parseFormat

protected static java.util.Map parseFormat(java.lang.String format,
                                           boolean parameterFormat)

parse

public java.util.Map parse(java.lang.String[] args)
                    throws java.text.ParseException
Parses the given command line and returns a map of parameter/option names to a List of values. Each value may be of type Integer, Long, and String.

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 values,
                                java.util.Map options)

parseValues

protected java.util.List 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
Throws:
ArgumentParser.ArgumentParseException

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 properties 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}] ..

Parameters:
args - the command line arguments to match with the specified format patterns.

Copyright 2003-2008 Frank Fock and Jochen Katz (SNMP4J.org)