2 * Svjatoslav Commons - shared library of common functionality.
3 * Copyright ©2012-2020, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 3 of the GNU Lesser General Public License
7 * or later as published by the Free Software Foundation.
10 package eu.svjatoslav.commons.commandline;
12 import java.io.BufferedReader;
13 import java.io.IOException;
14 import java.io.InputStreamReader;
17 * Command-line interface helper.
19 public class CLIHelper {
22 * Ask boolean value from user on command-line.
24 * @param prompt to show to the user
25 * @return <code>true</code> is user answered affirmative.
27 public static boolean askBoolean(final String prompt) {
29 final BufferedReader br = new BufferedReader(new InputStreamReader(
34 System.out.print(prompt);
36 String line = br.readLine();
38 final String userInput = line.toLowerCase();
40 if ("y".equals(userInput) || "yes".equals(userInput)
41 || "true".equals(userInput))
44 if ("n".equals(userInput) || "no".equals(userInput)
45 || "false".equals(userInput))
49 .println("Invalid input. You shall enter y/yes/true or n/no/false.");
50 } catch (final IOException ioe) {
51 ioe.printStackTrace();
56 * Ask numerical long value from user on command-line.
58 * @param prompt to show to the user
59 * @return value given by user
61 public static long askLong(final String prompt) {
63 final BufferedReader br = new BufferedReader(new InputStreamReader(
67 System.out.print(prompt);
70 final String userInput = br.readLine();
73 return Long.parseLong(userInput);
74 } catch (final NumberFormatException e) {
75 System.out.println("\nError: You shall enter an integer.");
77 } catch (final IOException ioe) {
78 ioe.printStackTrace();
85 * Ask string value from user on command-line.
87 * @param prompt to show to the user
88 * @return value given by the user
90 public static String askString(final String prompt) {
92 final BufferedReader br = new BufferedReader(new InputStreamReader(
96 System.out.print(prompt);
100 } catch (final IOException ioe) {
101 ioe.printStackTrace();