1 package fr.ifremer.quadrige2.core.action; 2 3 /*- 4 * #%L 5 * Quadrige2 Core :: Quadrige2 Core Shared 6 * $Id:$ 7 * $HeadURL:$ 8 * %% 9 * Copyright (C) 2017 Ifremer 10 * %% 11 * This program is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Affero General Public License as published by 13 * the Free Software Foundation, either version 3 of the License, or 14 * (at your option) any later version. 15 * 16 * This program is distributed in the hope that it will be useful, 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 * GNU General Public License for more details. 20 * 21 * You should have received a copy of the GNU Affero General Public License 22 * along with this program. If not, see <http://www.gnu.org/licenses/>. 23 * #L% 24 */ 25 26 27 28 import org.apache.commons.lang3.StringUtils; 29 30 import java.util.Scanner; 31 32 /** 33 * <p>CommandLines class.</p> 34 */ 35 public class CommandLines { 36 37 /** 38 * <p>Constructor for CommandLines.</p> 39 */ 40 protected CommandLines() { 41 42 } 43 44 /** 45 * <p>readNotBlankInput.</p> 46 * 47 * @param message a {@link java.lang.String} object. 48 * @return a {@link java.lang.String} object. 49 */ 50 public static String readNotBlankInput(String message) { 51 return readInput(message, null, true); 52 } 53 54 /** 55 * <p>readInput.</p> 56 * 57 * @param message a {@link java.lang.String} object. 58 * @param defaultValue a {@link java.lang.String} object. 59 * @param mandatory a boolean. 60 * @return a {@link java.lang.String} object. 61 */ 62 public static String readInput(String message, String defaultValue, boolean mandatory) { 63 64 Scanner scanIn = new Scanner(System.in); 65 String inputValue = null; 66 while (inputValue == null) { 67 System.out.print(message.trim()); 68 if (StringUtils.isNotEmpty(defaultValue)) { 69 System.out.print(String.format(" [%s]", defaultValue)); 70 } 71 System.out.print(": "); 72 inputValue = scanIn.nextLine(); 73 if (StringUtils.isBlank(inputValue)) { 74 // A default exists: use it 75 if (StringUtils.isNotEmpty(defaultValue)) { 76 inputValue = defaultValue; 77 } 78 // No default value, but mandatory: prepare for a new iteration 79 else if (mandatory) { 80 inputValue = null; 81 } 82 } 83 } 84 // scanIn.close(); 85 86 return inputValue; 87 } 88 89 }