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 Boolean
askBoolean
(String prompt) Convenience method that callsaskBoolean(String, Boolean, boolean)
with no default andallowEmpty = false
.static Boolean
askBoolean
(String prompt, Boolean defaultValue) Convenience method that callsaskBoolean(String, Boolean, boolean)
withallowEmpty = false
.static Boolean
askBoolean
(String prompt, Boolean defaultValue, boolean allowEmpty) Asks the user for a boolean value using the specified prompt on the command line.static Float
Convenience method foraskFloat(String, Float, Float, Float, boolean)
with no defaultValue or min/max range andallowEmpty = false
.static Float
Convenience method foraskFloat(String, Float, Float, Float, boolean)
with no min/max range andallowEmpty = false
.static Float
Asks the user for a float value using the specified prompt on the command line.static Integer
askInteger
(String prompt) Convenience method foraskInteger(String, Integer, Integer, Integer, boolean)
with no defaultValue or min/max range andallowEmpty = false
.static Integer
askInteger
(String prompt, Integer defaultValue) Convenience method foraskInteger(String, Integer, Integer, Integer, boolean)
with no min/max range andallowEmpty = false
.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.static Long
Convenience method foraskLong(String, Long, Long, Long, boolean)
with no defaultValue or min/max range andallowEmpty = false
.static Long
Convenience method foraskLong(String, Long, Long, Long, boolean)
with no min/max range andallowEmpty = false
.static Long
Asks the user for a long value using the specified prompt on the command line.static String
Convenience method foraskString(String, String, Integer, Integer, boolean)
with no defaultValue or min/max length andallowEmpty = false
.static String
Convenience method foraskString(String, String, Integer, Integer, boolean)
with no min/max length andallowEmpty = false
.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.
-
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
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 userdefaultValue
- the boolean value to return if the user provides no input (may benull
)allowEmpty
- iftrue
, empty input is acceptable (and method returnsnull
if no default); otherwise keep asking- Returns:
true
if the user answered affirmatively,false
if 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:
true
if the user answered affirmatively,false
if 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:
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. IfdefaultValue
is specified and the user presses Enter, that default is returned. IfdefaultValue
isnull
and the user presses Enter, the behavior depends onallowEmpty
:- if
allowEmpty
is 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), ornull
if no lower boundmax
- the maximum acceptable value (inclusive), ornull
if no upper boundallowEmpty
- iftrue
, empty input returnsnull
whendefaultValue
is null. When defaultValue is not null, allowEmpty has no effect.- Returns:
- a
Float
value 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. IfdefaultValue
is specified and the user presses Enter, that default is returned. IfdefaultValue
isnull
and the user presses Enter, the behavior depends onallowEmpty
:- if
allowEmpty
is 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), ornull
if no lower boundmax
- the maximum acceptable value (inclusive), ornull
if no upper boundallowEmpty
- iftrue
, empty input returnsnull
whendefaultValue
is null- Returns:
- a
Long
value 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. IfdefaultValue
is specified and the user presses Enter, that default is returned. IfdefaultValue
isnull
and the user presses Enter, the behavior depends onallowEmpty
:- if
allowEmpty
is 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), ornull
if no lower boundmax
- the maximum acceptable value (inclusive), ornull
if no upper boundallowEmpty
- iftrue
, empty input returnsnull
whendefaultValue
is null- Returns:
- an
Integer
value that the user entered, or thedefaultValue
, ornull
if 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
defaultValue
is non-null, return that default; - otherwise if
allowEmpty
is true, returnnull
; - otherwise, keep prompting until valid input is given.
Additionally, if
minLength
ormaxLength
are 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, ornull
if no lower boundmaxLength
- the maximum number of characters allowed, ornull
if no upper boundallowEmpty
- iftrue
, empty input returnsnull
whendefaultValue
is null- Returns:
- the value typed by the user,
defaultValue
, ornull
as 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
.
-