Class ParamUtils


  • public class ParamUtils
    extends java.lang.Object
    Utility class for work with command-line parameters.
    Since:
    7.0
    • Constructor Summary

      Constructors 
      Constructor Description
      ParamUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String getOptionalParam​(java.util.Map params, java.lang.String paramName, java.lang.String defaultValue)
      Retrieves the value of an optional parameter.
      static java.lang.String getRequiredParam​(java.util.Map params, java.lang.String paramName)
      Retrieves the value of a required parameter.
      static java.lang.String getRequiredProperty​(java.lang.String prop)
      Retrieves the value of a required system property.
      static java.util.Map parseCommandLine​(java.lang.String[] args)
      Builds a collection of parameters (name-value) from a given command-line.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ParamUtils

        public ParamUtils()
    • Method Detail

      • getRequiredParam

        public static java.lang.String getRequiredParam​(java.util.Map params,
                                                        java.lang.String paramName)
                                                 throws java.lang.Exception
        Retrieves the value of a required parameter.
        Parameters:
        params - collection of parameters
        paramName - parameter to retrieve
        Returns:
        the value of the parameter
        Throws:
        java.lang.Exception - if the parameter is not found in the given collection
      • getOptionalParam

        public static java.lang.String getOptionalParam​(java.util.Map params,
                                                        java.lang.String paramName,
                                                        java.lang.String defaultValue)
        Retrieves the value of an optional parameter. If the parameter is not set, a default value is returned.
        Parameters:
        params - collection of parameters
        paramName - parameter to retrieve
        defaultValue - default value
        Returns:
        the value of the parameter or the default, if the parameter is not set
      • parseCommandLine

        public static java.util.Map parseCommandLine​(java.lang.String[] args)
                                              throws java.lang.Exception
        Builds a collection of parameters (name-value) from a given command-line. If a command-line token starts with "-" it is considered to be a parameter name, otherwise it is deemed a parameter value. It is allowed to have parameters without a value - in this case an empty string is used to represent the value. An example for a valid command-line is: "-flagA -param1 value1 -param2 value2 -flagB".
        Parameters:
        args - command-line arguments
        Returns:
        parameter name to parameter value mapping
        Throws:
        java.lang.Exception - if a value without corresponding parameter name is encountered
      • getRequiredProperty

        public static java.lang.String getRequiredProperty​(java.lang.String prop)
                                                    throws java.lang.Exception
        Retrieves the value of a required system property.
        Parameters:
        prop - system property name
        Returns:
        the system property value
        Throws:
        java.lang.Exception - if the property is not set