001    /*
002     * Copyright (C) 2012 eXo Platform SAS.
003     *
004     * This is free software; you can redistribute it and/or modify it
005     * under the terms of the GNU Lesser General Public License as
006     * published by the Free Software Foundation; either version 2.1 of
007     * the License, or (at your option) any later version.
008     *
009     * This software is distributed in the hope that it will be useful,
010     * but WITHOUT ANY WARRANTY; without even the implied warranty of
011     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012     * Lesser General Public License for more details.
013     *
014     * You should have received a copy of the GNU Lesser General Public
015     * License along with this software; if not, write to the Free
016     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018     */
019    package org.crsh.shell.impl.command.system;
020    
021    import org.crsh.cli.Command;
022    import org.crsh.cli.Usage;
023    import org.crsh.command.BaseCommand;
024    import org.crsh.command.InvocationContext;
025    import org.crsh.shell.impl.command.CRaSH;
026    import org.crsh.text.Color;
027    import org.crsh.text.Decoration;
028    import org.crsh.text.Style;
029    import org.crsh.text.ui.LabelElement;
030    import org.crsh.text.ui.TableElement;
031    
032    import java.io.IOException;
033    import java.util.Map;
034    
035    /** @author Julien Viet */
036    public class help extends BaseCommand {
037    
038      @Usage("provides basic help")
039      @Command
040      public void main(InvocationContext<Object> context) throws Exception {
041    
042        //
043        TableElement table = new TableElement().rightCellPadding(1);
044        table.row(
045          new LabelElement("NAME").style(Style.style(Decoration.bold)),
046          new LabelElement("DESCRIPTION")
047        );
048    
049        //
050        CRaSH crash = (CRaSH)context.getSession().get("crash");
051        for (Map.Entry<String, String> command : crash.getCommands()) {
052          try {
053            String desc = command.getValue();
054            if (desc == null) {
055              desc = "";
056            }
057            table.row(
058              new LabelElement(command.getKey()).style(Style.style(Color.red)),
059              new LabelElement(desc));
060          } catch (Exception ignore) {
061            //
062          }
063        }
064    
065        //
066        context.provide(new LabelElement("Try one of these commands with the -h or --help switch:\n"));
067        context.provide(table);
068      }
069    }