From: Svjatoslav Agejenko Date: Sun, 30 Mar 2025 20:54:52 +0000 (+0300) Subject: Improve default value handling X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=commitdiff_plain;h=1fb92dc40cb60bf962a2277a480394cd6b006591;p=cli-helper.git Improve default value handling --- 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 fcdd8e9..041a50a 100755 --- a/src/main/java/eu/svjatoslav/commons/cli_helper/CLIHelper.java +++ b/src/main/java/eu/svjatoslav/commons/cli_helper/CLIHelper.java @@ -33,18 +33,31 @@ public class CLIHelper { * {@code false} if they answered negatively, * or {@code defaultValue} if input was empty */ - public static boolean askBoolean(final String prompt, final Boolean defaultValue) { + public static Boolean askBoolean(final String prompt, final Boolean defaultValue, boolean allowEmpty) { while (true) { - String finalPrompt = prompt; - if (defaultValue != null){ - // Show the user something like "Continue? [Y/n]: " or "Continue? [y/N]: " - finalPrompt = prompt + " [" + (defaultValue ? "Y/n" : "y/N") + "]: "; - } - String line = askString(finalPrompt); + // If we have a defaultValue, display it in brackets; otherwise display no default + String displayPrompt = prompt + + (defaultValue != null ? " [" + defaultValue + "]" : "") + + ": "; + + // Read user input + System.out.print(displayPrompt); + String line = new Scanner(System.in).nextLine().trim(); + // If user just pressed Enter: + if (line.isEmpty()){ + + // If a defaultValue was supplied, return it + if (defaultValue != null) { + return defaultValue; + } - if ((defaultValue != null) && (line == null)) { - return defaultValue; + if (allowEmpty) { + return null; + } else { + System.out.println("Input cannot be empty. Please enter y/yes/true or n/no/false."); + continue; + } } final String userInput = line.toLowerCase(); @@ -76,7 +89,7 @@ public class CLIHelper { * @param max The maximum acceptable value (inclusive), or null if no upper bound * @return A Float value that the user entered, or the defaultValue, or null if no defaultValue was given */ - public static Float askFloat(String prompt, Float defaultValue, Float min, Float max) { + public static Float askFloat(String prompt, Float defaultValue, Float min, Float max, boolean allowEmpty) { while (true) { // If we have a defaultValue, display it in brackets; otherwise display no default String displayPrompt = prompt @@ -87,12 +100,21 @@ public class CLIHelper { System.out.print(displayPrompt); String input = new Scanner(System.in).nextLine().trim(); - // If user just pressed Enter: - if (input.isEmpty()) { - // If a defaultValue was supplied, return it; else return null - return defaultValue; + if (input.isEmpty()){ + // If a defaultValue was supplied, return it + if (defaultValue != null) { + return defaultValue; + } + + if (allowEmpty) { + return null; + } else { + System.out.println("Input cannot be empty. Please enter a valid float."); + continue; + } } + // Parse float value try { float parsedValue = Float.parseFloat(input); @@ -134,7 +156,7 @@ public class CLIHelper { * @param max The maximum acceptable value (inclusive), or null if no upper bound * @return A Long value that the user entered, or the defaultValue, or null if no defaultValue was given */ - public static Long askLong(String prompt, Long defaultValue, Long min, Long max) { + public static Long askLong(String prompt, Long defaultValue, Long min, Long max, boolean allowEmpty) { while (true) { // If we have a defaultValue, display it in brackets; otherwise display no default String displayPrompt = prompt @@ -145,10 +167,19 @@ public class CLIHelper { System.out.print(displayPrompt); String input = new Scanner(System.in).nextLine().trim(); - // If user just pressed Enter: - if (input.isEmpty()) { - // If a defaultValue was supplied, return it; else return null - return defaultValue; + if (input.isEmpty()){ + + // If a defaultValue was supplied, return it + if (defaultValue != null) { + return defaultValue; + } + + if (allowEmpty) { + return null; + } else { + System.out.println("Input cannot be empty. Please enter a valid long."); + continue; + } } // Parse long value @@ -186,13 +217,14 @@ public class CLIHelper { *
  • If {@code max} is not {@code null}, we enforce that the entered integer is {@code <= max}.
  • * * - * @param prompt The prompt displayed to the user - * @param defaultValue The default long if user simply presses Enter (may be null) - * @param min The minimum acceptable value (inclusive), or null if no lower bound - * @param max The maximum acceptable value (inclusive), or null if no upper bound - * @return An integer value that the user entered, or the defaultValue, or null if no defaultValue was given + * @param prompt The prompt displayed to the user. + * @param defaultValue The default long if user simply presses Enter (may be null). + * @param min The minimum acceptable value (inclusive), or null if no lower bound. + * @param max The maximum acceptable value (inclusive), or null if no upper bound. + * @param allowEmpty If true, empty input is acceptable. Otherwise keep asking user. + * @return An integer value that the user entered, or the defaultValue, or null if no defaultValue was given. */ - public static Integer askInteger(String prompt, Integer defaultValue, Integer min, Integer max) { + public static Integer askInteger(String prompt, Integer defaultValue, Integer min, Integer max, boolean allowEmpty) { while (true) { // If we have a defaultValue, display it in brackets; otherwise display no default String displayPrompt = prompt @@ -204,9 +236,19 @@ public class CLIHelper { String input = new Scanner(System.in).nextLine().trim(); // If user just pressed Enter: - if (input.isEmpty()) { - // If a defaultValue was supplied, return it; else return null - return defaultValue; + if (input.isEmpty()){ + + // If a defaultValue was supplied, return it + if (defaultValue != null) { + return defaultValue; + } + + if (allowEmpty) { + return null; + } else { + System.out.println("Input cannot be empty. Please enter a valid integer."); + continue; + } } // Parse long value @@ -243,23 +285,34 @@ 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) { - Scanner scanner = new Scanner(System.in); - System.out.print(prompt + (defaultValue != null ? " [" + defaultValue + "]" : "") + ": "); - String input = scanner.nextLine().trim(); + public static String askString(String prompt, String defaultValue, boolean allowEmpty) { + while (true) { + // If we have a defaultValue, display it in brackets; otherwise display no default + String displayPrompt = prompt + + (defaultValue != null ? " [" + defaultValue + "]" : "") + + ": "; - // Return the default if user just pressed ENTER - return input.isEmpty() ? defaultValue : input; - } + // Read user input + System.out.print(displayPrompt); + String input = new Scanner(System.in).nextLine().trim(); + + if (input.isEmpty()){ + + // If a defaultValue was supplied, return it + if (defaultValue != null) { + return defaultValue; + } + + if (allowEmpty) { + return null; + } else { + System.out.println("Input cannot be empty. Please enter a valid string."); + continue; + } + } + + return input; + } - /** - * Asks the user for a string value using the specified prompt on the command line. - * If the user presses ENTER without typing anything, {@code null} is returned. - * - * @param prompt the message to display to the user - * @return the value typed by the user, or {@code null} if empty input - */ - public static String askString(String prompt) { - return askString(prompt, null); } }