Class CLIHelper

java.lang.Object
eu.svjatoslav.commons.cli_helper.CLIHelper

public class CLIHelper extends Object

A utility class that provides methods for reading and validating various data types from the command line. The methods in this class prompt the user with a given message, read the user’s input, validate and parse that input, and then either return a valid value or re-prompt on invalid input.

Each method optionally supports:

  • Returning a specified default value if the user presses ENTER without providing input.
  • Allowing empty input (returning null) when no default value is provided.
  • Enforcing minimum/maximum ranges or lengths (when applicable).

Example usage:

 
   Boolean answer = CLIHelper.askBoolean("Do you want to continue?", true);
   Integer number = CLIHelper.askInteger("Enter a number between 5 and 10:", 7, 5, 10, false);
   String name = CLIHelper.askString("What's your name?", "Anonymous", 2, 20, false);
 
 
  • Constructor Details

    • CLIHelper

      public CLIHelper()
  • Method Details

    • askBoolean

      public static Boolean askBoolean(String prompt, Boolean defaultValue, boolean allowEmpty)
      Asks the user for a boolean value using the specified prompt on the command line.

      Valid “true” responses are: y, yes, true. Valid “false” responses are: n, no, false.

      If the user presses ENTER (empty input):

      • and defaultValue is non-null, return it;
      • otherwise if allowEmpty is true, return null;
      • otherwise keep prompting until valid input is given.
      Parameters:
      prompt - the message to display to the user
      defaultValue - the boolean value to return if the user provides no input (may be null)
      allowEmpty - if true, empty input is acceptable (and method returns null if no default); otherwise keep asking
      Returns:
      true if the user answered affirmatively, false if negatively, or the default/empty result as described above
    • askBoolean

      public static Boolean askBoolean(String prompt, Boolean defaultValue)
      Convenience method that calls askBoolean(String, Boolean, boolean) with allowEmpty = false.
      Parameters:
      prompt - the message to display to the user
      defaultValue - the boolean value to return if the user provides no input (may be null)
      Returns:
      true if the user answered affirmatively, false if negatively, or the default if provided
    • askBoolean

      public static Boolean askBoolean(String prompt)
      Convenience method that calls askBoolean(String, Boolean, boolean) with no default and allowEmpty = false.
      Parameters:
      prompt - the message to display to the user
      Returns:
      true if the user answered affirmatively, false if negatively
      Throws:
      IllegalStateException - if the user never provides a valid input
    • askFloat

      public static Float askFloat(String prompt, Float defaultValue, Float min, Float max, boolean allowEmpty)
      Asks the user for a float value using the specified prompt on the command line.

      The user is prompted until a valid float (optionally enforced via min/max) is provided. If defaultValue is specified and the user presses Enter, that default is returned. If defaultValue is null and the user presses Enter, the behavior depends on allowEmpty:

      • if allowEmpty is true, return null;
      • otherwise, keep prompting until valid input is given.
      Parameters:
      prompt - the prompt displayed to the user
      defaultValue - the default float if user simply presses Enter (may be null)
      min - the minimum acceptable value (inclusive), or null if no lower bound
      max - the maximum acceptable value (inclusive), or null if no upper bound
      allowEmpty - if true, empty input returns null when defaultValue is null. When defaultValue is not null, allowEmpty has no effect.
      Returns:
      a Float value entered by the user, or defaultValue, or null
    • askFloat

      public static Float askFloat(String prompt, Float defaultValue)
      Convenience method for askFloat(String, Float, Float, Float, boolean) with no min/max range and allowEmpty = false.
    • askFloat

      public static Float askFloat(String prompt)
      Convenience method for askFloat(String, Float, Float, Float, boolean) with no defaultValue or min/max range and allowEmpty = false.
    • askLong

      public static Long askLong(String prompt, Long defaultValue, Long min, Long max, boolean allowEmpty)
      Asks the user for a long value using the specified prompt on the command line.

      The user is prompted until a valid long (optionally enforced via min/max) is provided. If defaultValue is specified and the user presses Enter, that default is returned. If defaultValue is null and the user presses Enter, the behavior depends on allowEmpty:

      • if allowEmpty is true, return null;
      • otherwise, keep prompting until valid input is given.
      Parameters:
      prompt - the prompt displayed to the user
      defaultValue - the default long if user simply presses Enter (may be null)
      min - the minimum acceptable value (inclusive), or null if no lower bound
      max - the maximum acceptable value (inclusive), or null if no upper bound
      allowEmpty - if true, empty input returns null when defaultValue is null
      Returns:
      a Long value entered by the user, or defaultValue, or null
    • askLong

      public static Long askLong(String prompt, Long defaultValue)
      Convenience method for askLong(String, Long, Long, Long, boolean) with no min/max range and allowEmpty = false.
    • askLong

      public static Long askLong(String prompt)
      Convenience method for askLong(String, Long, Long, Long, boolean) with no defaultValue or min/max range and allowEmpty = false.
    • askInteger

      public static Integer askInteger(String prompt, Integer defaultValue, Integer min, Integer max, boolean allowEmpty)
      Asks the user for an integer value using the specified prompt on the command line.

      The user is prompted until a valid integer (optionally enforced via min/max) is provided. If defaultValue is specified and the user presses Enter, that default is returned. If defaultValue is null and the user presses Enter, the behavior depends on allowEmpty:

      • if allowEmpty is true, return null;
      • otherwise, keep prompting until valid input is given.
      Parameters:
      prompt - the prompt displayed to the user
      defaultValue - the default integer if the user simply presses Enter (may be null)
      min - the minimum acceptable value (inclusive), or null if no lower bound
      max - the maximum acceptable value (inclusive), or null if no upper bound
      allowEmpty - if true, empty input returns null when defaultValue is null
      Returns:
      an Integer value that the user entered, or the defaultValue, or null if allowed
    • askInteger

      public static Integer askInteger(String prompt, Integer defaultValue)
      Convenience method for askInteger(String, Integer, Integer, Integer, boolean) with no min/max range and allowEmpty = false.
    • askInteger

      public static Integer askInteger(String prompt)
      Convenience method for askInteger(String, Integer, Integer, Integer, boolean) with no defaultValue or min/max range and allowEmpty = false.
    • askString

      public static String askString(String prompt, String defaultValue, Integer minLength, Integer maxLength, boolean allowEmpty)
      Asks the user for a string value using the specified prompt on the command line.

      If the user presses ENTER without typing anything:

      • and defaultValue is non-null, return that default;
      • otherwise if allowEmpty is true, return null;
      • otherwise, keep prompting until valid input is given.

      Additionally, if minLength or maxLength are specified, the input must fall within those character bounds.

      Parameters:
      prompt - the message to display to the user
      defaultValue - the value to return if the user provides no input (may be null)
      minLength - the minimum number of characters required, or null if no lower bound
      maxLength - the maximum number of characters allowed, or null if no upper bound
      allowEmpty - if true, empty input returns null when defaultValue is null
      Returns:
      the value typed by the user, defaultValue, or null as described above
    • askString

      public static String askString(String prompt, String defaultValue)
      Convenience method for askString(String, String, Integer, Integer, boolean) with no min/max length and allowEmpty = false.
    • askString

      public static String askString(String prompt)
      Convenience method for askString(String, String, Integer, Integer, boolean) with no defaultValue or min/max length and allowEmpty = false.