}
protected void printUsage(CommandSession session, Action action, Map<Option,Field> optionsMap, Map<Argument,Field> argsMap, PrintStream out)
{
Command command = action.getClass().getAnnotation(Command.class);
Terminal term = session != null ? (Terminal) session.get(".jline.terminal") : null;
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());
options.add(HELP);
boolean globalScope = NameScoping.isGlobalScope(session, command.scope());
if (command != null && (command.description() != null || command.name() != null))
{
out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a("DESCRIPTION").a(Ansi.Attribute.RESET));
out.print(" ");
if (command.name() != null) {
if (globalScope) {
out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a(command.name()).a(Ansi.Attribute.RESET));
} else {
out.println(Ansi.ansi().a(command.scope()).a(":").a(Ansi.Attribute.INTENSITY_BOLD).a(command.name()).a(Ansi.Attribute.RESET));
}
out.println();
}
out.print("\t");
out.println(command.description());
out.println();
}
StringBuffer syntax = new StringBuffer();
if (command != null)
{
if (globalScope) {
syntax.append(command.name());
} else {
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)
{
if (!argument.required())
{
syntax.append(String.format("[%s] ", argument.name()));
}
else
{
syntax.append(String.format("%s ", argument.name()));
}
}
}
out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a("SYNTAX").a(Ansi.Attribute.RESET));
out.print(" ");
out.println(syntax.toString());
out.println();
if (arguments.size() > 0)
{
out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a("ARGUMENTS").a(Ansi.Attribute.RESET));
for (Argument argument : arguments)
{
out.print(" ");
out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a(argument.name()).a(Ansi.Attribute.RESET));
printFormatted(" ", argument.description(), term != null ? term.getTerminalWidth() : 80, out);
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)) {
out.print(" (defaults to ");
out.print(o.toString());
out.println(")");
}
} catch (Throwable t) {
// Ignore
}
}
}
out.println();
}
if (options.size() > 0)
{
out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a("OPTIONS").a(Ansi.Attribute.RESET));
for (Option option : options)
{
String opt = option.name();
for (String alias : option.aliases())
{
opt += ", " + alias;
}
out.print(" ");
out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a(opt).a(Ansi.Attribute.RESET));
printFormatted(" ", option.description(), term != null ? term.getTerminalWidth() : 80, out);
try {
optionsMap.get(option).setAccessible(true);
Object o = optionsMap.get(option).get(action);
if (o != null
&& (!(o instanceof Boolean) || ((Boolean) o))
&& (!(o instanceof Number) || ((Number) o).doubleValue() != 0.0)) {
out.print(" (defaults to ");
out.print(o.toString());
out.println(")");
}
} catch (Throwable t) {
// Ignore
}
}
out.println();
}
if (command.detailedDescription().length() > 0) {
out.println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a("DETAILS").a(Ansi.Attribute.RESET));
String desc = loadDescription(action.getClass(), command.detailedDescription());
printFormatted(" ", desc, term != null ? term.getTerminalWidth() : 80, out);
}
}