2 * Svjatoslav Commons - shared library of common functionality. Author: Svjatoslav Agejenko.
3 * This project is released under Creative Commons Zero (CC0) license.
5 package eu.svjatoslav.commons.commandline;
7 import java.io.BufferedReader;
8 import java.io.IOException;
9 import java.io.InputStreamReader;
12 * Command-line interface helper.
14 public class CLIHelper {
17 * Ask boolean value from user on command-line.
19 * @param prompt to show to the user
20 * @return <code>true</code> is user answered affirmative.
22 public static boolean askBoolean(final String prompt) {
24 final BufferedReader br = new BufferedReader(new InputStreamReader(
29 System.out.print(prompt);
31 String line = br.readLine();
33 final String userInput = line.toLowerCase();
35 if ("y".equals(userInput) || "yes".equals(userInput)
36 || "true".equals(userInput))
39 if ("n".equals(userInput) || "no".equals(userInput)
40 || "false".equals(userInput))
44 .println("Invalid input. You shall enter y/yes/true or n/no/false.");
45 } catch (final IOException ioe) {
46 ioe.printStackTrace();
51 * Ask numerical long value from user on command-line.
53 * @param prompt to show to the user
54 * @return value given by user
56 public static long askLong(final String prompt) {
58 final BufferedReader br = new BufferedReader(new InputStreamReader(
62 System.out.print(prompt);
65 final String userInput = br.readLine();
68 return Long.parseLong(userInput);
69 } catch (final NumberFormatException e) {
70 System.out.println("\nError: You shall enter an integer.");
72 } catch (final IOException ioe) {
73 ioe.printStackTrace();
80 * Ask string value from user on command-line.
82 * @param prompt to show to the user
83 * @return value given by the user
85 public static String askString(final String prompt) {
87 final BufferedReader br = new BufferedReader(new InputStreamReader(
91 System.out.print(prompt);
95 } catch (final IOException ioe) {
96 ioe.printStackTrace();