2 * Svjatoslav Commons - shared library of common functionality.
3 * Copyright ©2012-2014, 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.
26 * @return <code>true</code> is user answered affirmative.
28 public static boolean askBoolean(final String prompt) {
30 final BufferedReader br = new BufferedReader(new InputStreamReader(
35 System.out.print(prompt);
37 final String userInput = br.readLine().toLowerCase();
39 if ("y".equals(userInput) || "yes".equals(userInput)
40 || "true".equals(userInput))
43 if ("n".equals(userInput) || "no".equals(userInput)
44 || "false".equals(userInput))
48 .println("Invalid input. You shall enter y/yes/true or n/no/false.");
49 } catch (final IOException ioe) {
50 ioe.printStackTrace();
55 * Ask numerical long value from user on command-line.
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 final long result = Long.parseLong(userInput);
75 } catch (final NumberFormatException e) {
76 System.out.println("\nError: You shall enter an integer.");
78 } catch (final IOException ioe) {
79 ioe.printStackTrace();
86 * Ask string value from user on command-line.
90 * @return value given by the user
92 public static String askString(final String prompt) {
94 final BufferedReader br = new BufferedReader(new InputStreamReader(
98 System.out.print(prompt);
101 final String userInput = br.readLine();
104 } catch (final IOException ioe) {
105 ioe.printStackTrace();