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));
}
}
}
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();
}
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);
}
}
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");
}
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);
}
Aggregations