List<CommandMetadata> commands = newArrayList(group.getCommands());
Collections.sort(commands, commandComparator);
if (group.getDefaultCommand() != null) {
CommandMetadata command = group.getDefaultCommand();
if (global != null) {
synopsis.append(global.getName());
if (!hideGlobalOptions) {
synopsis.appendWords(UsageHelper.toSynopsisUsage(command.getGlobalOptions()));
}
}
synopsis.append(group.getName()).appendWords(UsageHelper.toSynopsisUsage(command.getGroupOptions()));
synopsis.newline();
}
for (CommandMetadata command : commands) {
if (global != null) {
synopsis.append(global.getName());
if (!hideGlobalOptions) {
synopsis.appendWords(UsageHelper.toSynopsisUsage(command.getGlobalOptions()));
}
}
synopsis.append(group.getName()).appendWords(UsageHelper.toSynopsisUsage(command.getGroupOptions()));
synopsis.append(command.getName()).appendWords(UsageHelper.toSynopsisUsage(command.getCommandOptions()));
synopsis.newline();
}
synopsis.newline();
//
// OPTIONS
//
List<OptionMetadata> options = newArrayList();
options.addAll(group.getOptions());
if (global != null && !hideGlobalOptions) {
options.addAll(global.getOptions());
}
if (options.size() > 0) {
if (optionComparator != null) {
Collections.sort(options, optionComparator);
}
out.append("OPTIONS").newline();
for (OptionMetadata option : options) {
// option names
UsagePrinter optionPrinter = out.newIndentedPrinter(8);
optionPrinter.append(UsageHelper.toDescription(option)).newline();
// description
UsagePrinter descriptionPrinter = optionPrinter.newIndentedPrinter(4);
descriptionPrinter.append(option.getDescription()).newline();
descriptionPrinter.newline();
}
}
//
// COMMANDS
//
if (commands.size() > 0 || group.getDefaultCommand() != null) {
out.append("COMMANDS").newline();
UsagePrinter commandPrinter = out.newIndentedPrinter(8);
if (group.getDefaultCommand() != null && group.getDefaultCommand().getDescription() != null) {
commandPrinter.append("With no arguments,")
.append(group.getDefaultCommand().getDescription())
.newline()
.newline();
}
for (CommandMetadata command : group.getCommands()) {
commandPrinter.append(command.getName()).newline();
UsagePrinter descriptionPrinter = commandPrinter.newIndentedPrinter(4);
descriptionPrinter.append(command.getDescription()).newline().newline();
for (OptionMetadata option : command.getCommandOptions()) {
if (!option.isHidden() && option.getDescription() != null) {
descriptionPrinter.append("With")
.append(longest(option.getOptions()))
.append("option,")
.append(option.getDescription())