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.RowElement;
031    import org.crsh.text.ui.TableElement;
032    
033    import java.io.IOException;
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 IOException {
041    
042        //
043        TableElement table = new TableElement().rightCellPadding(1);
044        table.add(
045            new RowElement().
046                add(new LabelElement("NAME").style(Style.style(Decoration.bold))).
047                add(new LabelElement("DESCRIPTION")));
048    
049        //
050        CRaSH crash = (CRaSH)context.getSession().get("crash");
051        Iterable<String> names = crash.getCommandNames();
052        for (String name : names) {
053          try {
054            String desc = crash.getCommandDescription(name);
055            if (desc == null) {
056              desc = "";
057            }
058            table.add(
059                new RowElement().
060                    add(new LabelElement(name).style(Style.style(Color.red))).
061                    add(new LabelElement(desc)));
062          } catch (Exception ignore) {
063            //
064          }
065        }
066    
067        //
068        context.provide(new LabelElement("Try one of these commands with the -h or --help switch:\n"));
069        context.provide(table);
070      }
071    }