Search in sources :

Example 46 with JCommander

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

the class HelpCommand method run.

public void run() {
    JCommander parentJc = commandAncestors.get(commandAncestors.size() - 1);
    if (commands == null || commands.isEmpty()) {
        System.out.println(new HelpFormatter().width(ConsoleUtil.getConsoleWidth()).format(commandAncestors));
    } else {
        boolean printedHelp = false;
        for (String cmd : commands) {
            JCommander command = ExtendedCommands.getSubcommand(parentJc, cmd);
            if (command == null) {
                System.err.println("No such command: " + cmd);
            } else {
                printedHelp = true;
                System.out.println(new HelpFormatter().width(ConsoleUtil.getConsoleWidth()).format(((Command) command.getObjects().get(0)).getCommandHierarchy()));
            }
        }
        if (!printedHelp) {
            System.out.println(new HelpFormatter().width(ConsoleUtil.getConsoleWidth()).format(commandAncestors));
        }
    }
}
Also used : JCommander(com.beust.jcommander.JCommander)

Example 47 with JCommander

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

the class Main method main.

public static void main(String[] args) {
    Main main = new Main();
    JCommander jc = new JCommander(main);
    main.jc = jc;
    jc.setProgramName("smali");
    List<JCommander> commandHierarchy = main.getCommandHierarchy();
    ExtendedCommands.addExtendedCommand(jc, new AssembleCommand(commandHierarchy));
    ExtendedCommands.addExtendedCommand(jc, new HelpCommand(commandHierarchy));
    ExtendedCommands.addExtendedCommand(jc, new HlepCommand(commandHierarchy));
    jc.parse(args);
    if (main.version) {
        version();
    }
    if (jc.getParsedCommand() == null || main.help) {
        main.usage();
        return;
    }
    Command command = (Command) jc.getCommands().get(jc.getParsedCommand()).getObjects().get(0);
    command.run();
}
Also used : HlepCommand(org.jf.smali.HelpCommand.HlepCommand) Command(org.jf.util.jcommander.Command) HlepCommand(org.jf.smali.HelpCommand.HlepCommand) JCommander(com.beust.jcommander.JCommander)

Example 48 with JCommander

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

Example 49 with JCommander

use of com.beust.jcommander.JCommander 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 50 with JCommander

use of com.beust.jcommander.JCommander in project pulsar by yahoo.

the class PulsarClusterMetadataSetup method main.

public static void main(String[] args) throws Exception {
    Arguments arguments = new Arguments();
    JCommander jcommander = new JCommander();
    try {
        jcommander.addObject(arguments);
        jcommander.parse(args);
        if (arguments.help) {
            jcommander.usage();
            return;
        }
    } catch (Exception e) {
        jcommander.usage();
        return;
    }
    log.info("Setting up cluster {} with zk={} global-zk={}", arguments.cluster, arguments.zookeeper, arguments.globalZookeeper);
    // Format BookKeeper metadata
    ClientConfiguration bkConf = new ClientConfiguration();
    bkConf.setLedgerManagerFactoryClass(HierarchicalLedgerManagerFactory.class);
    bkConf.setZkServers(arguments.zookeeper);
    if (!BookKeeperAdmin.format(bkConf, false, /* interactive */
    false)) {
        throw new IOException("Failed to initialize BookKeeper metadata");
    }
    ZooKeeperClientFactory zkfactory = new ZookeeperClientFactoryImpl();
    ZooKeeper localZk = zkfactory.create(arguments.zookeeper, SessionType.ReadWrite, 30000).get();
    ZooKeeper globalZk = zkfactory.create(arguments.globalZookeeper, SessionType.ReadWrite, 30000).get();
    localZk.create("/managed-ledgers", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    localZk.create("/namespace", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    try {
        ZkUtils.createFullPathOptimistic(globalZk, "/admin/policies", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    } catch (NodeExistsException e) {
    // Ignore
    }
    try {
        ZkUtils.createFullPathOptimistic(globalZk, "/admin/clusters", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    } catch (NodeExistsException e) {
    // Ignore
    }
    ClusterData clusterData = new ClusterData(arguments.clusterWebServiceUrl, arguments.clusterWebServiceUrlTls, arguments.clusterBrokerServiceUrl, arguments.clusterBrokerServiceUrlTls);
    byte[] clusterDataJson = ObjectMapperFactory.getThreadLocal().writeValueAsBytes(clusterData);
    globalZk.create("/admin/clusters/" + arguments.cluster, clusterDataJson, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    // Create marker for "global" cluster
    ClusterData globalClusterData = new ClusterData(null, null);
    byte[] globalClusterDataJson = ObjectMapperFactory.getThreadLocal().writeValueAsBytes(globalClusterData);
    try {
        globalZk.create("/admin/clusters/global", globalClusterDataJson, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    } catch (NodeExistsException e) {
    // Ignore
    }
    log.info("Cluster metadata for '{}' setup correctly", arguments.cluster);
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) ClusterData(com.yahoo.pulsar.common.policies.data.ClusterData) ZookeeperClientFactoryImpl(com.yahoo.pulsar.zookeeper.ZookeeperClientFactoryImpl) JCommander(com.beust.jcommander.JCommander) NodeExistsException(org.apache.zookeeper.KeeperException.NodeExistsException) IOException(java.io.IOException) IOException(java.io.IOException) NodeExistsException(org.apache.zookeeper.KeeperException.NodeExistsException) ClientConfiguration(org.apache.bookkeeper.conf.ClientConfiguration) ZooKeeperClientFactory(com.yahoo.pulsar.zookeeper.ZooKeeperClientFactory)

Aggregations

JCommander (com.beust.jcommander.JCommander)68 ParameterException (com.beust.jcommander.ParameterException)15 IOException (java.io.IOException)10 Map (java.util.Map)6 ParameterDescription (com.beust.jcommander.ParameterDescription)4 File (java.io.File)3 FileInputStream (java.io.FileInputStream)3 Console (com.beust.jcommander.internal.Console)2 DefaultCommand (com.evolveum.midpoint.cli.common.DefaultCommand)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Charsets (com.google.common.base.Charsets)2 Throwables (com.google.common.base.Throwables)2 HostAndPort (com.google.common.net.HostAndPort)2 Service (com.google.common.util.concurrent.Service)2 CreationException (com.google.inject.CreationException)2 SystemException (com.torodb.core.exceptions.SystemException)2 BackendPasswordConfig (com.torodb.packaging.config.model.backend.BackendPasswordConfig)2 AbstractDerby (com.torodb.packaging.config.model.backend.derby.AbstractDerby)2 AbstractPostgres (com.torodb.packaging.config.model.backend.postgres.AbstractPostgres)2 AbstractReplication (com.torodb.packaging.config.model.protocol.mongo.AbstractReplication)2