1 | |
---|
2 | package de.ugoe.cs.util.console.defaultcommands; |
---|
3 | |
---|
4 | import java.io.IOException; |
---|
5 | import java.io.InputStream; |
---|
6 | import java.security.InvalidParameterException; |
---|
7 | import java.util.List; |
---|
8 | |
---|
9 | import org.apache.commons.io.IOUtils; |
---|
10 | import org.apache.commons.lang.WordUtils; |
---|
11 | |
---|
12 | import de.ugoe.cs.util.console.Command; |
---|
13 | import de.ugoe.cs.util.console.CommandExecuter; |
---|
14 | import de.ugoe.cs.util.console.Console; |
---|
15 | |
---|
16 | /** |
---|
17 | * <p> |
---|
18 | * Prints the manual of a command to the Console. |
---|
19 | * </p> |
---|
20 | * |
---|
21 | * @version $Revision: $ $Date: Aug 30, 2012$ |
---|
22 | * @author 2012, last modified by $Author: sherbold$ |
---|
23 | */ |
---|
24 | public class CMDman implements Command { |
---|
25 | |
---|
26 | /* |
---|
27 | * (non-Javadoc) |
---|
28 | * |
---|
29 | * @see de.ugoe.cs.util.console.Command#run(java.util.List) |
---|
30 | */ |
---|
31 | @Override |
---|
32 | public void run(List<Object> parameters) { |
---|
33 | String command; |
---|
34 | if (parameters.size() > 0) { |
---|
35 | command = (String) parameters.get(0); |
---|
36 | } |
---|
37 | else { |
---|
38 | throw new InvalidParameterException(); |
---|
39 | } |
---|
40 | |
---|
41 | Command cmd = CommandExecuter.getInstance().getCMD(command); |
---|
42 | |
---|
43 | if (cmd != null) { |
---|
44 | InputStream manStream = |
---|
45 | ClassLoader.getSystemResourceAsStream("manuals/" + command); |
---|
46 | if (manStream != null) { |
---|
47 | try { |
---|
48 | Console.println(WordUtils.wrap(IOUtils.toString(manStream, "UTF-8"), 100).replace("$USAGE$", "Usage: " + cmd.help())); |
---|
49 | } |
---|
50 | catch (IOException e) { |
---|
51 | Console.printerrln("Failure reading man page"); |
---|
52 | Console.logException(e); |
---|
53 | } |
---|
54 | } |
---|
55 | else { |
---|
56 | Console.println("Usage: " + help()); |
---|
57 | } |
---|
58 | } |
---|
59 | else { |
---|
60 | Console.println("Command " + command + " not found."); |
---|
61 | } |
---|
62 | } |
---|
63 | |
---|
64 | /* |
---|
65 | * (non-Javadoc) |
---|
66 | * |
---|
67 | * @see de.ugoe.cs.util.console.Command#help() |
---|
68 | */ |
---|
69 | @Override |
---|
70 | public String help() { |
---|
71 | return "man <commandName>"; |
---|
72 | } |
---|
73 | |
---|
74 | } |
---|