Search in sources :

Example 1 with ParameterDescription

use of com.beust.jcommander.ParameterDescription in project jslint4java by happygiraffe.

the class Main method processOptions.

private List<String> processOptions(String[] args) {
    JSLintFlags jslintFlags = new JSLintFlags();
    Flags flags = new Flags();
    JCommander jc = new JCommander(new Object[] { flags, jslintFlags });
    jc.setProgramName(FULL_PROGRAM_NAME);
    try {
        jc.parse(args);
    } catch (ParameterException e) {
        info(e.getMessage());
        usage(jc);
    }
    if (flags.version) {
        version();
    }
    if (flags.help) {
        usage(jc);
    }
    if (flags.encoding != null) {
        encoding = flags.encoding;
    }
    lint = makeLint(flags);
    setResultFormatter(flags.report);
    for (ParameterDescription pd : jc.getParameters()) {
        Parameterized p = pd.getParameterized();
        // Is it declared on JSLintFlags?
        if (!pd.getObject().getClass().isAssignableFrom(JSLintFlags.class)) {
            continue;
        }
        try {
            // Need to get Option.
            Option o = getOption(p.getName());
            // Need to get value.
            Object val = p.get(jslintFlags);
            if (val == null) {
                continue;
            }
            Class<?> type = p.getType();
            if (type.isAssignableFrom(Boolean.class)) {
                lint.addOption(o);
            } else // In theory, everything else should be a String for later parsing.
            if (type.isAssignableFrom(String.class)) {
                lint.addOption(o, (String) val);
            } else {
                die("unknown type \"" + type + "\" (for " + p.getName() + ")");
            }
        } catch (IllegalArgumentException e) {
            die(e.getMessage());
        }
    }
    if (flags.files.isEmpty()) {
        usage(jc);
        // can never happen
        return null;
    } else {
        return flags.files;
    }
}
Also used : Parameterized(com.beust.jcommander.Parameterized) JCommander(com.beust.jcommander.JCommander) ParameterException(com.beust.jcommander.ParameterException) Option(com.googlecode.jslint4java.Option) ParameterDescription(com.beust.jcommander.ParameterDescription)

Example 2 with ParameterDescription

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

the class HelpFormatter method getSortedParameters.

private List<ParameterDescription> getSortedParameters(JCommander jc) {
    List<ParameterDescription> parameters = Lists.newArrayList(jc.getParameters());
    final Pattern pattern = Pattern.compile("^-*(.*)$");
    Collections.sort(parameters, new Comparator<ParameterDescription>() {

        @Override
        public int compare(ParameterDescription o1, ParameterDescription o2) {
            String s1;
            Matcher matcher = pattern.matcher(o1.getParameter().names()[0]);
            if (matcher.matches()) {
                s1 = matcher.group(1);
            } else {
                throw new IllegalStateException();
            }
            String s2;
            matcher = pattern.matcher(o2.getParameter().names()[0]);
            if (matcher.matches()) {
                s2 = matcher.group(1);
            } else {
                throw new IllegalStateException();
            }
            return s1.compareTo(s2);
        }
    });
    return parameters;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) ParameterDescription(com.beust.jcommander.ParameterDescription)

Example 3 with ParameterDescription

use of com.beust.jcommander.ParameterDescription in project jslint4java by happygiraffe.

the class Main method usage.

private void usage(JCommander jc) {
    info(String.format("Usage: %s [options] file.js ...", FULL_PROGRAM_NAME));
    info("");
    List<ParameterDescription> parameters = jc.getParameters();
    String spec = " %-" + getLongestName(parameters) + "s %s";
    Collections.sort(parameters, new ParameterDescriptionComparator());
    for (ParameterDescription pd : parameters) {
        info(String.format(spec, pd.getLongestName(), pd.getDescription()));
    }
    info("");
    version();
}
Also used : ParameterDescription(com.beust.jcommander.ParameterDescription)

Example 4 with ParameterDescription

use of com.beust.jcommander.ParameterDescription in project jadx by skylot.

the class JadxCLIArgs method printUsage.

public void printUsage() {
    JCommander jc = new JCommander(this);
    // print usage in not sorted fields order (by default its sorted by description)
    PrintStream out = System.out;
    out.println();
    out.println("jadx - dex to java decompiler, version: " + JadxDecompiler.getVersion());
    out.println();
    out.println("usage: jadx [options] " + jc.getMainParameterDescription());
    out.println("options:");
    List<ParameterDescription> params = jc.getParameters();
    Map<String, ParameterDescription> paramsMap = new LinkedHashMap<String, ParameterDescription>(params.size());
    int maxNamesLen = 0;
    for (ParameterDescription p : params) {
        paramsMap.put(p.getParameterized().getName(), p);
        int len = p.getNames().length();
        if (len > maxNamesLen) {
            maxNamesLen = len;
        }
    }
    Field[] fields = JadxCLIArgs.class.getDeclaredFields();
    for (Field f : fields) {
        String name = f.getName();
        ParameterDescription p = paramsMap.get(name);
        if (p == null) {
            continue;
        }
        StringBuilder opt = new StringBuilder();
        opt.append(' ').append(p.getNames());
        addSpaces(opt, maxNamesLen - opt.length() + 2);
        opt.append("- ").append(p.getDescription());
        out.println(opt);
    }
    out.println("Example:");
    out.println(" jadx -d out classes.dex");
}
Also used : PrintStream(java.io.PrintStream) Field(java.lang.reflect.Field) JCommander(com.beust.jcommander.JCommander) ParameterDescription(com.beust.jcommander.ParameterDescription) LinkedHashMap(java.util.LinkedHashMap)

Example 5 with ParameterDescription

use of com.beust.jcommander.ParameterDescription 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

ParameterDescription (com.beust.jcommander.ParameterDescription)6 JCommander (com.beust.jcommander.JCommander)4 ParameterException (com.beust.jcommander.ParameterException)1 Parameterized (com.beust.jcommander.Parameterized)1 Parameters (com.beust.jcommander.Parameters)1 Option (com.googlecode.jslint4java.Option)1 IOException (java.io.IOException)1 PrintStream (java.io.PrintStream)1 StringWriter (java.io.StringWriter)1 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 Nonnull (javax.annotation.Nonnull)1 AggregateCompleter (jline.console.completer.AggregateCompleter)1 ArgumentCompleter (jline.console.completer.ArgumentCompleter)1