List of usage examples for org.apache.commons.cli BasicParser parse
public CommandLine parse(Options options, String[] arguments, Properties properties) throws ParseException
From source file:net.vhati.modmanager.cli.SlipstreamCLI.java
public static void main(String[] args) { BasicParser parser = new BasicParser(); Options options = new Options(); options.addOption(OptionBuilder.withLongOpt("extract-dats") .withDescription("extract FTL resources into a dir").hasArg().withArgName("DIR").create()); options.addOption(OptionBuilder.withLongOpt("global-panic") .withDescription("patch as if advanced find tags had panic='true'").create()); options.addOption(/*from ww w . jav a 2 s .c o m*/ OptionBuilder.withLongOpt("list-mods").withDescription("list available mod names").create()); options.addOption(OptionBuilder.withLongOpt("runftl") .withDescription("run the game (standalone or with 'patch')").create()); options.addOption(OptionBuilder.withLongOpt("patch") .withDescription("revert to vanilla and add named mods (if any)").create()); options.addOption( OptionBuilder.withLongOpt("validate").withDescription("check named mods for problems").create()); options.addOption("h", "help", false, "display this help and exit"); options.addOption(OptionBuilder.withLongOpt("version") .withDescription("output version information and exit").create()); CommandLine cmdline = null; try { cmdline = parser.parse(options, args, true); } catch (ParseException e) { System.err.println("Error parsing commandline: " + e.getMessage()); System.exit(1); } if (cmdline.hasOption("h")) { // Exits. HelpFormatter formatter = new HelpFormatter(); String helpHeader = "Perform actions against an FTL installation and/or a list of named mods." + formatter.getNewLine(); String helpFooter = formatter.getNewLine(); helpFooter += "Each MODFILE is a filename in the mods/ dir." + formatter.getNewLine(); helpFooter += "If a named mod is a directory, a temporary zip will be created."; formatter.printHelp("modman [OPTION] [MODFILE]...", helpHeader, options, helpFooter); System.exit(0); } if (cmdline.hasOption("version")) { // Exits. System.out.println(getVersionMessage()); System.exit(0); } DelayedDeleteHook deleteHook = new DelayedDeleteHook(); Runtime.getRuntime().addShutdownHook(deleteHook); if (cmdline.hasOption("validate")) { // Exits (0/1). log.info("Validating..."); StringBuilder resultBuf = new StringBuilder(); ReportFormatter formatter = new ReportFormatter(); boolean anyInvalid = false; for (String modFileName : cmdline.getArgs()) { File modFile = new File(modsDir, modFileName); if (modFile.isDirectory()) { log.info(String.format("Zipping dir: %s/", modFile.getName())); try { modFile = createTempMod(modFile); deleteHook.addDoomedFile(modFile); } catch (IOException e) { log.error(String.format("Error zipping \"%s/\".", modFile.getName()), e); List<ReportMessage> tmpMessages = new ArrayList<ReportMessage>(); tmpMessages.add(new ReportMessage(ReportMessage.SECTION, modFileName)); tmpMessages.add(new ReportMessage(ReportMessage.EXCEPTION, e.getMessage())); formatter.format(tmpMessages, resultBuf, 0); resultBuf.append("\n"); anyInvalid = true; continue; } } Report validateReport = ModUtilities.validateModFile(modFile); formatter.format(validateReport.messages, resultBuf, 0); resultBuf.append("\n"); if (validateReport.outcome == false) anyInvalid = true; } if (resultBuf.length() == 0) { resultBuf.append("No mods were checked."); } System.out.println(); System.out.println(resultBuf.toString()); System.exit(anyInvalid ? 1 : 0); } File configFile = new File("modman.cfg"); Properties config = getConfig(configFile); if (cmdline.hasOption("list-mods")) { // Exits. log.info("Listing mods..."); boolean allowZip = config.getProperty("allow_zip", "false").equals("true"); File[] modFiles = modsDir.listFiles(new ModAndDirFileFilter(allowZip, true)); List<String> dirList = new ArrayList<String>(); List<String> fileList = new ArrayList<String>(); for (File f : modFiles) { if (f.isDirectory()) dirList.add(f.getName() + "/"); else fileList.add(f.getName()); } Collections.sort(dirList); Collections.sort(fileList); for (String s : dirList) System.out.println(s); for (String s : fileList) System.out.println(s); System.exit(0); } File datsDir = null; if (cmdline.hasOption("extract-dats") || cmdline.hasOption("patch") || cmdline.hasOption("runftl")) { datsDir = getDatsDir(config); } if (cmdline.hasOption("extract-dats")) { // Exits (0/1). log.info("Extracting dats..."); String extractPath = cmdline.getOptionValue("extract-dats"); File extractDir = new File(extractPath); File dataDatFile = new File(datsDir, "data.dat"); File resDatFile = new File(datsDir, "resource.dat"); File[] datFiles = new File[] { dataDatFile, resDatFile }; FTLDat.AbstractPack srcP = null; FTLDat.AbstractPack dstP = null; InputStream is = null; try { if (!extractDir.exists()) extractDir.mkdirs(); dstP = new FTLDat.FolderPack(extractDir); for (File datFile : datFiles) { srcP = new FTLDat.FTLPack(datFile, "r"); List<String> innerPaths = srcP.list(); for (String innerPath : innerPaths) { if (dstP.contains(innerPath)) { log.info("While extracting resources, this file was overwritten: " + innerPath); dstP.remove(innerPath); } is = srcP.getInputStream(innerPath); dstP.add(innerPath, is); } srcP.close(); } } catch (IOException e) { log.error("Error extracting dats.", e); System.exit(1); } finally { try { if (is != null) is.close(); } catch (IOException ex) { } try { if (srcP != null) srcP.close(); } catch (IOException ex) { } try { if (dstP != null) dstP.close(); } catch (IOException ex) { } } System.exit(0); } if (cmdline.hasOption("patch")) { // Exits sometimes (1 on failure). log.info("Patching..."); List<File> modFiles = new ArrayList<File>(); for (String modFileName : cmdline.getArgs()) { File modFile = new File(modsDir, modFileName); if (modFile.isDirectory()) { log.info(String.format("Zipping dir: %s/", modFile.getName())); try { modFile = createTempMod(modFile); deleteHook.addDoomedFile(modFile); } catch (IOException e) { log.error(String.format("Error zipping \"%s/\".", modFile.getName()), e); System.exit(1); } } modFiles.add(modFile); } BackedUpDat dataDat = new BackedUpDat(); dataDat.datFile = new File(datsDir, "data.dat"); dataDat.bakFile = new File(backupDir, "data.dat.bak"); BackedUpDat resDat = new BackedUpDat(); resDat.datFile = new File(datsDir, "resource.dat"); resDat.bakFile = new File(backupDir, "resource.dat.bak"); boolean globalPanic = cmdline.hasOption("global-panic"); SilentPatchObserver patchObserver = new SilentPatchObserver(); ModPatchThread patchThread = new ModPatchThread(modFiles, dataDat, resDat, globalPanic, patchObserver); deleteHook.addWatchedThread(patchThread); patchThread.start(); while (patchThread.isAlive()) { try { patchThread.join(); } catch (InterruptedException e) { } } if (!patchObserver.hasSucceeded()) System.exit(1); } if (cmdline.hasOption("runftl")) { // Exits (0/1). log.info("Running FTL..."); File exeFile = FTLUtilities.findGameExe(datsDir); if (exeFile != null) { try { FTLUtilities.launchGame(exeFile); } catch (Exception e) { log.error("Error launching FTL.", e); System.exit(1); } } else { log.error("Could not find FTL's executable."); System.exit(1); } System.exit(0); } System.exit(0); }
From source file:org.mule.util.SystemUtils.java
private static CommandLine parseCommandLine(String args[], String opts[][]) throws DefaultMuleException { Options options = new Options(); for (int i = 0; i < opts.length; i++) { options.addOption(opts[i][0], opts[i][1].equals("true") ? true : false, opts[i][2]); }//from w ww . j a v a 2 s .c o m BasicParser parser = new BasicParser(); try { CommandLine line = parser.parse(options, args, true); if (line == null) { throw new DefaultMuleException("Unknown error parsing the Mule command line"); } return line; } catch (ParseException p) { throw new DefaultMuleException("Unable to parse the Mule command line because of: " + p.toString(), p); } }