View Javadoc

1   /*******************************************************************************
2    * Copyright (c) 2007, 2014 Massimiliano Ziccardi
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *******************************************************************************/
16  package it.jnrpe.server.console;
17  
18  import it.jnrpe.JNRPE;
19  
20  import java.io.IOException;
21  import java.util.Map;
22  
23  import jline.console.ConsoleReader;
24  
25  public class HelpCommand extends ConsoleCommand {
26  
27  	public final static String NAME = "help";
28  	private final Map<String, IConsoleCommand> commandMap;
29  
30  	public HelpCommand(ConsoleReader consoleReader, JNRPE jnrpe,
31  			Map<String, IConsoleCommand> commands) {
32  		super(consoleReader, jnrpe);
33  		commandMap = commands;
34  	}
35  
36  	public boolean execute(String[] args) throws Exception {
37  		if (args == null || args.length == 0) {
38  			getConsole().println("Available commands are : ");
39  			for (IConsoleCommand command : commandMap.values()) {
40  				// getConsole().println("  * \u001B[1m" + command.getName() +
41  				// "\u001B[0m");
42  				getConsole().println("  * " + highlight(command.getName()));
43  			}
44  
45  			return false;
46  		}
47  		if (args.length != 1) {
48  			getConsole().println(
49  					"Only one parameter can be specified for the help command");
50  			return false;
51  		}
52  
53  		IConsoleCommand command = commandMap.get(args[0].toLowerCase());
54  		if (command == null) {
55  			getConsole().println("Unknown command : '" + args[0] + "'");
56  			return false;
57  		}
58  
59  		command.printHelp();
60  
61  		return false;
62  	}
63  
64  	public String getName() {
65  		return NAME;
66  	}
67  
68  	public String getCommandLine() {
69  		return "[COMMAND NAME]";
70  	}
71  
72  	public void printHelp() throws IOException {
73  		getConsole().println("Command Line: help " + getCommandLine());
74  		getConsole().println(
75  				"   Without parameters shows the list of available commands");
76  		getConsole().println(
77  				"   otherwise prints some help about the specified command");
78  	}
79  
80  }