Alternative helper functions
authorSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Mon, 31 Mar 2025 21:07:16 +0000 (00:07 +0300)
committerSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Mon, 31 Mar 2025 21:07:16 +0000 (00:07 +0300)
src/main/java/eu/svjatoslav/commons/cli_helper/CLIHelper.java

index 041a50a..9d574e0 100755 (executable)
@@ -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);
+    }
+
 }