*/
public class DocBookCommandHelpPrinter extends AbstractCommandHelpPrinter {
@Override
public void printHelp(Action action, PrintStream out, boolean includeHelpOption) {
Command command = action.getClass().getAnnotation(Command.class);
Set<Option> options = new HashSet<>();
List<Argument> arguments = new ArrayList<Argument>();
Map<Argument, Field> argFields = new HashMap<>();
Map<Option, Field> optFields = new HashMap<>();
for (Class<?> type = action.getClass(); type != null; type = type.getSuperclass()) {
for (Field field : type.getDeclaredFields()) {
Option option = field.getAnnotation(Option.class);
if (option != null) {
options.add(option);
}
Argument argument = field.getAnnotation(Argument.class);
if (argument != null) {
argument = replaceDefaultArgument(field, argument);
argFields.put(argument, field);
int index = argument.index();
while (arguments.size() <= index) {
arguments.add(null);
}
if (arguments.get(index) != null) {
throw new IllegalArgumentException("Duplicate argument index: " + index + " on Action " + action.getClass().getName());
}
arguments.set(index, argument);
}
}
}
if (includeHelpOption)
options.add(HelpOption.HELP);
out.println("<section>");
out.println(" <title>" + command.scope() + ":" + command.name() + "</title>");
out.println(" <section>");
out.println(" <title>Description</title>");
out.println(" <para>");
out.println(command.description());
out.println(" </para>");
out.println(" </section>");
StringBuffer syntax = new StringBuffer();
syntax.append(String.format("%s:%s", command.scope(), command.name()));
if (options.size() > 0) {
syntax.append(" [options]");
}
if (arguments.size() > 0) {
syntax.append(' ');
for (Argument argument : arguments) {
syntax.append(String.format(argument.required() ? "%s " : "[%s] ", argument.name()));
}
}
out.println(" <section>");
out.println(" <title>Syntax</title>");
out.println(" <para>");
out.println(syntax.toString());
out.println(" </para>");
out.println(" </section>");
if (arguments.size() > 0) {
out.println(" <section>");
out.println(" <title>Arguments</title>");
out.println(" <informaltable>");
for (Argument argument : arguments) {
out.println(" <tr>");
out.println(" <td>" + argument.name() + "</td>");
String description = argument.description();
if (!argument.required()) {
if (argument.valueToShowInHelp() != null && argument.valueToShowInHelp().length() != 0) {
if (Argument.DEFAULT_STRING.equals(argument.valueToShowInHelp())) {
Object o = getDefaultValue(action, argFields.get(argument));
String defaultValue = getDefaultValueString(o);
if (defaultValue != null) {
description += " (defaults to " + o.toString() + ")";
}
}
}
}
out.println(" <td>" + description + "</td>");
out.println(" </tr>");
}
out.println(" </informaltable>");
out.println(" </section>");
}
if (options.size() > 0) {
out.println(" <section>");
out.println(" <title>Options</title>");
out.println(" <informaltable>");
for (Option option : options) {
String opt = option.name();
String description = option.description();
for (String alias : option.aliases()) {
opt += ", " + alias;
}
Object o = getDefaultValue(action, optFields.get(option));
String defaultValue = getDefaultValueString(o);
if (defaultValue != null) {
description += " (defaults to " + o.toString() + ")";
}
out.println(" <tr>");
out.println(" <td>" + opt + "</td>");
out.println(" <td>" + description + "</td>");
out.println(" </tr>");
}
out.println(" </informaltable>");
out.println(" </section>");
}
if (command.detailedDescription().length() > 0) {
out.println(" <section>");
out.println(" <title>Details</title>");
out.println(" <para>");
out.println(command.detailedDescription());
out.println(" </para>");
out.println(" </section>");
}
out.println("</section>");
}