File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -27,7 +27,7 @@ plugins {
2727}
2828
2929group = ' hu.zza'
30- project. version = ' 0.3.0 '
30+ project. version = ' 0.3.1 '
3131sourceCompatibility = ' 11'
3232
3333repositories {
Original file line number Diff line number Diff line change 1+ /*
2+ *
3+ * clim // Command Line Interface Menu
4+ * // https://git.zza.hu/clim
5+ *
6+ * Copyright (C) 2020-2021 Szabó László András // hu-zza
7+ *
8+ * This file is part of clim.
9+ *
10+ * clim is free software: you can redistribute it and/or modify
11+ * it under the terms of the GNU General Public License as published by
12+ * the Free Software Foundation, either version 3 of the License, or
13+ * (at your option) any later version.
14+ *
15+ * clim is distributed in the hope that it will be useful,
16+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+ * GNU General Public License for more details.
19+ *
20+ * You should have received a copy of the GNU General Public License
21+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
22+ */
23+
24+ package hu .zza .clim ;
25+
26+ public class ClimException extends RuntimeException {
27+
28+ public ClimException () {
29+ super ();
30+ }
31+
32+ public ClimException (String message ) {
33+ super (message );
34+ }
35+
36+ public ClimException (String message , Throwable cause ) {
37+ super (message , cause );
38+ }
39+
40+ public ClimException (Throwable cause ) {
41+ super (cause );
42+ }
43+ }
Original file line number Diff line number Diff line change @@ -155,7 +155,7 @@ private void tryToChooseAnOption(String input) {
155155 inputProcessorService .processInputRelatedToOptions (input , options ));
156156 } catch (Exception e ) {
157157 positionHistory .pollFirst ();
158- warnAboutInput ( input , e );
158+ throw new ClimException ( PROCESSING_FAILED . getMessage ( input ) , e );
159159 }
160160 }
161161
@@ -164,14 +164,6 @@ private void chooseOptionByProcessedInput(ProcessedInput processedInput) {
164164 position = menuStructure .get (selected ).select (processedInput );
165165 }
166166
167- private static void warnAboutInput (String input , Exception e ) {
168- if (e .getMessage () != null ) {
169- System .err .print (PROCESSING_FAILED .getMessage (input , e .getMessage ()));
170- } else {
171- System .err .print (PROCESSING_FAILED .getMessage (input , "" ));
172- e .printStackTrace ();
173- }
174- }
175167
176168 private Position returnValidatedPositionOrThrow (Position position ) {
177169 refreshOptions ();
Original file line number Diff line number Diff line change 2323
2424package hu .zza .clim ;
2525
26+ import static hu .zza .clim .menu .Message .INITIALIZATION_FAILED ;
27+
2628import com .google .gson .JsonArray ;
2729import com .google .gson .JsonElement ;
2830import com .google .gson .JsonObject ;
@@ -100,6 +102,14 @@ private void clearBuilt() {
100102 }
101103
102104 public MenuStructure build () {
105+ try {
106+ return buildMenuStructure ();
107+ } catch (Exception e ) {
108+ throw new IllegalArgumentException (INITIALIZATION_FAILED .getMessage (e .getMessage ()), e );
109+ }
110+ }
111+
112+ private MenuStructure buildMenuStructure () {
103113 clearBuilt ();
104114 findAllNodesAndLeaves ();
105115 checkBeforeBuild ();
Original file line number Diff line number Diff line change @@ -28,7 +28,7 @@ MENU_OPTION_DECORATOR=%s
2828MENU_ORDINAL_OPTION_DECORATOR =%d. %s
2929MENU_HISTORY_SEPARATOR =>
3030
31- PROCESSING_FAILED =The menu can not process the given input: '%s'%nCause: %s% n
31+ PROCESSING_FAILED =The menu can not process the given input: '%s'%n
3232INITIALIZATION_FAILED =Object initialization fails because of parameter invalidity:%n%s%n
3333
3434STOP_BECAUSE_NULL =Execution stopped because of the value of the '%s': It cannot be null.
Original file line number Diff line number Diff line change 2121 * along with this program. If not, see <https://www.gnu.org/licenses/>.
2222 */
2323
24+ import hu .zza .clim .ClimException ;
2425import hu .zza .clim .HeaderStyle ;
2526import hu .zza .clim .Menu ;
2627import hu .zza .clim .MenuBuilder ;
@@ -115,7 +116,11 @@ public static void main(String[] args) throws Exception {
115116 while (waitingForUserInput ) {
116117 menu .listOptions ();
117118 if (scanner .hasNext ()) {
118- menu .chooseOption (scanner .nextLine ());
119+ try {
120+ menu .chooseOption (scanner .nextLine ());
121+ } catch (ClimException e ) {
122+ System .err .println (e .getMessage ());
123+ }
119124 } else {
120125 waitingForUserInput = false ;
121126 }
You can’t perform that action at this time.
0 commit comments