improved javadoc
[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
25          *            to show to the user
26          * @return <code>true</code> is user answered affirmative.
27          */
28         public static boolean askBoolean(final String prompt) {
29
30                 final BufferedReader br = new BufferedReader(new InputStreamReader(
31                                 System.in));
32
33                 while (true)
34                         try {
35                                 System.out.print(prompt);
36
37                                 final String userInput = br.readLine().toLowerCase();
38
39                                 if ("y".equals(userInput) || "yes".equals(userInput)
40                                                 || "true".equals(userInput))
41                                         return true;
42
43                                 if ("n".equals(userInput) || "no".equals(userInput)
44                                                 || "false".equals(userInput))
45                                         return false;
46
47                                 System.out
48                                                 .println("Invalid input. You shall enter y/yes/true or n/no/false.");
49                         } catch (final IOException ioe) {
50                                 ioe.printStackTrace();
51                         }
52         }
53
54         /**
55          * Ask numerical long value from user on command-line.
56          *
57          * @param prompt
58          *            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                                         final long result = Long.parseLong(userInput);
74                                         return result;
75                                 } catch (final NumberFormatException e) {
76                                         System.out.println("\nError: You shall enter an integer.");
77                                 }
78                         } catch (final IOException ioe) {
79                                 ioe.printStackTrace();
80                         }
81
82                 }
83         }
84
85         /**
86          * Ask string value from user on command-line.
87          * 
88          * @param prompt
89          *            to show to the user
90          * @return value given by the user
91          */
92         public static String askString(final String prompt) {
93
94                 final BufferedReader br = new BufferedReader(new InputStreamReader(
95                                 System.in));
96
97                 while (true) {
98                         System.out.print(prompt);
99
100                         try {
101                                 final String userInput = br.readLine();
102
103                                 return userInput;
104                         } catch (final IOException ioe) {
105                                 ioe.printStackTrace();
106                         }
107
108                 }
109         }
110
111 }