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

Last change on this file since 927 was 927, checked in by sherbold, 12 years ago
  • added copyright under the Apache License, Version 2.0
  • Property svn:mime-type set to text/plain
File size: 2.5 KB
Line 
1//   Copyright 2012 Georg-August-Universität Göttingen, Germany
2//
3//   Licensed under the Apache License, Version 2.0 (the "License");
4//   you may not use this file except in compliance with the License.
5//   You may obtain a copy of the License at
6//
7//       http://www.apache.org/licenses/LICENSE-2.0
8//
9//   Unless required by applicable law or agreed to in writing, software
10//   distributed under the License is distributed on an "AS IS" BASIS,
11//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//   See the License for the specific language governing permissions and
13//   limitations under the License.
14
15package de.ugoe.cs.util.console.defaultcommands;
16
17import java.io.IOException;
18import java.io.InputStream;
19import java.util.List;
20
21import org.apache.commons.io.IOUtils;
22import org.apache.commons.lang.WordUtils;
23
24import de.ugoe.cs.util.console.Command;
25import de.ugoe.cs.util.console.CommandExecuter;
26import de.ugoe.cs.util.console.Console;
27
28/**
29 * <p>
30 * Prints the manual of a command to the Console.
31 * </p>
32 *
33 * @version $Revision: $ $Date: Aug 30, 2012$
34 * @author 2012, last modified by $Author: sherbold$
35 */
36public class CMDman implements Command {
37
38    /*
39     * (non-Javadoc)
40     *
41     * @see de.ugoe.cs.util.console.Command#run(java.util.List)
42     */
43    @Override
44    public void run(List<Object> parameters) {
45        String command;
46        if (parameters.size() > 0) {
47            command = (String) parameters.get(0);
48        }
49        else {
50            throw new IllegalArgumentException();
51        }
52
53        Command cmd = CommandExecuter.getInstance().getCMD(command);
54
55        if (cmd != null) {
56            InputStream manStream =
57                ClassLoader.getSystemResourceAsStream("manuals/" + command);
58            if (manStream != null) {
59                try {
60                    Console.println(WordUtils.wrap(IOUtils.toString(manStream, "UTF-8"), 100).replace("$USAGE$", "Usage: " + cmd.help()));
61                }
62                catch (IOException e) {
63                    Console.printerrln("Failure reading man page");
64                    Console.logException(e);
65                }
66            }
67            else {
68                Console.println("Usage: " + help());
69            }
70        }
71        else {
72            Console.println("Command " + command + " not found.");
73        }
74    }
75
76    /*
77     * (non-Javadoc)
78     *
79     * @see de.ugoe.cs.util.console.Command#help()
80     */
81    @Override
82    public String help() {
83        return "man <commandName>";
84    }
85
86}
Note: See TracBrowser for help on using the repository browser.