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

Last change on this file since 718 was 718, checked in by sherbold, 12 years ago
  • added command man that prints manuals of commands to the console
  • added manuals für the commands exec, exit, listCommands, man, and trainMarkovModel
  • 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.security.InvalidParameterException;
7import java.util.List;
8
9import org.apache.commons.io.IOUtils;
10import org.apache.commons.lang.WordUtils;
11
12import de.ugoe.cs.util.console.Command;
13import de.ugoe.cs.util.console.CommandExecuter;
14import 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 */
24public 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}
Note: See TracBrowser for help on using the repository browser.