List of usage examples for org.apache.commons.cli DefaultParser DefaultParser
DefaultParser
From source file:be.redlab.maven.yamlprops.create.YamlConvertCli.java
public static void main(String... args) throws IOException { CommandLineParser parser = new DefaultParser(); Options options = new Options(); HelpFormatter formatter = new HelpFormatter(); options.addOption(Option.builder("s").argName("source").desc("the source folder or file to convert") .longOpt("source").hasArg(true).build()); options.addOption(Option.builder("t").argName("target").desc("the target file to store in") .longOpt("target").hasArg(true).build()); options.addOption(Option.builder("h").desc("print help").build()); try {//from w ww . j a v a 2 s. c o m CommandLine cmd = parser.parse(options, args); if (cmd.hasOption('h')) { formatter.printHelp("converter", options); } File source = new File(cmd.getOptionValue("s", System.getProperty("user.dir"))); String name = source.getName(); if (source.isDirectory()) { PropertiesToYamlConverter yamlConverter = new PropertiesToYamlConverter(); String[] ext = { "properties" }; Iterator<File> fileIterator = FileUtils.iterateFiles(source, ext, true); while (fileIterator.hasNext()) { File next = fileIterator.next(); System.out.println(next); String s = StringUtils.removeStart(next.getParentFile().getPath(), source.getPath()); System.out.println(s); String f = StringUtils.split(s, IOUtils.DIR_SEPARATOR)[0]; System.out.println("key = " + f); Properties p = new Properties(); try { p.load(new FileReader(next)); yamlConverter.addProperties(f, p); } catch (IOException e) { e.printStackTrace(); } } FileWriter fileWriter = new FileWriter( new File(source.getParentFile(), StringUtils.substringBeforeLast(name, ".") + ".yaml")); yamlConverter.writeYaml(fileWriter); fileWriter.close(); } else { Properties p = new Properties(); p.load(new FileReader(source)); FileWriter fileWriter = new FileWriter( new File(source.getParentFile(), StringUtils.substringBeforeLast(name, ".") + ".yaml")); new PropertiesToYamlConverter().addProperties(name, p).writeYaml(fileWriter); fileWriter.close(); } } catch (ParseException e) { e.printStackTrace(); formatter.printHelp("converter", options); } }
From source file:io.minimum.minecraft.rbclean.RedisBungeeClean.java
public static void main(String... args) { Options options = new Options(); Option hostOption = new Option("h", "host", true, "Sets the Redis host to use."); hostOption.setRequired(true);//from ww w . j av a2 s . c o m options.addOption(hostOption); Option portOption = new Option("p", "port", true, "Sets the Redis port to use."); options.addOption(portOption); Option passwordOption = new Option("w", "password", true, "Sets the Redis password to use."); options.addOption(passwordOption); Option dryRunOption = new Option("d", "dry-run", false, "Performs a dry run (no data is modified)."); options.addOption(dryRunOption); CommandLine commandLine; try { commandLine = new DefaultParser().parse(options, args); } catch (ParseException e) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("RedisBungeeClean", options); return; } int port = commandLine.hasOption('p') ? Integer.parseInt(commandLine.getOptionValue('p')) : 6379; try (Jedis jedis = new Jedis(commandLine.getOptionValue('h'), port, 0)) { if (commandLine.hasOption('w')) { jedis.auth(commandLine.getOptionValue('w')); } System.out.println("Fetching UUID cache..."); Map<String, String> uuidCache = jedis.hgetAll("uuid-cache"); Gson gson = new Gson(); // Just in case we need it, compress everything in JSON format. if (!commandLine.hasOption('d')) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss"); File file = new File("uuid-cache-previous-" + dateFormat.format(new Date()) + ".json.gz"); try { file.createNewFile(); } catch (IOException e) { System.out.println("Can't write backup of the UUID cache, will NOT proceed."); e.printStackTrace(); return; } System.out.println("Creating backup (as " + file.getName() + ")..."); try (OutputStreamWriter bw = new OutputStreamWriter( new GZIPOutputStream(new FileOutputStream(file)))) { gson.toJson(uuidCache, bw); } catch (IOException e) { System.out.println("Can't write backup of the UUID cache, will NOT proceed."); e.printStackTrace(); return; } } System.out.println("Cleaning out the bird cage (this may take a while...)"); int originalSize = uuidCache.size(); for (Iterator<Map.Entry<String, String>> it = uuidCache.entrySet().iterator(); it.hasNext();) { CachedUUIDEntry entry = gson.fromJson(it.next().getValue(), CachedUUIDEntry.class); if (entry.expired()) { it.remove(); } } int newSize = uuidCache.size(); if (commandLine.hasOption('d')) { System.out.println( (originalSize - newSize) + " records would be expunged if a dry run was not conducted."); } else { System.out.println("Expunging " + (originalSize - newSize) + " records..."); Transaction transaction = jedis.multi(); transaction.del("uuid-cache"); transaction.hmset("uuid-cache", uuidCache); transaction.exec(); System.out.println("Expunging complete."); } } }
From source file:com.amertkara.multiplerunners.Application.java
public static void main(String[] args) { if (args.length == 0) { Options options = new Options(); CommandLineParser parser = new DefaultParser(); HelpFormatter formatter = new HelpFormatter(); Option parserOpt = Option.builder("parse").desc("Parser").build(); Option analyzerOpt = Option.builder("analyze").desc("Analyzer").build(); options.addOption(analyzerOpt);/*from www. ja v a 2s.c o m*/ options.addOption(parserOpt); formatter.printHelp("sample", options); } else { SpringApplication.run(Application.class, args); } }
From source file:name.ikysil.beanpathdsl.codegen.CLI.java
/** * @param args the command line arguments */// w ww . j av a2s . co m public static void main(String[] args) { final Options options = buildOptions(); try { CommandLineParser parser = new DefaultParser(); CommandLine cmdLine = parser.parse(options, args); if (cmdLine.hasOption("h")) { usage(options); System.exit(1); } Configuration configuration = new Configuration(); if (cmdLine.hasOption("s")) { configuration.setOutputDirectory(cmdLine.getOptionValue("s")); } if (cmdLine.hasOption("c")) { configuration.setOutputCharset(Charset.forName(cmdLine.getOptionValue("c"))); } if (cmdLine.hasOption("cnp")) { configuration.setClassNamePrefix(cmdLine.getOptionValue("cnp")); } if (cmdLine.hasOption("cns")) { configuration.setClassNameSuffix(cmdLine.getOptionValue("cns")); } if (cmdLine.hasOption("pnp")) { configuration.setPackageNamePrefix(cmdLine.getOptionValue("pnp")); } if (cmdLine.hasOption("pns")) { configuration.setPackageNameSuffix(cmdLine.getOptionValue("pns")); } new CodeGen(configuration).process(); } catch (ParseException ex) { System.err.println(String.format("Can not parse arguments: %s", ex.getMessage())); usage(options); } }
From source file:com.alexoree.jenkins.Main.java
public static void main(String[] args) throws Exception { // create Options object Options options = new Options(); options.addOption("t", false, "throttle the downloads, waits 5 seconds in between each d/l"); // automatically generate the help statement HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("jenkins-sync", options); CommandLineParser parser = new DefaultParser(); CommandLine cmd = parser.parse(options, args); boolean throttle = cmd.hasOption("t"); String plugins = "https://updates.jenkins-ci.org/latest/"; List<String> ps = new ArrayList<String>(); Document doc = Jsoup.connect(plugins).get(); for (Element file : doc.select("td a")) { //System.out.println(file.attr("href")); if (file.attr("href").endsWith(".hpi") || file.attr("href").endsWith(".war")) { ps.add(file.attr("href")); }//from w w w . j a v a 2s . co m } File root = new File("."); //https://updates.jenkins-ci.org/latest/AdaptivePlugin.hpi new File("./latest").mkdirs(); //output zip file String zipFile = "jenkinsSync.zip"; // create byte buffer byte[] buffer = new byte[1024]; FileOutputStream fos = new FileOutputStream(zipFile); ZipOutputStream zos = new ZipOutputStream(fos); //download the plugins for (int i = 0; i < ps.size(); i++) { System.out.println("[" + i + "/" + ps.size() + "] downloading " + plugins + ps.get(i)); String outputFile = download(root.getAbsolutePath() + "/latest/" + ps.get(i), plugins + ps.get(i)); FileInputStream fis = new FileInputStream(outputFile); // begin writing a new ZIP entry, positions the stream to the start of the entry data zos.putNextEntry(new ZipEntry(outputFile.replace(root.getAbsolutePath(), "") .replace("updates.jenkins-ci.org/", "").replace("https:/", ""))); int length; while ((length = fis.read(buffer)) > 0) { zos.write(buffer, 0, length); } zos.closeEntry(); fis.close(); if (throttle) Thread.sleep(WAIT); new File(root.getAbsolutePath() + "/latest/" + ps.get(i)).deleteOnExit(); } //download the json metadata plugins = "https://updates.jenkins-ci.org/"; ps = new ArrayList<String>(); doc = Jsoup.connect(plugins).get(); for (Element file : doc.select("td a")) { //System.out.println(file.attr("href")); if (file.attr("href").endsWith(".json")) { ps.add(file.attr("href")); } } for (int i = 0; i < ps.size(); i++) { download(root.getAbsolutePath() + "/" + ps.get(i), plugins + ps.get(i)); FileInputStream fis = new FileInputStream(root.getAbsolutePath() + "/" + ps.get(i)); // begin writing a new ZIP entry, positions the stream to the start of the entry data zos.putNextEntry(new ZipEntry(plugins + ps.get(i))); int length; while ((length = fis.read(buffer)) > 0) { zos.write(buffer, 0, length); } zos.closeEntry(); fis.close(); new File(root.getAbsolutePath() + "/" + ps.get(i)).deleteOnExit(); if (throttle) Thread.sleep(WAIT); } // close the ZipOutputStream zos.close(); }
From source file:de.drippinger.serviceExtractor.CLIEntry.java
public static void main(String... args) { Banner.printBanner();/*w ww.ja v a 2s . c o m*/ try { CommandLineParser parser = new DefaultParser(); CommandLine cmd = parser.parse(createOptions(), args); RunParameter parameter = null; if (cmd.hasOption("h")) { printHelp(); } else if (cmd.hasOption("c")) { parameter = extractParameterFromConfigFile(cmd); } else { parameter = extractParameterFromCommandline(cmd); } if (parameter != null && parameter.hasSufficientData()) { parameter.printParameter(); // TODO Do main stuff } else { log.warn("Not sufficient parameters provided. exiting"); } } catch (ParseException | ExtractorException e) { log.error(e.toString()); } }
From source file:cz.deco.Main.java
public static void main(String[] args) { CommandLineParser parser = new DefaultParser(); DecoContextImpl context = new DecoContextImpl(); Options options = getOptions();/* w ww .j a v a 2 s . c o m*/ CommandLine cmd = null; try { cmd = parser.parse(options, args); } catch (ParseException exp) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("java -jar deco.jar", options); return; } String source = cmd.getOptionValue("s"); String target = cmd.getOptionValue("t"); String plan = cmd.getOptionValue("p"); String temp = cmd.getOptionValue("d"); context.setOutputArchive(new File(target).getAbsoluteFile().toPath()); context.setApplicationArchive(new File(source).getAbsoluteFile().toPath()); Path temporaryDir; if (cmd.hasOption("d")) { temporaryDir = new File(temp).getAbsoluteFile().toPath(); } else { temporaryDir = getTempDirectory(); } context.setTemporaryDir(temporaryDir); context.setDeploymentPlan(new File(plan).getAbsoluteFile().toPath()); new Application().doWork(context); cleanUp(temporaryDir); }
From source file:io.github.the28awg.okato.App.java
public static void main(String[] args) { Options options = new Options(); options.addOption(Option.builder().longOpt("token").hasArg().required().argName("token") .desc(" ? ? ??").build()); options.addOption(Option.builder("t").longOpt("type").hasArg().required().argName("type") .desc(" [city, street, building]").build()); options.addOption(Option.builder("q").longOpt("query").hasArg().argName("query") .desc(" ? ? ").build()); options.addOption(Option.builder("c").longOpt("city_id").hasArg().argName("city_id") .desc(" (? )").build()); options.addOption(Option.builder("s").longOpt("street_id").hasArg().argName("street_id") .desc(" ").build()); CommandLineParser parser = new DefaultParser(); HelpFormatter formatter = new HelpFormatter(); CommandLine cmd;// w w w. j a va 2s. c o m try { cmd = parser.parse(options, args); } catch (ParseException e) { System.out.println(e.getMessage()); formatter.printHelp("okato", options); System.exit(1); return; } AddressFactory.token(cmd.getOptionValue("token")); String arg_type = cmd.getOptionValue("type"); String arg_city_id = cmd.getOptionValue("city_id", ""); String arg_street_id = cmd.getOptionValue("street_id", ""); String arg_query = cmd.getOptionValue("query", ""); if (arg_type.equalsIgnoreCase("city")) { try { log(SimpleOkato.toSimpleResponse( AddressFactory.service().city(AddressFactory.token(), arg_query).execute().body())); } catch (IOException e) { e.printStackTrace(); } } else if (arg_type.equalsIgnoreCase("street")) { try { log(SimpleOkato.toSimpleResponse(AddressFactory.service() .street(AddressFactory.token(), arg_city_id, arg_query).execute().body())); } catch (IOException e) { e.printStackTrace(); } } else if (arg_type.equalsIgnoreCase("building")) { try { log(SimpleOkato.toSimpleResponse(AddressFactory.service() .building(AddressFactory.token(), arg_city_id, arg_street_id, arg_query).execute().body())); } catch (IOException e) { e.printStackTrace(); } } }
From source file:jrrombaldo.pset.PSETMain.java
public static void main(String[] args) { Options options = prepareOptions();// w w w. j av a 2 s . c om CommandLineParser parser = new DefaultParser(); try { CommandLine line = parser.parse(options, args); String domain = line.getOptionValue("d"); // print help if (line.hasOption("h")) { printHelp(options); return; } // start gui e do nothing else if (!line.hasOption("c")) { startGuiVersion(domain); return; } // start gui e do nothing else if (!line.hasOption("d")) { System.out.println("a target domain is required, none was specified!"); printHelp(options); return; } if (!line.hasOption("g") && !line.hasOption("b")) { System.out.println("No search engine selected, at least one should be present"); printHelp(options); return; } if (line.hasOption("p")) { String proxy = line.getOptionValue("p"); System.out.println(proxy); } Set<String> results = new HashSet<>(); if (line.hasOption("g")) { results.addAll(new GoogleSearch(domain).listSubdomains()); } if (line.hasOption("b")) { results.addAll(new BingSearch(domain).listSubdomains()); } List<String> sortedResult = new ArrayList<String>(results); Collections.sort(sortedResult); int q = 1; for (String subDomain : sortedResult) { if (q == 1) { System.out.println("\nResults:"); } System.out.println(q + ": " + subDomain); q++; } } catch (ParseException exp) { System.out.println(exp.getLocalizedMessage()); printHelp(options); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.hpe.caf.worker.datastore.cs.Main.java
public static void main(String[] args) throws Exception { DefaultParser defaultParser = new DefaultParser(); CommandLine cli = defaultParser.parse(createOptions(), args); createContainerSdk(cli);/* w ww . j a va 2 s . c om*/ System.out.println("Seemed to work!"); }