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

Last change on this file since 1341 was 1341, checked in by pharms, 10 years ago
  • corrected indentation of manuals
  • Property svn:mime-type set to text/plain
File size: 3.1 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.BufferedReader;
18import java.io.IOException;
19import java.io.InputStream;
20import java.io.InputStreamReader;
21import java.util.List;
22
23import org.apache.commons.lang.WordUtils;
24
25import de.ugoe.cs.util.console.Command;
26import de.ugoe.cs.util.console.CommandExecuter;
27import de.ugoe.cs.util.console.Console;
28
29/**
30 * <p>
31 * Prints the manual of a command to the Console.
32 * </p>
33 *
34 * @version $Revision: $ $Date: Aug 30, 2012$
35 * @author 2012, last modified by $Author: sherbold$
36 */
37public class CMDman implements Command {
38
39    /*
40     * (non-Javadoc)
41     *
42     * @see de.ugoe.cs.util.console.Command#run(java.util.List)
43     */
44    @Override
45    public void run(List<Object> parameters) {
46        String command;
47        if (parameters.size() > 0) {
48            command = (String) parameters.get(0);
49        }
50        else {
51            throw new IllegalArgumentException();
52        }
53
54        Command cmd = CommandExecuter.getInstance().getCMD(command);
55
56        if (cmd != null) {
57            InputStream manStream =
58                ClassLoader.getSystemResourceAsStream("manuals/" + command);
59           
60            if (manStream != null) {
61                BufferedReader reader = new BufferedReader(new InputStreamReader(manStream));
62                try {
63                    String line = reader.readLine();
64                   
65                    while (line != null) {
66                        line = line.replace("$USAGE$", "Usage: " + cmd.help());
67                        Console.println(WordUtils.wrap(line, 100));
68                        line = reader.readLine();
69                    }
70                }
71                catch (IOException e) {
72                    Console.printerrln("Failure reading man page");
73                    Console.logException(e);
74                }
75                finally {
76                    try {
77                        reader.close();
78                    }
79                    catch (IOException e) {
80                        // Ignore, as this is unimportant to the user
81                    }
82                }
83            }
84            else {
85                Console.println("No man page for command " + command + " available");
86            }
87        }
88        else {
89            Console.println("Command " + command + " not found.");
90        }
91    }
92
93    /*
94     * (non-Javadoc)
95     *
96     * @see de.ugoe.cs.util.console.Command#help()
97     */
98    @Override
99    public String help() {
100        return "man <commandName>";
101    }
102
103}
Note: See TracBrowser for help on using the repository browser.