From 52d7ad9bb5962ca9f76bee3d57c47f3e681e6c92 Mon Sep 17 00:00:00 2001 From: Svjatoslav Agejenko Date: Tue, 1 Apr 2025 00:07:16 +0300 Subject: [PATCH] Alternative helper functions --- .../commons/cli_helper/CLIHelper.java | 52 ++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/src/main/java/eu/svjatoslav/commons/cli_helper/CLIHelper.java b/src/main/java/eu/svjatoslav/commons/cli_helper/CLIHelper.java index 041a50a..9d574e0 100755 --- a/src/main/java/eu/svjatoslav/commons/cli_helper/CLIHelper.java +++ b/src/main/java/eu/svjatoslav/commons/cli_helper/CLIHelper.java @@ -73,6 +73,14 @@ public class CLIHelper { } } + public static Boolean askBoolean(String prompt, Boolean defaultValue) { + return askBoolean(prompt, defaultValue, false); + } + + public static Boolean askBoolean(String prompt) { + return askBoolean(prompt, null, false); + } + /** * Asks the user for a float value using the specified prompt on the command line. * The user is prompted until a valid float (within optional ranges) is provided. @@ -140,6 +148,14 @@ public class CLIHelper { } } + public static Float askFloat(String prompt, Float defaultValue) { + return askFloat(prompt, defaultValue, null, null, false); + } + + public static Float askFloat(String prompt) { + return askFloat(prompt, null, null, null, false); + } + /** * Asks the user for a long value using the specified prompt on the command line. * The user is prompted until a valid long (within optional ranges) is provided. @@ -207,6 +223,15 @@ public class CLIHelper { } } + public static Long askLong(String prompt, Long defaultValue) { + return askLong(prompt, defaultValue, null, null, false); + } + + public static Long askLong(String prompt) { + return askLong(prompt, null, null, null, false); + } + + /** * Asks the user for an integer value using the specified prompt on the command line. * The user is prompted until a valid integer (within optional ranges) is provided. @@ -276,6 +301,13 @@ public class CLIHelper { } } + public static Integer askInteger (String prompt, Integer defaultValue) { + return askInteger(prompt, defaultValue, null, null, false); + } + + public static Integer askInteger (String prompt) { + return askInteger(prompt, null, null, null, false); + } /** * Asks the user for a string value using the specified prompt on the command line. * If the user presses ENTER without typing anything and {@code defaultValue} is non-null, @@ -285,7 +317,7 @@ public class CLIHelper { * @param defaultValue the value to return if the user provides no input * @return the value typed by the user, or {@code defaultValue} if empty input */ - public static String askString(String prompt, String defaultValue, boolean allowEmpty) { + public static String askString(String prompt, String defaultValue, Integer minLength, Integer maxLength, boolean allowEmpty) { while (true) { // If we have a defaultValue, display it in brackets; otherwise display no default String displayPrompt = prompt @@ -311,8 +343,26 @@ public class CLIHelper { } } + if (minLength != null && input.length() < minLength) { + System.out.println("Input must be at least " + minLength + " characters long."); + continue; + } + + if (maxLength != null && input.length() > maxLength) { + System.out.println("Input must be at most " + maxLength + " characters long."); + continue; + } + return input; } + } + public static String askString(String prompt, String defaultValue) { + return askString(prompt, defaultValue, null, null, false); } + + public static String askString(String prompt) { + return askString(prompt, null, null, null, false); + } + } -- 2.20.1