Class CliArgs

java.lang.Object
com.jenkov.cliargs.CliArgs

public class CliArgs extends Object
Utility class for parsing command line arguments. Provides methods to handle switches and their values from command line input.
  • Constructor Details

    • CliArgs

      public CliArgs(String[] args)
      Constructs a CliArgs instance and parses the given arguments.
      Parameters:
      args - the command line arguments array to parse
  • Method Details

    • parse

      public void parse(String[] arguments)
      Parses the given arguments array.
      Parameters:
      arguments - the arguments to parse
    • args

      public String[] args()
      Returns the original arguments array.
      Returns:
      the arguments array
    • arg

      public String arg(int index)
      Retrieves the argument at the specified index.
      Parameters:
      index - the index of the argument
      Returns:
      the argument at the index
    • switchPresent

      public boolean switchPresent(String switchName)
      Checks if the specified switch is present in the arguments.
      Parameters:
      switchName - the name of the switch
      Returns:
      true if the switch is present, false otherwise
    • switchValue

      public String switchValue(String switchName)
      Retrieves the value of a switch.
      Parameters:
      switchName - the name of the switch
      Returns:
      the switch value or null if not found
    • switchValue

      public String switchValue(String switchName, String defaultValue)
      Retrieves the value of a switch, with a default value if not found.
      Parameters:
      switchName - the name of the switch
      defaultValue - the default value to return if switch is not found
      Returns:
      the switch value or the default value
    • switchLongValue

      public Long switchLongValue(String switchName)
      Retrieves the long value of a switch.
      Parameters:
      switchName - the name of the switch
      Returns:
      the switch value as Long or null
    • switchLongValue

      public Long switchLongValue(String switchName, Long defaultValue)
      Retrieves the long value of a switch, with a default value.
      Parameters:
      switchName - the name of the switch
      defaultValue - the default value to return if switch is not found or parsing fails
      Returns:
      the switch value as Long or the default value
    • switchDoubleValue

      public Double switchDoubleValue(String switchName)
      Retrieves the double value of a switch.
      Parameters:
      switchName - the name of the switch
      Returns:
      the switch value as Double or null
    • switchDoubleValue

      public Double switchDoubleValue(String switchName, Double defaultValue)
      Retrieves the double value of a switch, with a default value.
      Parameters:
      switchName - the name of the switch
      defaultValue - the default value to return if switch is not found or parsing fails
      Returns:
      the switch value as Double or the default value
    • switchValues

      public String[] switchValues(String switchName)
      Retrieves the array of values for a switch.
      Parameters:
      switchName - the name of the switch
      Returns:
      the array of switch values, or empty array if not found
    • switchPojo

      public <T> T switchPojo(Class<T> pojoClass)
      Creates a POJO instance and fills it with switch values based on its fields.
      Type Parameters:
      T - the type of the POJO
      Parameters:
      pojoClass - the class of the POJO to create
      Returns:
      the instantiated POJO with fields set from switches
      Throws:
      RuntimeException - if instantiation or field access fails
    • targets

      public String[] targets()
      Returns the array of targets (non-switch arguments).
      Returns:
      the array of target arguments