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 */
019package org.crsh.shell.impl.command.system;
020
021import org.crsh.cli.Command;
022import org.crsh.cli.Usage;
023import org.crsh.command.BaseCommand;
024import org.crsh.command.InvocationContext;
025import org.crsh.shell.impl.command.CRaSH;
026import org.crsh.text.Color;
027import org.crsh.text.Decoration;
028import org.crsh.text.Style;
029import org.crsh.text.ui.LabelElement;
030import org.crsh.text.ui.RowElement;
031import org.crsh.text.ui.TableElement;
032
033import java.io.IOException;
034
035/** @author Julien Viet */
036public 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}