1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
41
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 }