001package crash.commands.base;
002
003import org.crsh.cli.Command;
004import org.crsh.cli.Usage;
005import org.crsh.command.BaseCommand;
006import org.crsh.command.DescriptionFormat;
007import org.crsh.command.InvocationContext;
008import org.crsh.command.ShellCommand;
009import org.crsh.shell.impl.command.CRaSH;
010import org.crsh.text.Color;
011import org.crsh.text.Decoration;
012import org.crsh.text.Style;
013import org.crsh.text.ui.LabelElement;
014import org.crsh.text.ui.RowElement;
015import org.crsh.text.ui.TableElement;
016
017import java.io.IOException;
018
019/** @author Julien Viet */
020public class help extends BaseCommand {
021
022  @Usage("provides basic help")
023  @Command
024  public void main(InvocationContext<Object> context) throws IOException {
025
026    //
027    TableElement table = new TableElement().rightCellPadding(1);
028    table.add(
029        new RowElement().
030            add(new LabelElement("NAME").style(Style.style(Decoration.bold))).
031            add(new LabelElement("DESCRIPTION")));
032
033    //
034    CRaSH crash = (CRaSH)context.getSession().get("crash");
035    Iterable<String> names = crash.getCommandNames();
036    for (String name : names) {
037      try {
038        ShellCommand cmd = crash.getCommand(name);
039        if (cmd != null) {
040          String desc = cmd.describe(name, DescriptionFormat.DESCRIBE);
041          if (desc == null) {
042            desc = "";
043          }
044          table.add(
045              new RowElement().
046                  add(new LabelElement(name).style(Style.style(Color.red))).
047                  add(new LabelElement(desc)));
048        }
049      } catch (Exception ignore) {
050        //
051      }
052    }
053
054    //
055    context.provide(new LabelElement("Try one of these commands with the -h or --help switch:\n"));
056    context.provide(table);
057  }
058}