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.
24 public static boolean askBoolean(final String prompt) {
26 final BufferedReader br = new BufferedReader(new InputStreamReader(
31 System.out.print(prompt);
33 final String userInput = br.readLine().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 public static long askLong(final String prompt) {
55 final BufferedReader br = new BufferedReader(new InputStreamReader(
59 System.out.print(prompt);
62 final String userInput = br.readLine();
65 final long result = Long.parseLong(userInput);
67 } catch (final NumberFormatException e) {
68 System.out.println("\nError: You shall enter an integer.");
70 } catch (final IOException ioe) {
71 ioe.printStackTrace();
78 * Ask string value from user on command-line.
80 public static String askString(final String prompt) {
82 final BufferedReader br = new BufferedReader(new InputStreamReader(
86 System.out.print(prompt);
89 final String userInput = br.readLine();
92 } catch (final IOException ioe) {
93 ioe.printStackTrace();