001: /*
002: * This is free software, licensed under the Gnu Public License (GPL)
003: * get a copy from <http://www.gnu.org/licenses/gpl.html>
004: *
005: * author: Henner Zeller <H.Zeller@acm.org>
006: */
007: package henplus.commands;
008:
009: import java.util.StringTokenizer;
010: import java.util.Iterator;
011:
012: import henplus.HenPlus;
013: import henplus.SQLSession;
014: import henplus.CommandDispatcher;
015: import henplus.Command;
016: import henplus.AbstractCommand;
017: import henplus.view.util.SortedMatchIterator;
018:
019: /**
020: * document me.
021: */
022: public class HelpCommand extends AbstractCommand {
023: final static int INDENT = 42;
024:
025: /**
026: * returns the command-string this command can handle.
027: */
028: public String[] getCommandList() {
029: return new String[] { "help", "?" };
030: }
031:
032: public boolean requiresValidSession(String cmd) {
033: return false;
034: }
035:
036: /**
037: * Returns a list of strings that are possible at this stage.
038: */
039: public Iterator complete(final CommandDispatcher disp,
040: String partialCommand, final String lastWord) {
041: // if we already have one arguemnt and try to expand the next: no.
042: int argc = argumentCount(partialCommand);
043: if (argc > 2 || (argc == 2 && lastWord.length() == 0)) {
044: return null;
045: }
046:
047: final Iterator it = disp.getRegisteredCommandNames(lastWord);
048: return new SortedMatchIterator(lastWord, it) {
049: protected boolean exclude(String cmdName) {
050: Command cmd = disp.getCommandFrom(cmdName);
051: return (cmd.getLongDescription(cmdName) == null);
052: }
053: };
054: }
055:
056: /**
057: * execute the command given.
058: */
059: public int execute(SQLSession session, String cmdstr, String param) {
060: StringTokenizer st = new StringTokenizer(param);
061: if (st.countTokens() > 1)
062: return SYNTAX_ERROR;
063: param = (st.hasMoreElements()) ? (String) st.nextElement()
064: : null;
065: /*
066: * nothing given: provide generic help.
067: */
068: if (param == null) {
069: Iterator it = HenPlus.getInstance().getDispatcher()
070: .getRegisteredCommands();
071: while (it.hasNext()) {
072: Command cmd = (Command) it.next();
073: String description = cmd.getShortDescription();
074: if (description == null) // no description ..
075: continue;
076:
077: StringBuffer cmdPrint = new StringBuffer(" ");
078: String[] cmds = cmd.getCommandList();
079: String firstSynopsis = cmd.getSynopsis(cmds[0]);
080: /*
081: * either print a list of known commands or the complete
082: * synopsis, if there is only one command.
083: */
084: if (cmds.length > 1 || firstSynopsis == null) {
085: for (int i = 0; i < cmds.length; ++i) {
086: if (i != 0)
087: cmdPrint.append(" | ");
088: cmdPrint.append(cmds[i]);
089: }
090: } else {
091: cmdPrint
092: .append(firstSynopsis.length() < INDENT ? firstSynopsis
093: : cmds[0]);
094: }
095: HenPlus.msg().print(cmdPrint.toString());
096: for (int i = cmdPrint.length(); i < INDENT; ++i) {
097: HenPlus.msg().print(" ");
098: }
099: HenPlus.msg().print(": ");
100: HenPlus.msg().println(description);
101: }
102: HenPlus.msg().println(
103: "config read from ["
104: + HenPlus.getInstance()
105: .getConfigurationDirectoryInfo()
106: + "]");
107: } else {
108: CommandDispatcher disp = HenPlus.getInstance()
109: .getDispatcher();
110: String cmdString = disp.getCommandNameFrom(param);
111: Command c = disp.getCommandFrom(param);
112: if (c == null) {
113: HenPlus.msg().println(
114: "Help: unknown command '" + param + "'");
115: return EXEC_FAILED;
116: }
117: printDescription(cmdString, c);
118: }
119: return SUCCESS;
120: }
121:
122: private void printDescription(String cmdStr, Command c) {
123: String desc = c.getLongDescription(cmdStr);
124: if (desc == null) {
125: if (c.getShortDescription() != null) {
126: desc = "\t[short description]: "
127: + c.getShortDescription();
128: }
129: }
130: String synopsis = c.getSynopsis(cmdStr);
131:
132: if (synopsis != null) {
133: HenPlus.msg().attributeBold();
134: HenPlus.msg().println("SYNOPSIS");
135: HenPlus.msg().attributeReset();
136: HenPlus.msg().println("\t" + synopsis);
137: HenPlus.msg().println();
138: }
139: if (desc != null) {
140: HenPlus.msg().attributeBold();
141: HenPlus.msg().println("DESCRIPTION");
142: HenPlus.msg().attributeReset();
143: HenPlus.msg().println(desc);
144: if (c.requiresValidSession(cmdStr)) {
145: HenPlus.msg().println("\tRequires valid session.");
146: }
147: }
148: if (desc == null && synopsis == null) {
149: HenPlus.msg().println(
150: "no detailed help for '" + cmdStr + "'");
151: }
152: }
153:
154: /**
155: * return a descriptive string.
156: */
157: public String getShortDescription() {
158: return "provides help for commands";
159: }
160:
161: public String getSynopsis(String cmd) {
162: return cmd + " [command]";
163: }
164:
165: public String getLongDescription(String cmd) {
166: String dsc;
167: dsc = "\tProvides help for the given command. If invoked without a\n"
168: + "\tcommand name as parameter, a list of all available commands\n"
169: + "\tis shown.";
170: return dsc;
171: }
172: }
173:
174: /*
175: * Local variables:
176: * c-basic-offset: 4
177: * compile-command: "ant -emacs -find build.xml"
178: * End:
179: */
|