java.lang.Object
net.sourceforge.argparse4j.internal.ArgumentImpl
All Implemented Interfaces:
Argument

public final class ArgumentImpl extends Object implements Argument
The application code must not use this class directly.
  • Field Details

  • Constructor Details

  • Method Details

    • getPrimaryFlag

      String getPrimaryFlag()
    • textualName

      public String textualName()
      Description copied from interface: Argument

      Returns textual representation of the argument name.

      For named arguments, this method returns the first argument given in ArgumentContainer.addArgument(String...). For positional arguments, this method returns the flags joined with "/", e.g. -f/--foo.

      Specified by:
      textualName in interface Argument
      Returns:
      The textual representation of the argument name.
    • formatShortSyntax

      String formatShortSyntax()
      Short syntax is used in usage message, e.g. --foo BAR
      Returns:
      short syntax
    • formatShortSyntaxNoBracket

      String formatShortSyntaxNoBracket()
      Short syntax is used in usage message, e.g. --foo BAR, but without bracket when this is not required option.
      Returns:
      short syntax
    • resolveMetavar

      String[] resolveMetavar()
    • formatMetavar

      String formatMetavar()
    • formatHelpTitle

      private String formatHelpTitle()
    • printHelp

      public void printHelp(PrintWriter writer, boolean defaultHelp, TextWidthCounter textWidthCounter, int width)
    • convert

      public Object convert(ArgumentParserImpl parser, String value) throws ArgumentParserException
      Throws:
      ArgumentParserException
    • nargs

      public ArgumentImpl nargs(int n)
      Description copied from interface: Argument

      Sets the number of command line arguments that should be consumed.

      Don't give this method '*', '+' or '?'. They are converted to int value and it is not what you expect. For these strings, use Argument.nargs(String).

      Specified by:
      nargs in interface Argument
      Parameters:
      n - A positive integer
      Returns:
      this
    • nargs

      public ArgumentImpl nargs(String n)
      Description copied from interface: Argument

      Sets the number of command line arguments that should be consumed.

      This method takes one of following string: "*", "+" and "?". If "*" is given, All command line arguments present are gathered into a List. If "+" is given, just like "*", all command line arguments present are gathered into a List. Additionally, an error message will be generated if there wasn't at least one command line argument present. If "?" is given, one argument will be consumed from the command line if possible, and produced as a single item. If no command line argument is present, the value from Argument.setDefault(Object) will be produced. Note that for named arguments, there is an additional case - the option string is present but not followed by a command line argument. In this case the value from Argument.setConst(Object) will be produced.

      Specified by:
      nargs in interface Argument
      Parameters:
      n - "*", "+" or "?"
      Returns:
      this
    • setConst

      public ArgumentImpl setConst(Object value)
      Description copied from interface: Argument

      Sets constant values that are not read from the command line but are required for the various actions.

      The const value defaults to null.

      Specified by:
      setConst in interface Argument
      Parameters:
      value - The const value
      Returns:
      this
    • setConst

      @SafeVarargs public final <E> ArgumentImpl setConst(E... values)
      Description copied from interface: Argument

      Sets list of constant values that are not read from the command line but are required for the various actions.

      The given values will be converted to List. The const value defaults to null. If you want to set non-List item, use Argument.setConst(Object).

      Specified by:
      setConst in interface Argument
      Type Parameters:
      E - The type of the values
      Parameters:
      values - The const values
      Returns:
      this
    • setDefault

      public ArgumentImpl setDefault(Object value)
      Description copied from interface: Argument

      Sets value which should be used if the command line argument is not present.

      The default value defaults to null.

      Specified by:
      setDefault in interface Argument
      Parameters:
      value - The default value
      Returns:
      this
    • setDefault

      @SafeVarargs public final <E> ArgumentImpl setDefault(E... values)
      Description copied from interface: Argument

      Sets list of values which should be used if the command line argument is not present.

      The default value defaults to null. The given values will be converted to List. If you want to set non-List item, use Argument.setDefault(Object).

      Specified by:
      setDefault in interface Argument
      Type Parameters:
      E - The type of the values
      Parameters:
      values - The default values
      Returns:
      this
    • setDefault

      public ArgumentImpl setDefault(FeatureControl ctrl)
      Description copied from interface: Argument

      Sets special value to control default value handling.

      Currently, only FeatureControl.SUPPRESS is available. If it is given, default value is not add as a attribute.

      Specified by:
      setDefault in interface Argument
      Parameters:
      ctrl - The special value to control default value handling.
      Returns:
      this
    • createReflectArgumentType

      private <T> ReflectArgumentType<T> createReflectArgumentType(Class<T> type)
    • type

      public <T> ArgumentImpl type(Class<T> type)
      Description copied from interface: Argument

      Sets the type which the command line argument should be converted to.

      By default, type is String, which means no conversion is made. The type must have a constructor which takes one String argument.

      As a convenience, if one of following primitive types (boolean.class, byte.class, short.class, int.class, long.class, float.class and double.class) is specified, it is converted to its wrapped type counterpart. For example, if int.class is given, it is silently converted to Integer.class.

      Specified by:
      type in interface Argument
      Type Parameters:
      T - The type of the value this argument accepts
      Parameters:
      type - The type which the command line argument should be converted to.
      Returns:
      this
    • type

      public <T> ArgumentImpl type(ArgumentType<T> type)
      Description copied from interface: Argument

      Sets ArgumentType object which converts command line argument to appropriate type.

      This would be useful if you need to convert the command line argument into a type which does not have a constructor with one String argument.

      Specified by:
      type in interface Argument
      Type Parameters:
      T - The type of the value this argument accepts
      Parameters:
      type - The ArgumentType object
      Returns:
      this
    • required

      public ArgumentImpl required(boolean required)
      Description copied from interface: Argument

      If true is given, this named argument must be specified in command line otherwise error will be issued.

      The default value is false. This object is a positional argument, this property is ignored.

      Specified by:
      required in interface Argument
      Parameters:
      required - true or false
      Returns:
      this
    • action

      public ArgumentImpl action(ArgumentAction action)
      Description copied from interface: Argument
      Sets the action to be taken when this argument is encountered at the command line.
      Specified by:
      action in interface Argument
      Parameters:
      action - ArgumentAction object
      Returns:
      this
    • choices

      public ArgumentImpl choices(ArgumentChoice choice)
      Description copied from interface: Argument

      Sets ArgumentChoice object which inspects value so that it fulfills its criteria.

      This method is useful if more complex inspection is necessary than basic Argument.choices(Object...).

      Specified by:
      choices in interface Argument
      Parameters:
      choice - ArgumentChoice object.
      Returns:
      this
    • choices

      public <E> ArgumentImpl choices(Collection<E> values)
      Description copied from interface: Argument
      Sets a collection of the allowable values for the argument.
      Specified by:
      choices in interface Argument
      Type Parameters:
      E - The type of the values
      Parameters:
      values - A collection of the allowable values
      Returns:
      this
    • choices

      @SafeVarargs public final <E> ArgumentImpl choices(E... values)
      Description copied from interface: Argument
      Sets a collection of the allowable values for the argument.
      Specified by:
      choices in interface Argument
      Type Parameters:
      E - The type of the values
      Parameters:
      values - A collection of the allowable values
      Returns:
      this
    • dest

      public ArgumentImpl dest(String dest)
      Description copied from interface: Argument

      The name of the attribute to be added.

      The default value is For positional arguments, The default value is normally supplied as the first argument to ArgumentParser.parseArgs(String[]). For named arguments, ArgumentParser generates the default value of dest by taking the first long option string and stripping away the initial -- string. If no long option strings were supplied, dest will be derived from the first short option string by stripping the initial - character. Any internal - characters will be converted to _.

      Specified by:
      dest in interface Argument
      Parameters:
      dest - The name of the attribute to be added
      Returns:
      this
    • metavar

      public ArgumentImpl metavar(String... metavar)
      Description copied from interface: Argument
      Set the name for the argument in usage messages.
      Specified by:
      metavar in interface Argument
      Parameters:
      metavar - The name for the argument in usage messages
      Returns:
      this
    • help

      public ArgumentImpl help(String help)
      Description copied from interface: Argument
      Sets the brief description of what the argument does.
      Specified by:
      help in interface Argument
      Parameters:
      help - The brief description of what the argument does
      Returns:
      this
    • help

      public ArgumentImpl help(FeatureControl ctrl)
      Description copied from interface: Argument

      Sets special value to control help message handling.

      Currently, only FeatureControl.SUPPRESS is available. If it is given, the help entry for this option is not displayed in the help message.

      Specified by:
      help in interface Argument
      Parameters:
      ctrl - The special value to control help message handling.
      Returns:
      this
    • isNamedArgument

      boolean isNamedArgument()
    • run

      public void run(ArgumentParserImpl parser, Map<String,Object> res, String flag, Object value, Consumer<Object> valueSetter) throws ArgumentParserException
      Throws:
      ArgumentParserException
    • getDest

      public String getDest()
      Description copied from interface: Argument
      Returns dest value.
      Specified by:
      getDest in interface Argument
      Returns:
      The dest value
    • getConst

      public Object getConst()
      Description copied from interface: Argument
      Returns const value.
      Specified by:
      getConst in interface Argument
      Returns:
      The const value
    • getDefault

      public Object getDefault()
      Description copied from interface: Argument
      Returns default value.
      Specified by:
      getDefault in interface Argument
      Returns:
      The default value
    • getDefaultControl

      public FeatureControl getDefaultControl()
      Description copied from interface: Argument
      Returns default control.
      Specified by:
      getDefaultControl in interface Argument
      Returns:
      The default control
    • getHelpControl

      public FeatureControl getHelpControl()
      Description copied from interface: Argument
      Returns help control.
      Specified by:
      getHelpControl in interface Argument
      Returns:
      The help control
    • getName

      public String getName()
    • isRequired

      public boolean isRequired()
    • getMinNumArg

      int getMinNumArg()
    • getMaxNumArg

      int getMaxNumArg()
    • getMetavar

      public String[] getMetavar()
    • getArgumentGroup

      ArgumentGroupImpl getArgumentGroup()
    • getAction

      public ArgumentAction getAction()
    • getHelp

      public String getHelp()
    • getFlags

      String[] getFlags()
    • localize

      private String localize(String messageKey)