org.ow2.petals.cli.command.Help Maven / Gradle / Ivy
The newest version!
/**
* Copyright (c) 2010-2012 EBM WebSourcing, 2012-2023 Linagora
*
* This program/library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 2.1 of the License, or (at your
* option) any later version.
*
* This program/library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program/library; If not, see http://www.gnu.org/licenses/
* for the GNU Lesser General Public License version 2.1.
*/
package org.ow2.petals.cli.command;
import java.util.Map;
import java.util.TreeMap;
import org.apache.commons.cli.CommandLine;
import org.ow2.petals.cli.api.command.AbstractCommand;
import org.ow2.petals.cli.api.command.Command;
import org.ow2.petals.cli.api.command.exception.CommandException;
import org.ow2.petals.cli.api.command.exception.CommandTooManyArgumentsException;
import com.ebmwebsourcing.easycommons.lang.StringHelper;
import jline.console.completer.Completer;
import jline.console.completer.StringsCompleter;
/**
* This command print help about other commands in the shell.
* @author Sebastien Andre - EBM WebSourcing
* @author Alexandre Lagane - Linagora
*/
public class Help extends AbstractCommand {
public Help() {
super();
this.setUsage(this.getName() + " ");
this.setDescription("Display this help message or help for a specific command");
}
/**
* {@inheritDoc}
*
*
* Dedicated processing to the command 'help': No connection is required to
* execute it.
*
*/
@Override
public boolean isConnectionRequired(CommandLine cli) {
return false;
}
@Override
public void doExecute(CommandLine cli, final String... args) throws CommandException {
if (!this.checkArguments(args, 0, 1)) {
throw new CommandTooManyArgumentsException(this, args);
}
final String message;
if (args.length == 0) {
message = this.getHelp();
} else if (this.getShell().getCommands().containsKey(args[0])) {
message = this.getHelpCommand(args[0]);
} else {
throw new CommandException(this, args[0] + ": unknown command");
}
this.getShell().getPrintStream().println(message);
}
private static String formatTitle(final String title) {
return String.format("%n%s:%n", title);
}
private static String formatInfo(final String info) {
return String.format(" %s%n", info);
}
private static String formatHelp(final String name, final String desc) {
if (name.length() <= 8)
return String.format(" %-10s %s.%n", name, desc);
return String.format(" %s%n %s.%n", name, desc);
}
private String getHelp() {
final Map descs = new TreeMap<>();
for (final Command cmd : this.getShell().getCommands().values()) {
final String name = cmd.getName();
descs.put(name, cmd.getDescription());
}
final StringBuilder buffer = new StringBuilder();
buffer.append(formatTitle("Available commands"));
for (final Map.Entry entry : descs.entrySet()) {
buffer.append(formatHelp(entry.getKey(), entry.getValue()));
}
buffer.append(formatTitle("For help on a specific command type"));
buffer.append(formatInfo("help "));
return buffer.toString();
}
private String getHelpCommand(final String name) {
final Command cmd = this.getShell().getCommands().get(name);
final String usage = cmd.getUsage();
final String description = cmd.getDescription();
final String optionDescription = cmd.getOptionsDescription();
final String format;
if (StringHelper.isNullOrEmpty(optionDescription)) {
format = String.format("%nUSAGE:%n %s %s%n%nDESCRIPTION:%n %s%n", name, usage,
description);
} else {
format = String.format(
"%nUSAGE:%n %s %s%n%nDESCRIPTION:%n %s%n%nOPTIONS DESCRIPTION:%n%s%n", name,
usage, description, optionDescription);
}
return format;
}
@Override
public Completer getDefaultCompleter() {
return new StringsCompleter(this.getShell().getCommands().keySet());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy