Class BasicOneArgParam<T>

java.lang.Object
org.rwtodd.args.BasicOneArgParam<T>
Type Parameters:
T - the type of the value maintained by this parameter.
All Implemented Interfaces:
OneArgParam, Param
Direct Known Subclasses:
DoubleParam, ExistingDirectoryParam, ExistingFileParam, IntListParam, IntParam, StringParam

public abstract class BasicOneArgParam<T> extends Object implements OneArgParam
A base class for a typical 1-argument parameter. It is expected that many custom parameters can be built as one-off anonymous classes with this class as the base.
  • Field Details

    • arg

      protected T arg
    • paramNames

      protected final Collection<String> paramNames
    • helpText

      protected final String helpText
  • Constructor Details

    • BasicOneArgParam

      public BasicOneArgParam(Collection<String> names, T dflt, String help)
      Construct a parameter.
      Parameters:
      names - a collection of names by which this parameter can be referenced on the command line.
      dflt - the default, starting value of the parameter.
      help - the help string for this parameter.
  • Method Details

    • getValue

      public T getValue()
      Fetch the value stored by this parameter.
      Returns:
      the value.
    • addToMap

      public void addToMap(Map<String,Param> map)
      Description copied from interface: Param
      Add the parameter's names to a Map<String,Param>.
      Specified by:
      addToMap in interface Param
      Parameters:
      map - the Map to which our names should be added.
    • addHelp

      public void addHelp(PrintStream ps)
      Description copied from interface: Param
      adds help for this parameter to the given stream.
      Specified by:
      addHelp in interface Param
      Parameters:
      ps - the stream to use
    • convertArg

      protected abstract T convertArg(String param, String arg) throws Exception
      A conversion method to get a T from a string. All subclasses must define this for the type they handle.
      Parameters:
      param - the name of the parameter found on the command line. In some cases it might affect conversion, but it is also good for error messages.
      arg - the argument to convert to a type T.
      Returns:
      the converted argument.
      Throws:
      Exception - if there is a problem with the conversion.
    • validate

      protected T validate(String param, T arg) throws Exception
      A validation method which subclasses can override to restrict the valid values of the argument.
      Parameters:
      param - the name of the given parameter.
      arg - the argument to the parameter, as coverted by convertArg(String, String).
      Returns:
      the validated arg, which doesn't have to mach the provided arg.
      Throws:
      Exception - if the provided arg was invalid.
    • process

      public void process(String param, String argument) throws ArgParserException
      Description copied from interface: OneArgParam
      Process an parameter with its argument.
      Specified by:
      process in interface OneArgParam
      Parameters:
      param - the name of the parameter, as it was found in the command-line.
      argument - the given argument to the parameter.
      Throws:
      ArgParserException - if there is a problem parsing the argument.