Code cleanup and formatting. Migrated to java 1.8.
[svjatoslav_commons.git] / src / main / java / eu / svjatoslav / commons / commandline / CLIHelper.java
1 /*
2  * Svjatoslav Commons - shared library of common functionality.
3  * Copyright ©2012-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
4  *
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.
8  */
9
10 package eu.svjatoslav.commons.commandline;
11
12 import java.io.BufferedReader;
13 import java.io.IOException;
14 import java.io.InputStreamReader;
15
16 /**
17  * Command-line interface helper.
18  */
19 public class CLIHelper {
20
21     /**
22      * Ask boolean value from user on command-line.
23      *
24      * @param prompt to show to the user
25      * @return <code>true</code> is user answered affirmative.
26      */
27     public static boolean askBoolean(final String prompt) {
28
29         final BufferedReader br = new BufferedReader(new InputStreamReader(
30                 System.in));
31
32         while (true)
33             try {
34                 System.out.print(prompt);
35
36                 String line = br.readLine();
37                 if (line != null) {
38                     final String userInput = line.toLowerCase();
39
40                     if ("y".equals(userInput) || "yes".equals(userInput)
41                             || "true".equals(userInput))
42                         return true;
43
44                     if ("n".equals(userInput) || "no".equals(userInput)
45                             || "false".equals(userInput))
46                         return false;
47                 }
48                 System.out
49                         .println("Invalid input. You shall enter y/yes/true or n/no/false.");
50             } catch (final IOException ioe) {
51                 ioe.printStackTrace();
52             }
53     }
54
55     /**
56      * Ask numerical long value from user on command-line.
57      *
58      * @param prompt to show to the user
59      * @return value given by user
60      */
61     public static long askLong(final String prompt) {
62
63         final BufferedReader br = new BufferedReader(new InputStreamReader(
64                 System.in));
65
66         while (true) {
67             System.out.print(prompt);
68
69             try {
70                 final String userInput = br.readLine();
71
72                 try {
73                     return Long.parseLong(userInput);
74                 } catch (final NumberFormatException e) {
75                     System.out.println("\nError: You shall enter an integer.");
76                 }
77             } catch (final IOException ioe) {
78                 ioe.printStackTrace();
79             }
80
81         }
82     }
83
84     /**
85      * Ask string value from user on command-line.
86      *
87      * @param prompt to show to the user
88      * @return value given by the user
89      */
90     public static String askString(final String prompt) {
91
92         final BufferedReader br = new BufferedReader(new InputStreamReader(
93                 System.in));
94
95         while (true) {
96             System.out.print(prompt);
97
98             try {
99                 final String userInput = br.readLine();
100
101                 return userInput;
102             } catch (final IOException ioe) {
103                 ioe.printStackTrace();
104             }
105
106         }
107     }
108
109 }