KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmanage > cmdui > commands > HelpHandler


1 /**
2  * Copyright 2004-2005 jManage.org
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.jmanage.cmdui.commands;
17
18 import org.jmanage.cmdui.*;
19 import org.jmanage.cmdui.util.Out;
20 import org.jmanage.cmdui.util.Table;
21
22 import java.util.Collection JavaDoc;
23 import java.util.Iterator JavaDoc;
24
25 /**
26  *
27  * date: Feb 4, 2005
28  * @author Rakesh Kalra
29  */

30 public class HelpHandler implements CommandHandler {
31
32     /**
33      *
34      * @param context
35      * @return true if the command was handled properly; false otherwise
36      */

37     public boolean execute(HandlerContext context) {
38
39         try {
40             return execute0(context);
41         } catch (InvalidCommandException e) {
42             /* this is not possible */
43             throw new RuntimeException JavaDoc(e);
44         }
45     }
46
47     private boolean execute0(HandlerContext context)
48         throws InvalidCommandException {
49
50         String JavaDoc[] args = context.getCommand().getArgs();
51         if(args.length == 1){
52             /* print long help for given command */
53             CommandHandler handler = null;
54             try {
55                 handler = CommandHandlerFactory.getHandler(args[0]);
56                 handler.help();
57                 return true;
58             } catch (InvalidCommandException e) {
59                 Out.println(e.getMessage());
60                 Out.println();
61             }
62         }
63
64         /* print help about using jmanage command */
65         Out.println("jmanage [-username <username>] [-password <password>] " +
66                 "[-verbose[=<level>]] [command] [command args]");
67         /* print short help for all commands */
68         Out.println();
69         Out.println("Commands:");
70         Table table = new Table(2);
71         Collection JavaDoc commandNames = CommandHandlerFactory.getCommandNames();
72         for(Iterator JavaDoc it=commandNames.iterator(); it.hasNext();){
73             String JavaDoc commandName = (String JavaDoc)it.next();
74             CommandHandler handler =
75                     CommandHandlerFactory.getHandler(commandName);
76             table.add(commandName, handler.getShortHelp());
77         }
78         table.print();
79         Out.println();
80         Out.println("Type \"help <command>\" for detailed command help.");
81         return true;
82     }
83
84     public String JavaDoc getShortHelp(){
85         return "Prints jManage command line help";
86     }
87
88     public void help() {
89         Out.println(getShortHelp());
90     }
91 }
92
Popular Tags