more elegant parameter initialization
[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         public static boolean askBoolean(final String prompt) {
25
26                 final BufferedReader br = new BufferedReader(new InputStreamReader(
27                                 System.in));
28
29                 while (true)
30                         try {
31                                 System.out.print(prompt);
32
33                                 final String userInput = br.readLine().toLowerCase();
34
35                                 if ("y".equals(userInput) || "yes".equals(userInput)
36                                                 || "true".equals(userInput))
37                                         return true;
38
39                                 if ("n".equals(userInput) || "no".equals(userInput)
40                                                 || "false".equals(userInput))
41                                         return false;
42
43                                 System.out
44                                                 .println("Invalid input. You shall enter y/yes/true or n/no/false.");
45                         } catch (final IOException ioe) {
46                                 ioe.printStackTrace();
47                         }
48         }
49
50         /**
51          * Ask numerical long value from user on command-line.
52          */
53         public static long askLong(final String prompt) {
54
55                 final BufferedReader br = new BufferedReader(new InputStreamReader(
56                                 System.in));
57
58                 while (true) {
59                         System.out.print(prompt);
60
61                         try {
62                                 final String userInput = br.readLine();
63
64                                 try {
65                                         final long result = Long.parseLong(userInput);
66                                         return result;
67                                 } catch (final NumberFormatException e) {
68                                         System.out.println("\nError: You shall enter an integer.");
69                                 }
70                         } catch (final IOException ioe) {
71                                 ioe.printStackTrace();
72                         }
73
74                 }
75         }
76
77         /**
78          * Ask string value from user on command-line.
79          */
80         public static String askString(final String prompt) {
81
82                 final BufferedReader br = new BufferedReader(new InputStreamReader(
83                                 System.in));
84
85                 while (true) {
86                         System.out.print(prompt);
87
88                         try {
89                                 final String userInput = br.readLine();
90
91                                 return userInput;
92                         } catch (final IOException ioe) {
93                                 ioe.printStackTrace();
94                         }
95
96                 }
97         }
98
99 }