X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Feu%2Fsvjatoslav%2Fcommons%2Fcommandline%2FCLIHelper.java;h=2cbfb8df0b08f10973ebaa5cd5651015160dbca1;hb=443c6a564efa9f5868fd81c0db23a480cbe3cab4;hp=5537df872897ad46fcb59879b0bf75088f5ca7d9;hpb=cf965fda534cc562368c9f2a3f34475e2519fcdc;p=svjatoslav_commons.git diff --git a/src/main/java/eu/svjatoslav/commons/commandline/CLIHelper.java b/src/main/java/eu/svjatoslav/commons/commandline/CLIHelper.java index 5537df8..2cbfb8d 100755 --- a/src/main/java/eu/svjatoslav/commons/commandline/CLIHelper.java +++ b/src/main/java/eu/svjatoslav/commons/commandline/CLIHelper.java @@ -1,10 +1,10 @@ /* * Svjatoslav Commons - shared library of common functionality. - * Copyright ©2012-2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu - * + * Copyright ©2012-2018, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu + * * This program is free software; you can redistribute it and/or - * modify it under the terms of version 2 of the GNU General Public License - * as published by the Free Software Foundation. + * modify it under the terms of version 3 of the GNU Lesser General Public License + * or later as published by the Free Software Foundation. */ package eu.svjatoslav.commons.commandline; @@ -18,82 +18,90 @@ import java.io.InputStreamReader; */ public class CLIHelper { - /** - * Ask boolean value from user on command-line. - */ - public static boolean askBoolean(final String prompt) { - - final BufferedReader br = new BufferedReader(new InputStreamReader( - System.in)); - - while (true) - try { - System.out.print(prompt); - - final String userInput = br.readLine().toLowerCase(); - - if ("y".equals(userInput) || "yes".equals(userInput) - || "true".equals(userInput)) - return true; - - if ("n".equals(userInput) || "no".equals(userInput) - || "false".equals(userInput)) - return false; - - System.out - .println("Invalid input. You shall enter y/yes/true or n/no/false."); - } catch (final IOException ioe) { - ioe.printStackTrace(); - } - } - - /** - * Ask numerical long value from user on command-line. - */ - public static long askLong(final String prompt) { - - final BufferedReader br = new BufferedReader(new InputStreamReader( - System.in)); - - while (true) { - System.out.print(prompt); - - try { - final String userInput = br.readLine(); - - try { - final long result = Long.parseLong(userInput); - return result; - } catch (final NumberFormatException e) { - System.out.println("\nError: You shall enter an integer."); - } - } catch (final IOException ioe) { - ioe.printStackTrace(); - } - - } - } - - /** - * Ask string value from user on command-line. - */ - public static String askString(final String prompt) { - - final BufferedReader br = new BufferedReader(new InputStreamReader( - System.in)); - - while (true) { - System.out.print(prompt); - - try { - final String userInput = br.readLine(); - - return userInput; - } catch (final IOException ioe) { - ioe.printStackTrace(); - } - - } - } + /** + * Ask boolean value from user on command-line. + * + * @param prompt to show to the user + * @return true is user answered affirmative. + */ + public static boolean askBoolean(final String prompt) { + + final BufferedReader br = new BufferedReader(new InputStreamReader( + System.in)); + + while (true) + try { + System.out.print(prompt); + + String line = br.readLine(); + if (line != null) { + final String userInput = line.toLowerCase(); + + if ("y".equals(userInput) || "yes".equals(userInput) + || "true".equals(userInput)) + return true; + + if ("n".equals(userInput) || "no".equals(userInput) + || "false".equals(userInput)) + return false; + } + System.out + .println("Invalid input. You shall enter y/yes/true or n/no/false."); + } catch (final IOException ioe) { + ioe.printStackTrace(); + } + } + + /** + * Ask numerical long value from user on command-line. + * + * @param prompt to show to the user + * @return value given by user + */ + public static long askLong(final String prompt) { + + final BufferedReader br = new BufferedReader(new InputStreamReader( + System.in)); + + while (true) { + System.out.print(prompt); + + try { + final String userInput = br.readLine(); + + try { + return Long.parseLong(userInput); + } catch (final NumberFormatException e) { + System.out.println("\nError: You shall enter an integer."); + } + } catch (final IOException ioe) { + ioe.printStackTrace(); + } + + } + } + + /** + * Ask string value from user on command-line. + * + * @param prompt to show to the user + * @return value given by the user + */ + public static String askString(final String prompt) { + + final BufferedReader br = new BufferedReader(new InputStreamReader( + System.in)); + + while (true) { + System.out.print(prompt); + + try { + return br.readLine(); + } catch (final IOException ioe) { + ioe.printStackTrace(); + } + + } + } }