Package net.sourceforge.javautil.ui.cli

Source Code of net.sourceforge.javautil.ui.cli.CommandLineHelpCommand

package net.sourceforge.javautil.ui.cli;

import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.Collections;
import java.util.List;

import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import net.sourceforge.javautil.ui.cli.widget.TextTable;
import net.sourceforge.javautil.ui.command.UICommandArguments;
import net.sourceforge.javautil.ui.command.UICommandContext;
import net.sourceforge.javautil.ui.command.UICommandSet;
import net.sourceforge.javautil.ui.command.UICommandArguments.PassedArgument;
import net.sourceforge.javautil.ui.model.impl.UITableModelDefault;

/**
* Default help command for CLI's.
*
* @author elponderador
* @author $Author: ponderator $
* @version $Id: CommandLineHelpCommand.java 1581 2009-12-22 18:45:09Z ponderator $
*/
public class CommandLineHelpCommand<CTX extends CommandLineContext> extends CommandLineCommand<CTX, CommandLineArgumentsStandard> {
 
  protected final UICommandSet<? extends CommandLineCommand, ? extends UICommandContext> set;

  public CommandLineHelpCommand(UICommandSet<? extends CommandLineCommand, ? extends UICommandContext> set) {
    this(set, "help", "Command help");
  }
 
  public CommandLineHelpCommand(UICommandSet<? extends CommandLineCommand, ? extends UICommandContext> set, String name, String description) {
    super(name, description);
    this.set = set;
   
    this.addArgument("command", String.class, "A command for which to get specific help");
  }

  public Object execute(CTX ctx, CommandLineArgumentsStandard arguments) {
    PrintStream out = ctx.getWriter();
   
    if (arguments != null && arguments.getArgument(0) != null) {
     
      String name = String.valueOf( arguments.getArgument(0).getValue() );
      CommandLineCommand clc = set.createCommand(name);
     
      if (clc == null) return "Unrecognized command: " + name; else { clc.showHelp(out); return null; }
     
    } else {

      return this.getCommandTable();
     
    }
  }
 
  protected UITableModelDefault getCommandTable () {
    UITableModelDefault dtm = new UITableModelDefault();
    dtm.setColumnHeaders("Command", "Description");
    dtm.setColumnWidths(25, 50);
   
    List<String> commands = set.getCommandNames();
    Collections.sort(commands);
   
    for (String command : commands) {
      CommandLineCommand clc = set.createCommand(command);
      dtm.addRow(new Object[] { command, clc.getDescription() });
    }
   
    return dtm;
  }

}
TOP

Related Classes of net.sourceforge.javautil.ui.cli.CommandLineHelpCommand

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.