use of org.apache.karaf.shell.api.action.Option in project karaf by apache.
the class DefaultActionPreparator method printUsage.
public void printUsage(Action action, Map<Option, Field> options, Map<Argument, Field> arguments, PrintStream out, boolean globalScope, int termWidth) {
Command command = action.getClass().getAnnotation(Command.class);
if (command != null) {
List<Argument> argumentsSet = new ArrayList<Argument>(arguments.keySet());
argumentsSet.sort(Comparator.comparing(Argument::index));
Set<Option> optionsSet = new HashSet<Option>(options.keySet());
optionsSet.add(HelpOption.HELP);
if (command != null && (command.description() != null || command.name() != null)) {
out.println(INTENSITY_BOLD + "DESCRIPTION" + INTENSITY_NORMAL);
out.print(" ");
if (command.name() != null) {
if (globalScope) {
out.println(INTENSITY_BOLD + command.name() + INTENSITY_NORMAL);
} else {
out.println(command.scope() + ":" + INTENSITY_BOLD + command.name() + INTENSITY_NORMAL);
}
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 : argumentsSet) {
if (!argument.required()) {
syntax.append(String.format("[%s] ", argument.name()));
} else {
syntax.append(String.format("%s ", argument.name()));
}
}
}
out.println(INTENSITY_BOLD + "SYNTAX" + INTENSITY_NORMAL);
out.print(" ");
out.println(syntax.toString());
out.println();
if (arguments.size() > 0) {
out.println(INTENSITY_BOLD + "ARGUMENTS" + INTENSITY_NORMAL);
for (Argument argument : argumentsSet) {
out.print(" ");
out.println(INTENSITY_BOLD + argument.name() + INTENSITY_NORMAL);
printFormatted(" ", argument.description(), termWidth, out, true);
if (!argument.required()) {
if (argument.valueToShowInHelp() != null && argument.valueToShowInHelp().length() != 0) {
if (Argument.DEFAULT_STRING.equals(argument.valueToShowInHelp())) {
Object o = getDefaultValue(action, arguments.get(argument));
String defaultValue = getDefaultValueString(o);
if (defaultValue != null) {
printDefaultsTo(out, defaultValue);
}
} else {
printDefaultsTo(out, argument.valueToShowInHelp());
}
}
}
}
out.println();
}
if (options.size() > 0) {
out.println(INTENSITY_BOLD + "OPTIONS" + INTENSITY_NORMAL);
for (Option option : optionsSet) {
String opt = option.name();
for (String alias : option.aliases()) {
opt += ", " + alias;
}
out.print(" ");
out.println(INTENSITY_BOLD + opt + INTENSITY_NORMAL);
printFormatted(" ", option.description(), termWidth, out, true);
if (option.valueToShowInHelp() != null && option.valueToShowInHelp().length() != 0) {
if (Option.DEFAULT_STRING.equals(option.valueToShowInHelp())) {
Object o = getDefaultValue(action, options.get(option));
String defaultValue = getDefaultValueString(o);
if (defaultValue != null) {
printDefaultsTo(out, defaultValue);
}
} else {
printDefaultsTo(out, option.valueToShowInHelp());
}
}
}
out.println();
}
if (command.detailedDescription().length() > 0) {
out.println(INTENSITY_BOLD + "DETAILS" + INTENSITY_NORMAL);
String desc = loadDescription(action.getClass(), command.detailedDescription());
printFormatted(" ", desc, termWidth, out, true);
}
}
}
Aggregations