Search in sources :

Example 1 with Parameters

use of com.beust.jcommander.Parameters in project smali by JesusFreke.

the class HelpFormatter method format.

@Nonnull
public String format(@Nonnull List<JCommander> commandHierarchy) {
    try {
        StringWriter stringWriter = new StringWriter();
        WrappedIndentingWriter writer = new WrappedIndentingWriter(stringWriter, width - 5, width);
        JCommander leafJc = Iterables.getLast(commandHierarchy);
        writer.write("usage:");
        writer.indent(2);
        for (JCommander jc : commandHierarchy) {
            writer.write(" ");
            writer.write(ExtendedCommands.commandName(jc));
        }
        if (includeParametersInUsage(leafJc)) {
            for (ParameterDescription param : leafJc.getParameters()) {
                if (!param.getParameter().hidden()) {
                    writer.write(" [");
                    writer.write(param.getParameter().getParameter().names()[0]);
                    writer.write("]");
                }
            }
        } else {
            if (!leafJc.getParameters().isEmpty()) {
                writer.write(" [<options>]");
            }
        }
        if (!leafJc.getCommands().isEmpty()) {
            writer.write(" [<command [<args>]]");
        }
        if (leafJc.getMainParameter() != null) {
            String[] argumentNames = ExtendedCommands.parameterArgumentNames(leafJc.getMainParameter());
            if (argumentNames.length == 0) {
                writer.write(" <args>");
            } else {
                String argumentName = argumentNames[0];
                boolean writeAngleBrackets = !argumentName.startsWith("<") && !argumentName.startsWith("[");
                writer.write(" ");
                if (writeAngleBrackets) {
                    writer.write("<");
                }
                writer.write(argumentNames[0]);
                if (writeAngleBrackets) {
                    writer.write(">");
                }
            }
        }
        writer.deindent(2);
        String commandDescription = ExtendedCommands.getCommandDescription(leafJc);
        if (commandDescription != null) {
            writer.write("\n");
            writer.write(commandDescription);
        }
        if (!leafJc.getParameters().isEmpty() || leafJc.getMainParameter() != null) {
            writer.write("\n\nOptions:");
            writer.indent(2);
            for (ParameterDescription param : getSortedParameters(leafJc)) {
                if (!param.getParameter().hidden()) {
                    writer.write("\n");
                    writer.indent(4);
                    if (!param.getNames().isEmpty()) {
                        writer.write(Joiner.on(',').join(param.getParameter().names()));
                    }
                    if (getParameterArity(param) > 0) {
                        String[] argumentNames = ExtendedCommands.parameterArgumentNames(param);
                        for (int i = 0; i < getParameterArity(param); i++) {
                            writer.write(" ");
                            if (i < argumentNames.length) {
                                writer.write("<");
                                writer.write(argumentNames[i]);
                                writer.write(">");
                            } else {
                                writer.write("<arg>");
                            }
                        }
                    }
                    if (param.getDescription() != null && !param.getDescription().isEmpty()) {
                        writer.write(" - ");
                        writer.write(param.getDescription());
                    }
                    if (param.getDefault() != null) {
                        String defaultValue = null;
                        if (param.getParameterized().getType() == Boolean.class || param.getParameterized().getType() == Boolean.TYPE) {
                            if ((Boolean) param.getDefault()) {
                                defaultValue = "True";
                            }
                        } else if (List.class.isAssignableFrom(param.getParameterized().getType())) {
                            if (!((List) param.getDefault()).isEmpty()) {
                                defaultValue = param.getDefault().toString();
                            }
                        } else {
                            defaultValue = param.getDefault().toString();
                        }
                        if (defaultValue != null) {
                            writer.write(" (default: ");
                            writer.write(defaultValue);
                            writer.write(")");
                        }
                    }
                    writer.deindent(4);
                }
            }
            if (leafJc.getMainParameter() != null) {
                String[] argumentNames = ExtendedCommands.parameterArgumentNames(leafJc.getMainParameter());
                writer.write("\n");
                writer.indent(4);
                if (argumentNames.length > 0) {
                    writer.write("<");
                    writer.write(argumentNames[0]);
                    writer.write(">");
                } else {
                    writer.write("<args>");
                }
                if (leafJc.getMainParameterDescription() != null) {
                    writer.write(" - ");
                    writer.write(leafJc.getMainParameterDescription());
                }
                writer.deindent(4);
            }
            writer.deindent(2);
        }
        if (!leafJc.getCommands().isEmpty()) {
            writer.write("\n\nCommands:");
            writer.indent(2);
            List<Entry<String, JCommander>> entryList = Lists.newArrayList(leafJc.getCommands().entrySet());
            Collections.sort(entryList, new Comparator<Entry<String, JCommander>>() {

                @Override
                public int compare(Entry<String, JCommander> o1, Entry<String, JCommander> o2) {
                    return o1.getKey().compareTo(o2.getKey());
                }
            });
            for (Entry<String, JCommander> entry : entryList) {
                String commandName = entry.getKey();
                JCommander command = entry.getValue();
                Object arg = command.getObjects().get(0);
                Parameters parametersAnno = arg.getClass().getAnnotation(Parameters.class);
                if (!parametersAnno.hidden()) {
                    writer.write("\n");
                    writer.indent(4);
                    writer.write(commandName);
                    List<String> aliases = getCommandAliases(command);
                    if (!aliases.isEmpty()) {
                        writer.write("(");
                        writer.write(Joiner.on(',').join(aliases));
                        writer.write(")");
                    }
                    String commandDesc = leafJc.getCommandDescription(commandName);
                    if (commandDesc != null) {
                        writer.write(" - ");
                        writer.write(commandDesc);
                    }
                    writer.deindent(4);
                }
            }
            writer.deindent(2);
        }
        String postfixDescription = getPostfixDescription(leafJc);
        if (!postfixDescription.isEmpty()) {
            writer.write("\n\n");
            writer.write(postfixDescription);
        }
        writer.flush();
        return stringWriter.getBuffer().toString();
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}
Also used : Parameters(com.beust.jcommander.Parameters) WrappedIndentingWriter(org.jf.util.WrappedIndentingWriter) IOException(java.io.IOException) Entry(java.util.Map.Entry) StringWriter(java.io.StringWriter) JCommander(com.beust.jcommander.JCommander) List(java.util.List) ParameterDescription(com.beust.jcommander.ParameterDescription) Nonnull(javax.annotation.Nonnull)

Aggregations

JCommander (com.beust.jcommander.JCommander)1 ParameterDescription (com.beust.jcommander.ParameterDescription)1 Parameters (com.beust.jcommander.Parameters)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 List (java.util.List)1 Entry (java.util.Map.Entry)1 Nonnull (javax.annotation.Nonnull)1 WrappedIndentingWriter (org.jf.util.WrappedIndentingWriter)1