11/*
22 * JBoss, Home of Professional Open Source
33 * Copyright 2014 Red Hat Inc. and/or its affiliates and other contributors
4- * as indicated by the @authors tag. All rights reserved.
4+ * as indicated by the @authors tag
55 * See the copyright.txt in the distribution for a
66 * full listing of individual contributors.
77 *
1919 */
2020package org .aesh ;
2121
22+ import java .io .IOException ;
23+
2224import org .aesh .command .Command ;
2325import org .aesh .command .CommandDefinition ;
2426import org .aesh .command .CommandResult ;
2830import org .aesh .command .registry .CommandRegistryException ;
2931import org .aesh .command .settings .Settings ;
3032import org .aesh .command .settings .SettingsBuilder ;
31- import org .aesh .readline .Prompt ;
3233import org .aesh .console .ReadlineConsole ;
34+ import org .aesh .readline .Prompt ;
3335import org .aesh .terminal .Connection ;
3436
35- import java .io .IOException ;
36-
3737/**
3838 * Use the AeshConsoleRunner when you want to easily create an interactive CLI application.
3939 *
40- * @author <a href="mailto:stalep@gmail.com">Ståle Pedersen</a>
40+ * @author Aesh team
4141 */
4242public class AeshConsoleRunner {
4343 private AeshCommandRegistryBuilder <CommandInvocation > registryBuilder ;
@@ -54,7 +54,7 @@ public static AeshConsoleRunner builder() {
5454 }
5555
5656 public AeshConsoleRunner commands (Class <? extends Command >... commands ) {
57- if (commands != null ) {
57+ if (commands != null ) {
5858 ensureRegistryBuilderInitialized ();
5959 try {
6060 registryBuilder .commands (commands );
@@ -66,7 +66,7 @@ public AeshConsoleRunner commands(Class<? extends Command>... commands) {
6666 }
6767
6868 public AeshConsoleRunner command (Class <? extends Command > command ) {
69- if (command != null ) {
69+ if (command != null ) {
7070 ensureRegistryBuilderInitialized ();
7171 try {
7272 registryBuilder .command (command );
@@ -78,24 +78,24 @@ public AeshConsoleRunner command(Class<? extends Command> command) {
7878 }
7979
8080 public AeshConsoleRunner commandRegistryBuilder (AeshCommandRegistryBuilder <CommandInvocation > commandRegistryBuilder ) {
81- if (registryBuilder != null ) {
81+ if (registryBuilder != null ) {
8282 throw new RuntimeException ("Cannot set CommandRegistryBuilder after it has been initialized. " +
8383 "CommandRegistryBuilder must be set before adding any commands." );
8484 }
85- if (commandRegistryBuilder != null ) {
85+ if (commandRegistryBuilder != null ) {
8686 this .registryBuilder = commandRegistryBuilder ;
8787 }
8888 return this ;
8989 }
9090
9191 private void ensureRegistryBuilderInitialized () {
92- if (registryBuilder == null ) {
92+ if (registryBuilder == null ) {
9393 registryBuilder = AeshCommandRegistryBuilder .builder ();
9494 }
9595 }
9696
9797 public AeshConsoleRunner settings (Settings settings ) {
98- if (settings != null )
98+ if (settings != null )
9999 this .settings = settings ;
100100 return this ;
101101 }
@@ -106,17 +106,17 @@ public AeshConsoleRunner connection(Connection connection) {
106106 }
107107
108108 public AeshConsoleRunner prompt (String prompt ) {
109- if (prompt != null )
109+ if (prompt != null )
110110 this .prompt = new Prompt (prompt );
111- if (console != null && console .running ())
111+ if (console != null && console .running ())
112112 console .setPrompt (this .prompt );
113113 return this ;
114114 }
115115
116116 public AeshConsoleRunner prompt (Prompt prompt ) {
117- if (prompt != null )
117+ if (prompt != null )
118118 this .prompt = prompt ;
119- if (console != null && console .running ())
119+ if (console != null && console .running ())
120120 console .setPrompt (this .prompt );
121121 return this ;
122122 }
@@ -132,29 +132,28 @@ public AeshConsoleRunner addExitCommand() {
132132 }
133133
134134 public void start () {
135- if (console == null ) {
135+ if (console == null ) {
136136 init ();
137- if (prompt != null )
137+ if (prompt != null )
138138 console .setPrompt (prompt );
139139 try {
140140 console .start ();
141- }
142- catch (IOException e ) {
143- throw new RuntimeException ("Exception while starting the console: " +e .getMessage ());
141+ } catch (IOException e ) {
142+ throw new RuntimeException ("Exception while starting the console: " + e .getMessage ());
144143 }
145144 }
146145 }
147146
148147 public void stop () {
149- if (console != null && console .running ())
148+ if (console != null && console .running ())
150149 console .stop ();
151150 }
152151
153152 @ SuppressWarnings ("unchecked" )
154153 private void init () {
155154 // Build the command registry from the builder
156155 CommandRegistry <CommandInvocation > builtRegistry = null ;
157- if (registryBuilder != null ) {
156+ if (registryBuilder != null ) {
158157 try {
159158 builtRegistry = registryBuilder .create ();
160159 } catch (Exception e ) {
@@ -163,58 +162,58 @@ private void init() {
163162 }
164163
165164 // Check if both builder and settings.commandRegistry have commands
166- if (builtRegistry != null && !builtRegistry .getAllCommandNames ().isEmpty () &&
165+ if (builtRegistry != null && !builtRegistry .getAllCommandNames ().isEmpty () &&
167166 settings != null && settings .commandRegistry () != null &&
168167 !settings .commandRegistry ().getAllCommandNames ().isEmpty ()) {
169- throw new RuntimeException ("Cannot define commands in both AeshConsoleRunner (via command() or commandRegistryBuilder()) " +
170- "and Settings.commandRegistry(). Please use only one method to specify commands." );
168+ throw new RuntimeException (
169+ "Cannot define commands in both AeshConsoleRunner (via command() or commandRegistryBuilder()) " +
170+ "and Settings.commandRegistry(). Please use only one method to specify commands." );
171171 }
172172
173173 // Determine which registry to use
174174 CommandRegistry <CommandInvocation > finalRegistry = null ;
175- if (builtRegistry != null && !builtRegistry .getAllCommandNames ().isEmpty ()) {
175+ if (builtRegistry != null && !builtRegistry .getAllCommandNames ().isEmpty ()) {
176176 finalRegistry = builtRegistry ;
177- } else if (settings != null && settings .commandRegistry () != null &&
177+ } else if (settings != null && settings .commandRegistry () != null &&
178178 !settings .commandRegistry ().getAllCommandNames ().isEmpty ()) {
179179 finalRegistry = settings .commandRegistry ();
180180 }
181181
182182 // Validate that we have at least one command
183- if (finalRegistry == null || finalRegistry .getAllCommandNames ().isEmpty ()) {
183+ if (finalRegistry == null || finalRegistry .getAllCommandNames ().isEmpty ()) {
184184 throw new RuntimeException ("No commands added, nothing to run" );
185185 }
186186
187187 try {
188- if (settings == null ) {
188+ if (settings == null ) {
189189 settings = SettingsBuilder .builder ()
190- .commandRegistry (finalRegistry )
191- .enableAlias (false )
192- .enableExport (false )
193- .enableMan (false )
194- .persistHistory (false )
195- .connection (connection )
196- .build ();
190+ .commandRegistry (finalRegistry )
191+ .enableAlias (false )
192+ .enableExport (false )
193+ .enableMan (false )
194+ .persistHistory (false )
195+ .connection (connection )
196+ .build ();
197197 }
198198 // User added their own settings object, but we need to add or replace the registry
199- else if (settings .commandRegistry () == null ||
199+ else if (settings .commandRegistry () == null ||
200200 settings .commandRegistry ().getAllCommandNames ().isEmpty ()) {
201201 SettingsBuilder settingsBuilder = new SettingsBuilder (settings )
202- .commandRegistry (finalRegistry );
202+ .commandRegistry (finalRegistry );
203203
204- if (connection != null )
204+ if (connection != null )
205205 settingsBuilder .connection (connection );
206206
207207 settings = settingsBuilder .build ();
208208 }
209209
210210 console = new ReadlineConsole (settings );
211- }
212- catch (Exception e ) {
211+ } catch (Exception e ) {
213212 throw new RuntimeException ("Error when initializing console: " + e .getMessage (), e );
214213 }
215214 }
216215
217- @ CommandDefinition (name = "exit" , description = "exit the program" , aliases = {"quit" })
216+ @ CommandDefinition (name = "exit" , description = "exit the program" , aliases = { "quit" })
218217 public static class ExitCommand implements Command {
219218 @ Override
220219 public CommandResult execute (CommandInvocation commandInvocation ) {
0 commit comments