| 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 | |
|---|
| 15 | package de.ugoe.cs.util.console.defaultcommands; |
|---|
| 16 | |
|---|
| 17 | import java.io.IOException; |
|---|
| 18 | import java.io.InputStream; |
|---|
| 19 | import java.util.List; |
|---|
| 20 | |
|---|
| 21 | import org.apache.commons.io.IOUtils; |
|---|
| 22 | import org.apache.commons.lang.WordUtils; |
|---|
| 23 | |
|---|
| 24 | import de.ugoe.cs.util.console.Command; |
|---|
| 25 | import de.ugoe.cs.util.console.CommandExecuter; |
|---|
| 26 | import 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 | */ |
|---|
| 36 | public 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 | } |
|---|