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