Klasse CommandLine


public final class CommandLine extends AbstractBaseCommandLine<CommandLine>
A class to parse the Java command-line and access arguments by name and type using CmdArgOptions.

Example of working with a command-line with two options and a custom exception type:

 CommandLine commandLine = new CommandLine()
     .setExceptionType(MyApplicationException.class)
     .addOption(Option.builder(String.class)
         .name("req1")
         .required(true)
         .defaultValue("default")
         .build())
     .addOption(Option.builder()
         .name("opt1")
         .required(false)
         .build())
     .parse();
 
Seit:
1.0.0 - 2022-04-29
Autor:
David M., Markus S.
  • Konstruktordetails

    • CommandLine

      public CommandLine()
  • Methodendetails

    • self

      protected CommandLine self()
      Reference to ourselves for chaining.
      Angegeben von:
      self in Klasse AbstractBaseCommandLine<CommandLine>
      Gibt zurück:
      this
    • parse

      public CommandLine parse(String[] _args)
      Parses the given arguments.
      Parameter:
      _args - arguments to read
      Gibt zurück:
      this
    • getArg

      public <T> T getArg(CmdArgOption<T> _option)
      Returns the value associated with argument option.

      If no value is present, the default value of that option is returned (and might by null).
      If the option does not support values or option was not set, null is returned.
      If the option is a repeatable option, the value of the first occurrence is returned.

      Typparameter:
      T - type of option value
      Parameter:
      _option - option
      Gibt zurück:
      value, maybe null
    • getArg

      public <T> T getArg(CmdArgOption<T> _option, T _default)
      Returns the value associated with argument option.

      If no value is present, the given default value is used.
      If the given default is also null, the default of that option is returned (and might by null). If the option does not support values or if the option was not set, null is returned.

      Typparameter:
      T - type of option value
      Parameter:
      _option - option
      _default - default to use when no value present (overrides default specified in option)
      Gibt zurück:
      value, maybe null
    • getArgs

      public <T> List<T> getArgs(CmdArgOption<T> _option)
      Returns the value associated with argument option.

      If no value is present, the default value of that option is returned (and might by null). If the option does not support values or if the option was not set, null is returned.

      Typparameter:
      T - type of option value
      Parameter:
      _option - option
      Gibt zurück:
      List, maybe empty or null
    • getArgs

      public <T> List<T> getArgs(CmdArgOption<T> _option, T _default)
      Returns the value associated with argument option.

      If no value is present, the given default value is used.
      If the given default is also null, the default of that option is returned (and might by null). If the option does not support values or if the option was not set, null is returned.

      Typparameter:
      T - type of option value
      Parameter:
      _option - option
      _default - default to use when no value present (overrides default specified in option)
      Gibt zurück:
      List, maybe empty or null
    • getArg

      public Object getArg(CharSequence _optionName)
      Returns an option value using the options name.
      Parameter:
      _optionName - option name
      Gibt zurück:
      value or null if option has no value
      Löst aus:
      RuntimeException - if option is unknown or command line was not parsed before
    • getArg

      public Object getArg(char _optionName)
      Returns an option value using the options short name.
      Parameter:
      _optionName - option short name
      Gibt zurück:
      value or null if option has no value
      Löst aus:
      RuntimeException - if option is unknown or command line was not parsed before
    • getArg

      public <T> T getArg(CharSequence _optionName, Class<T> _type, T _default)
      Returns an option value using the options name and converting the value to the given type.
      Will use the configured converter to convert the value.

      If given type is not the same as the type specified in CmdArgOption an exception is thrown.
      Typparameter:
      T - data type of argument
      Parameter:
      _optionName - option short name
      _type - expected value type
      _default - default to use if option not set (and not required)
      Gibt zurück:
      value or null if option has no value
      Löst aus:
      RuntimeException - if option is unknown or command line was not parsed
      RuntimeException - if type class is not the type defined in CmdArgOption
      Seit:
      1.0.1 - 2022-05-24
    • getArg

      public <T> T getArg(CharSequence _optionName, Class<T> _type)
      Returns an option value using the options name and converting the value to the given type.
      Will use the configured converter to convert the value.

      If given type is not the same as the type specified in CmdArgOption an exception is thrown.
      Typparameter:
      T - data type of argument
      Parameter:
      _optionName - option short name
      _type - expected value type
      Gibt zurück:
      value or null if option has no value or was not set
      Löst aus:
      RuntimeException - if option is unknown or command line was not parsed
      RuntimeException - if type class is not the type defined in CmdArgOption
      Seit:
      1.0.1 - 2022-05-24
    • getArg

      public <T> T getArg(char _optionName, Class<T> _type)
      Returns an option value using the options short name and converting the value to the given type.
      Will use the configured converter to convert the value.

      If given type is not the same as the type specified in CmdArgOption an exception is thrown.
      Typparameter:
      T - type of option value
      Parameter:
      _optionName - option short name
      _type - expected value type
      Gibt zurück:
      value or null if option has no value
      Löst aus:
      RuntimeException - if option is unknown or command line was not parsed
      RuntimeException - if type class is not the type defined in CmdArgOption
      Seit:
      1.0.1 - 2022-05-24
    • getArg

      public <T> T getArg(char _optionName, Class<T> _type, T _default)
      Returns an option value using the options short name and converting the value to the given type.
      Will use the configured converter to convert the value.

      If given type is not the same as the type specified in CmdArgOption an exception is thrown.
      Typparameter:
      T - data type of argument
      Parameter:
      _optionName - option short name
      _type - expected value type
      _default - default to use if option not set (and not required)
      Gibt zurück:
      value or null if option has no value
      Löst aus:
      RuntimeException - if option is unknown or command line was not parsed
      RuntimeException - if type class is not the type defined in CmdArgOption
      Seit:
      1.0.1 - 2022-05-24
    • getArgs

      public <T> List<T> getArgs(CharSequence _optionName, Class<T> _type)
      Returns all option values using the options name and converting the values to the given type.
      Will use the configured converter to convert the value.

      If given type is not the same as the type specified in CmdArgOption an exception is thrown.
      Typparameter:
      T - data type of argument
      Parameter:
      _optionName - option short name
      _type - expected value type
      Gibt zurück:
      value or null if option has no value
      Löst aus:
      RuntimeException - if option is unknown or command line was not parsed
      RuntimeException - if type class is not the type defined in CmdArgOption
      Seit:
      1.0.1 - 2022-05-24
    • getArgs

      public <T> List<T> getArgs(char _optionName, Class<T> _type)
      Returns all option values using the options short name and converting the values to the given type.
      Will use the configured converter to convert the value.

      If given type is not the same as the type specified in CmdArgOption an exception is thrown.
      Typparameter:
      T - data type of argument
      Parameter:
      _optionName - option short name
      _type - expected value type
      Gibt zurück:
      value or null if option has no value
      Löst aus:
      RuntimeException - if option is unknown or command line was not parsed
      RuntimeException - if type class is not the type defined in CmdArgOption
      Seit:
      1.0.1 - 2022-05-24
    • hasArg

      public boolean hasArg(CmdArgOption<?> _option)
      Checks if the given option was at least used once in the command line.
      Parameter:
      _option - option to check
      Gibt zurück:
      true if it was used at least once, false otherwise
    • hasArg

      public boolean hasArg(String _arg)
      Checks if the given option was at least used once in the command line.

      Will only check if the string argument was found as option name, will not check with short name.

      Parameter:
      _arg - option to check
      Gibt zurück:
      true if it was used at least once, false otherwise
    • hasArg

      public boolean hasArg(char _arg)
      Checks if the given option was at least used once in the command line.

      Will only check if the char argument was found as short option.

      Parameter:
      _arg - option to check
      Gibt zurück:
      true if it was used at least once, false otherwise
    • getArgCount

      public int getArgCount(CmdArgOption<?> _option)
      Returns the number of occurrences of the given option.

      If the option was never set, 0 is returned.

      Parameter:
      _option - option
      Gibt zurück:
      number of occurrences