Class CLIHelper
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic BooleanaskBoolean(String prompt) Convenience method that callsaskBoolean(String, Boolean, boolean)with no default andallowEmpty = false.static BooleanaskBoolean(String prompt, Boolean defaultValue) Convenience method that callsaskBoolean(String, Boolean, boolean)withallowEmpty = false.static BooleanaskBoolean(String prompt, Boolean defaultValue, boolean allowEmpty) Asks the user for a boolean value using the specified prompt on the command line.static FloatConvenience method foraskFloat(String, Float, Float, Float, boolean)with no defaultValue or min/max range andallowEmpty = false.static FloatConvenience method foraskFloat(String, Float, Float, Float, boolean)with no min/max range andallowEmpty = false.static FloatAsks the user for a float value using the specified prompt on the command line.static IntegeraskInteger(String prompt) Convenience method foraskInteger(String, Integer, Integer, Integer, boolean)with no defaultValue or min/max range andallowEmpty = false.static IntegeraskInteger(String prompt, Integer defaultValue) Convenience method foraskInteger(String, Integer, Integer, Integer, boolean)with no min/max range andallowEmpty = false.static IntegeraskInteger(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.static LongConvenience method foraskLong(String, Long, Long, Long, boolean)with no defaultValue or min/max range andallowEmpty = false.static LongConvenience method foraskLong(String, Long, Long, Long, boolean)with no min/max range andallowEmpty = false.static LongAsks the user for a long value using the specified prompt on the command line.static StringConvenience method foraskString(String, String, Integer, Integer, boolean)with no defaultValue or min/max length andallowEmpty = false.static StringConvenience method foraskString(String, String, Integer, Integer, boolean)with no min/max length andallowEmpty = false.static StringaskString(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.
-
Constructor Details
-
CLIHelper
public CLIHelper()
-
-
Method Details
-
askBoolean
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
defaultValueis non-null, return it; - otherwise if
allowEmptyis true, return null; - otherwise keep prompting until valid input is given.
- Parameters:
prompt- the message to display to the userdefaultValue- the boolean value to return if the user provides no input (may benull)allowEmpty- iftrue, empty input is acceptable (and method returnsnullif no default); otherwise keep asking- Returns:
trueif the user answered affirmatively,falseif negatively, or the default/empty result as described above
- and
-
askBoolean
Convenience method that callsaskBoolean(String, Boolean, boolean)withallowEmpty = false.- Parameters:
prompt- the message to display to the userdefaultValue- the boolean value to return if the user provides no input (may benull)- Returns:
trueif the user answered affirmatively,falseif negatively, or the default if provided
-
askBoolean
Convenience method that callsaskBoolean(String, Boolean, boolean)with no default andallowEmpty = false.- Parameters:
prompt- the message to display to the user- Returns:
trueif the user answered affirmatively,falseif 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. IfdefaultValueis specified and the user presses Enter, that default is returned. IfdefaultValueisnulland the user presses Enter, the behavior depends onallowEmpty:- if
allowEmptyis true, returnnull; - otherwise, keep prompting until valid input is given.
- Parameters:
prompt- the prompt displayed to the userdefaultValue- the default float if user simply presses Enter (may benull)min- the minimum acceptable value (inclusive), ornullif no lower boundmax- the maximum acceptable value (inclusive), ornullif no upper boundallowEmpty- iftrue, empty input returnsnullwhendefaultValueis null. When defaultValue is not null, allowEmpty has no effect.- Returns:
- a
Floatvalue entered by the user, ordefaultValue, ornull
- if
-
askFloat
Convenience method foraskFloat(String, Float, Float, Float, boolean)with no min/max range andallowEmpty = false. -
askFloat
Convenience method foraskFloat(String, Float, Float, Float, boolean)with no defaultValue or min/max range andallowEmpty = 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. IfdefaultValueis specified and the user presses Enter, that default is returned. IfdefaultValueisnulland the user presses Enter, the behavior depends onallowEmpty:- if
allowEmptyis true, returnnull; - otherwise, keep prompting until valid input is given.
- Parameters:
prompt- the prompt displayed to the userdefaultValue- the default long if user simply presses Enter (may benull)min- the minimum acceptable value (inclusive), ornullif no lower boundmax- the maximum acceptable value (inclusive), ornullif no upper boundallowEmpty- iftrue, empty input returnsnullwhendefaultValueis null- Returns:
- a
Longvalue entered by the user, ordefaultValue, ornull
- if
-
askLong
Convenience method foraskLong(String, Long, Long, Long, boolean)with no min/max range andallowEmpty = false. -
askLong
Convenience method foraskLong(String, Long, Long, Long, boolean)with no defaultValue or min/max range andallowEmpty = 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. IfdefaultValueis specified and the user presses Enter, that default is returned. IfdefaultValueisnulland the user presses Enter, the behavior depends onallowEmpty:- if
allowEmptyis true, returnnull; - otherwise, keep prompting until valid input is given.
- Parameters:
prompt- the prompt displayed to the userdefaultValue- the default integer if the user simply presses Enter (may benull)min- the minimum acceptable value (inclusive), ornullif no lower boundmax- the maximum acceptable value (inclusive), ornullif no upper boundallowEmpty- iftrue, empty input returnsnullwhendefaultValueis null- Returns:
- an
Integervalue that the user entered, or thedefaultValue, ornullif allowed
- if
-
askInteger
Convenience method foraskInteger(String, Integer, Integer, Integer, boolean)with no min/max range andallowEmpty = false. -
askInteger
Convenience method foraskInteger(String, Integer, Integer, Integer, boolean)with no defaultValue or min/max range andallowEmpty = 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
defaultValueis non-null, return that default; - otherwise if
allowEmptyis true, returnnull; - otherwise, keep prompting until valid input is given.
Additionally, if
minLengthormaxLengthare specified, the input must fall within those character bounds.- Parameters:
prompt- the message to display to the userdefaultValue- the value to return if the user provides no input (may benull)minLength- the minimum number of characters required, ornullif no lower boundmaxLength- the maximum number of characters allowed, ornullif no upper boundallowEmpty- iftrue, empty input returnsnullwhendefaultValueis null- Returns:
- the value typed by the user,
defaultValue, ornullas described above
- and
-
askString
Convenience method foraskString(String, String, Integer, Integer, boolean)with no min/max length andallowEmpty = false. -
askString
Convenience method foraskString(String, String, Integer, Integer, boolean)with no defaultValue or min/max length andallowEmpty = false.
-