List of usage examples for org.apache.commons.cli Option getValue
public String getValue()
null
if there is no value. From source file:com.cc.apptroy.baksmali.main.java
/** * Run!/*ww w. j a v a2s . com*/ */ public static void main(String[] args) throws IOException { Locale locale = new Locale("en", "US"); Locale.setDefault(locale); CommandLineParser parser = new PosixParser(); CommandLine commandLine; try { commandLine = parser.parse(options, args); } catch (ParseException ex) { usage(); return; } baksmaliOptions options = new baksmaliOptions(); boolean disassemble = true; boolean doDump = false; String dumpFileName = null; boolean setBootClassPath = false; String[] remainingArgs = commandLine.getArgs(); Option[] clOptions = commandLine.getOptions(); for (int i = 0; i < clOptions.length; i++) { Option option = clOptions[i]; String opt = option.getOpt(); switch (opt.charAt(0)) { case 'v': version(); return; case '?': while (++i < clOptions.length) { if (clOptions[i].getOpt().charAt(0) == '?') { usage(true); return; } } usage(false); return; case 'o': options.outputDirectory = commandLine.getOptionValue("o"); break; case 'p': options.noParameterRegisters = true; break; case 'l': options.useLocalsDirective = true; break; case 's': options.useSequentialLabels = true; break; case 'b': options.outputDebugInfo = false; break; case 'd': options.bootClassPathDirs.add(option.getValue()); break; case 'f': options.addCodeOffsets = true; break; case 'r': String[] values = commandLine.getOptionValues('r'); int registerInfo = 0; if (values == null || values.length == 0) { registerInfo = baksmaliOptions.ARGS | baksmaliOptions.DEST; } else { for (String value : values) { if (value.equalsIgnoreCase("ALL")) { registerInfo |= baksmaliOptions.ALL; } else if (value.equalsIgnoreCase("ALLPRE")) { registerInfo |= baksmaliOptions.ALLPRE; } else if (value.equalsIgnoreCase("ALLPOST")) { registerInfo |= baksmaliOptions.ALLPOST; } else if (value.equalsIgnoreCase("ARGS")) { registerInfo |= baksmaliOptions.ARGS; } else if (value.equalsIgnoreCase("DEST")) { registerInfo |= baksmaliOptions.DEST; } else if (value.equalsIgnoreCase("MERGE")) { registerInfo |= baksmaliOptions.MERGE; } else if (value.equalsIgnoreCase("FULLMERGE")) { registerInfo |= baksmaliOptions.FULLMERGE; } else { usage(); return; } } if ((registerInfo & baksmaliOptions.FULLMERGE) != 0) { registerInfo &= ~baksmaliOptions.MERGE; } } options.registerInfo = registerInfo; break; case 'c': String bcp = commandLine.getOptionValue("c"); if (bcp != null && bcp.charAt(0) == ':') { options.addExtraClassPath(bcp); } else { setBootClassPath = true; options.setBootClassPath(bcp); } break; case 'x': options.deodex = true; break; case 'X': options.experimental = true; break; case 'm': options.noAccessorComments = true; break; case 'a': options.apiLevel = Integer.parseInt(commandLine.getOptionValue("a")); break; case 'j': options.jobs = Integer.parseInt(commandLine.getOptionValue("j")); break; case 'i': String rif = commandLine.getOptionValue("i"); options.setResourceIdFiles(rif); break; case 't': options.useImplicitReferences = true; break; case 'e': options.dexEntry = commandLine.getOptionValue("e"); break; case 'k': options.checkPackagePrivateAccess = true; break; case 'N': disassemble = false; break; case 'D': doDump = true; dumpFileName = commandLine.getOptionValue("D"); break; case 'I': options.ignoreErrors = true; break; case 'T': options.customInlineDefinitions = new File(commandLine.getOptionValue("T")); break; default: assert false; } } if (remainingArgs.length != 1) { usage(); return; } if (options.jobs <= 0) { options.jobs = Runtime.getRuntime().availableProcessors(); if (options.jobs > 6) { options.jobs = 6; } } String inputDexFileName = remainingArgs[0]; File dexFileFile = new File(inputDexFileName); if (!dexFileFile.exists()) { System.err.println("Can't find the file " + inputDexFileName); System.exit(1); } //Read in and parse the dex file DexBackedDexFile dexFile = DexFileFactory.loadDexFile(dexFileFile, options.dexEntry, options.apiLevel, options.experimental); if (dexFile.isOdexFile()) { if (!options.deodex) { System.err.println("Warning: You are disassembling an odex file without deodexing it. You"); System.err.println("won't be able to re-assemble the results unless you deodex it with the -x"); System.err.println("option"); options.allowOdex = true; } } else { options.deodex = false; } if (!setBootClassPath && (options.deodex || options.registerInfo != 0)) { if (dexFile instanceof DexBackedOdexFile) { options.bootClassPathEntries = ((DexBackedOdexFile) dexFile).getDependencies(); } else { options.bootClassPathEntries = getDefaultBootClassPathForApi(options.apiLevel, options.experimental); } } if (options.customInlineDefinitions == null && dexFile instanceof DexBackedOdexFile) { options.inlineResolver = InlineMethodResolver .createInlineMethodResolver(((DexBackedOdexFile) dexFile).getOdexVersion()); } boolean errorOccurred = false; if (disassemble) { errorOccurred = !baksmali.disassembleDexFile(dexFile, options); } if (doDump) { if (dumpFileName == null) { dumpFileName = commandLine.getOptionValue(inputDexFileName + ".dump"); } dump.dump(dexFile, dumpFileName, options.apiLevel, options.experimental); } if (errorOccurred) { System.exit(1); } }
From source file:kellinwood.zipsigner.cmdline.Main.java
public static void main(String[] args) { try {//w w w. java 2 s . c om Options options = new Options(); CommandLine cmdLine = null; Option helpOption = new Option("h", "help", false, "Display usage information"); Option modeOption = new Option("m", "keymode", false, "Keymode one of: auto, auto-testkey, auto-none, media, platform, shared, testkey, none"); modeOption.setArgs(1); Option keyOption = new Option("k", "key", false, "PCKS#8 encoded private key file"); keyOption.setArgs(1); Option pwOption = new Option("p", "keypass", false, "Private key password"); pwOption.setArgs(1); Option certOption = new Option("c", "cert", false, "X.509 public key certificate file"); certOption.setArgs(1); Option sbtOption = new Option("t", "template", false, "Signature block template file"); sbtOption.setArgs(1); Option keystoreOption = new Option("s", "keystore", false, "Keystore file"); keystoreOption.setArgs(1); Option aliasOption = new Option("a", "alias", false, "Alias for key/cert in the keystore"); aliasOption.setArgs(1); options.addOption(helpOption); options.addOption(modeOption); options.addOption(keyOption); options.addOption(certOption); options.addOption(sbtOption); options.addOption(pwOption); options.addOption(keystoreOption); options.addOption(aliasOption); Parser parser = new BasicParser(); try { cmdLine = parser.parse(options, args); } catch (MissingOptionException x) { System.out.println("One or more required options are missing: " + x.getMessage()); usage(options); } catch (ParseException x) { System.out.println(x.getClass().getName() + ": " + x.getMessage()); usage(options); } if (cmdLine.hasOption(helpOption.getOpt())) usage(options); Properties log4jProperties = new Properties(); log4jProperties.load(new FileReader("log4j.properties")); PropertyConfigurator.configure(log4jProperties); LoggerManager.setLoggerFactory(new Log4jLoggerFactory()); List<String> argList = cmdLine.getArgList(); if (argList.size() != 2) usage(options); ZipSigner signer = new ZipSigner(); signer.addAutoKeyObserver(new Observer() { @Override public void update(Observable observable, Object o) { System.out.println("Signing with key: " + o); } }); Class bcProviderClass = Class.forName("org.bouncycastle.jce.provider.BouncyCastleProvider"); Provider bcProvider = (Provider) bcProviderClass.newInstance(); KeyStoreFileManager.setProvider(bcProvider); signer.loadProvider("org.spongycastle.jce.provider.BouncyCastleProvider"); PrivateKey privateKey = null; if (cmdLine.hasOption(keyOption.getOpt())) { if (!cmdLine.hasOption(certOption.getOpt())) { System.out.println("Certificate file is required when specifying a private key"); usage(options); } String keypw = null; if (cmdLine.hasOption(pwOption.getOpt())) keypw = pwOption.getValue(); else { keypw = new String(readPassword("Key password")); if (keypw.equals("")) keypw = null; } URL privateKeyUrl = new File(keyOption.getValue()).toURI().toURL(); privateKey = signer.readPrivateKey(privateKeyUrl, keypw); } X509Certificate cert = null; if (cmdLine.hasOption(certOption.getOpt())) { if (!cmdLine.hasOption(keyOption.getOpt())) { System.out.println("Private key file is required when specifying a certificate"); usage(options); } URL certUrl = new File(certOption.getValue()).toURI().toURL(); cert = signer.readPublicKey(certUrl); } byte[] sigBlockTemplate = null; if (cmdLine.hasOption(sbtOption.getOpt())) { URL sbtUrl = new File(sbtOption.getValue()).toURI().toURL(); sigBlockTemplate = signer.readContentAsBytes(sbtUrl); } if (cmdLine.hasOption(keyOption.getOpt())) { signer.setKeys("custom", cert, privateKey, sigBlockTemplate); signer.signZip(argList.get(0), argList.get(1)); } else if (cmdLine.hasOption(modeOption.getOpt())) { signer.setKeymode(modeOption.getValue()); signer.signZip(argList.get(0), argList.get(1)); } else if (cmdLine.hasOption((keystoreOption.getOpt()))) { String alias = null; if (!cmdLine.hasOption(aliasOption.getOpt())) { KeyStore keyStore = KeyStoreFileManager.loadKeyStore(keystoreOption.getValue(), (char[]) null); for (Enumeration<String> e = keyStore.aliases(); e.hasMoreElements();) { alias = e.nextElement(); System.out.println("Signing with key: " + alias); break; } } else alias = aliasOption.getValue(); String keypw = null; if (cmdLine.hasOption(pwOption.getOpt())) keypw = pwOption.getValue(); else { keypw = new String(readPassword("Key password")); if (keypw.equals("")) keypw = null; } CustomKeySigner.signZip(signer, keystoreOption.getValue(), null, alias, keypw.toCharArray(), "SHA1withRSA", argList.get(0), argList.get(1)); } else { signer.setKeymode("auto-testkey"); signer.signZip(argList.get(0), argList.get(1)); } } catch (Throwable t) { t.printStackTrace(); } }
From source file:edu.indiana.d2i.htrc.test.TestSuite.java
public static void testCommandLineParser(String[] args) throws IOException { GenericOptionsParser parser = new GenericOptionsParser(new Configuration(), args); CommandLine commandLine = parser.getCommandLine(); Option[] options = commandLine.getOptions(); for (Option op : options) { System.out.println(String.format("optName=%s, optValue=%s", op.getOpt(), op.getValue())); }// w w w . ja va2 s . c o m // second application argument is the output folder prefix String[] appArgs = parser.getRemainingArgs(); for (String appArg : appArgs) { System.out.println(String.format("appArg=%s", appArg)); } int iterationNum = 0; String[] arguments = LDAAnalysisDriver.generateArgs(options, new String[0], appArgs[0], appArgs[1] + "-iter-" + iterationNum); System.out.println("Passed in arguments in the first iteration"); for (String arg : arguments) { System.out.println(String.format("argument=%s", arg)); } String[] otherOps = { "-D", "user.args.lda.state.filepath=" + appArgs[1] + "-iter-" + iterationNum + File.separator + "part-r-00000" }; arguments = LDAAnalysisDriver.generateArgs(options, otherOps, appArgs[0], appArgs[1] + "-iter-" + ++iterationNum); System.out.println("Passed in arguments in the second iteration"); for (String arg : arguments) { System.out.println(String.format("argument=%s", arg)); } }
From source file:com.ning.hfind.primary.PrimaryFactory.java
public static Primary primaryFromOption(Option o) throws MalformedPatternException { String primaryString = o.getOpt(); String argument = o.getValue(); return getPrimary(primaryString, argument); }
From source file:com.teradata.benchto.driver.DriverApp.java
private static void exposeArgumentsAsPropertiesForSpring(CommandLine commandLine) { for (Option option : commandLine.getOptions()) { System.setProperty(option.getLongOpt(), option.getValue()); }// w w w.j ava 2 s. co m }
From source file:com.cloudera.beeswax.Server.java
private static int parsePort(Option opt) throws ParseException { int port = Integer.valueOf(opt.getValue()); if (port < 0 || port > USHRT_MAX) throw new ParseException("Port number must be a number in [0, " + USHRT_MAX + "]"); return port;//w ww .j a va 2 s. c om }
From source file:by.stub.cli.CommandLineInterpreter.java
/** * Identifies what command line arguments that have been passed by user are matching available options * * @return a map of passed command line arguments where key is the name of the argument *//* w ww . java2 s . com*/ public static Map<String, String> getCommandlineParams() { final Option[] options = line.getOptions(); return new HashMap<String, String>() { { for (final Option option : options) { put(option.getLongOpt(), option.getValue()); } } }; }
From source file:com.zimbra.common.util.CliUtil.java
/** * Returns the value for the given option name. * @param cl command line/*from w w w . jav a 2 s .co m*/ * @param name either short or long option name */ public static String getOptionValue(CommandLine cl, String name) { Option opt = getOption(cl, name); if (opt == null) { return null; } return opt.getValue(); }
From source file:edu.indiana.d2i.htrc.corpus.analysis.LDAAnalysisDriver.java
public static String[] generateArgs(Option[] genericOptions, String[] remainingGenericOps, String... appArgs) { List<String> args = new ArrayList<String>(); for (Option op : genericOptions) { args.add("-" + op.getOpt()); args.add(op.getValue()); }//from w ww . j a v a 2s . com for (String s : remainingGenericOps) { args.add(s); } for (String arg : appArgs) { args.add(arg); } return args.toArray(new String[0]); }
From source file:hivemall.optimizer.OptimizerOptions.java
public static void processOptions(@Nullable CommandLine cl, @Nonnull Map<String, String> options) { if (cl == null) { return;//from www .java 2s . c o m } for (Option opt : cl.getOptions()) { String optName = opt.getLongOpt(); if (optName == null) { optName = opt.getOpt(); } options.put(optName, opt.getValue()); } }