}
private void printUsageConf(Action action, ActionMetaData actionMeta, PrintStream out, boolean includeHelpOption) {
Map<Option, Field> optionsMap = actionMeta.getOptions();
Map<Argument, Field> argsMap = actionMeta.getArguments();
Command command = action.getClass().getAnnotation(Command.class);
List<Argument> arguments = new ArrayList<Argument>(argsMap.keySet());
Collections.sort(arguments, new Comparator<Argument>() {
public int compare(Argument o1, Argument o2) {
return Integer.valueOf(o1.index()).compareTo(Integer.valueOf(o2.index()));
}
});
Set<Option> options = new HashSet<Option>(optionsMap.keySet());
if (includeHelpOption)
options.add(HelpOption.HELP);
out.println("<section>");
out.print(" <title>");
out.print(command.scope());
out.print(":");
out.print(command.name());
out.println("</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()) {
try {
argsMap.get(argument).setAccessible(true);
Object o = argsMap.get(argument).get(action);
if (o != null && (!(o instanceof Boolean) || ((Boolean) o))
&& (!(o instanceof Number) || ((Number) o).doubleValue() != 0.0)) {
description += " (defaults to " + o.toString() + ")";
}
} catch (Exception e) {
// Ignore
}
}
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;
}
try {
optionsMap.get(option).get(action);
Object o = optionsMap.get(option).get(action);
if (o != null && (!(o instanceof Boolean) || ((Boolean) o))
&& (!(o instanceof Number) || ((Number) o).doubleValue() != 0.0)) {
description += " (defaults to " + o.toString() + ")";
}
} catch (Exception e) {
}
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() != null && command.detailedDescription().trim().length() > 0) {
out.println("<section>");
out.println(" <title>Details</title>");
String description = loadDescription(action.getClass(), command.detailedDescription());
out.println(" <para>");
}
}
out.println("</section>");