Class MapOptionHandler


  • public class MapOptionHandler
    extends OptionHandler<java.util.Map<?,​?>>
    Parses options into a Map.
    
     class Foo {
      @Option(name="-P",handler=MapOptionHandler.class)
       Map<String,String> args;
     }
     

    With this, -P x=1 -P y=2 parses to map of size 2. This option handler can be subtyped if you want to convert values to different types or to handle key=value in other formats, like key:=value.

    • Constructor Detail

    • Method Detail

      • getDefaultMetaVariable

        public java.lang.String getDefaultMetaVariable()
        Description copied from class: OptionHandler
        Gets the default meta variable name used to print the usage screen. The value returned by this method can be a reference in the ResourceBundle, if one was passed to CmdLineParser.
        Specified by:
        getDefaultMetaVariable in class OptionHandler<java.util.Map<?,​?>>
        Returns:
        null to hide a meta variable.
      • parseArguments

        public int parseArguments​(Parameters params)
                           throws CmdLineException
        Description copied from class: OptionHandler
        Called if the option that this owner recognizes is found.
        Specified by:
        parseArguments in class OptionHandler<java.util.Map<?,​?>>
        Parameters:
        params - The rest of the arguments. This method can use this object to access the arguments of the option if necessary. The object is valid only during the method call.
        Returns:
        The number of arguments consumed. (For example, returns 0 if this option doesn't take any parameters.)
        Throws:
        CmdLineException
      • createNewCollection

        protected java.util.Map createNewCollection​(java.lang.Class<? extends java.util.Map> type)
        Creates a new instance of the collection.
      • addToMap

        protected void addToMap​(java.lang.String argument,
                                java.util.Map m)
                         throws CmdLineException
        Encapsulates how a single string argument gets converted into key and value.
        Throws:
        CmdLineException
      • addToMap

        protected void addToMap​(java.util.Map m,
                                java.lang.String key,
                                java.lang.String value)
        This is the opportunity to convert values to some typed objects.