source: trunk/java-utils/src/main/java/de/ugoe/cs/util/console/defaultcommands/CMDman.java @ 766

Last change on this file since 766 was 766, checked in by sherbold, 12 years ago
  • Property svn:mime-type set to text/plain
File size: 1.9 KB
Line 
1
2package de.ugoe.cs.util.console.defaultcommands;
3
4import java.io.IOException;
5import java.io.InputStream;
6import java.util.List;
7
8import org.apache.commons.io.IOUtils;
9import org.apache.commons.lang.WordUtils;
10
11import de.ugoe.cs.util.console.Command;
12import de.ugoe.cs.util.console.CommandExecuter;
13import 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 */
23public 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}
Note: See TracBrowser for help on using the repository browser.