List of usage examples for org.apache.commons.cli CommandLine hasOption
public boolean hasOption(char opt)
From source file:de.bayern.gdi.App.java
/** * @param args the command line arguments *//* w w w .java2 s. co m*/ public static void main(String[] args) { Options options = new Options(); Option help = Option.builder("?").hasArg(false).longOpt("help").desc("Print this message and exit.") .build(); Option headless = Option.builder("h").hasArg(false).longOpt("headless").desc("Start command line tool.") .build(); Option conf = Option.builder("c").hasArg(true).longOpt("config") .desc("Directory to overwrite default configuration.").build(); Option user = Option.builder("u").hasArg(true).longOpt("user").desc("User name for protected services.") .build(); Option password = Option.builder("p").hasArg(true).longOpt("password") .desc("Password for protected services.").build(); options.addOption(help); options.addOption(headless); options.addOption(conf); options.addOption(user); options.addOption(password); CommandLineParser parser = new DefaultParser(); try { CommandLine line = parser.parse(options, args); if (line.hasOption("?")) { usage(options, 0); } if (line.hasOption("h")) { // First initialize log4j for headless execution final String pid = getProcessId("0"); System.setProperty("logfilename", "logdlc-" + pid + ".txt"); } // use configuration for gui and headless mode initConfig(line.getOptionValue("c")); if (line.hasOption("h")) { System.exit(Headless.main(line.getArgs(), line.getOptionValue("u"), line.getOptionValue("p"))); } startGUI(); } catch (ParseException pe) { System.err.println("Cannot parse input: " + pe.getMessage()); usage(options, 1); } }
From source file:io.yucca.lucene.IndexUtility.java
public static void main(String[] args) { CommandLineParser parser = new PosixParser(); Options options = new Options(); initOptions(options);//from ww w .j av a 2s.c om try { final CommandLine line = parser.parse(options, args); if (line.hasOption('h')) { usage(options); } if (line.hasOption('s')) { String value = line.getOptionValue('s'); sourceIndexDirectory = new File(value); if (sourceIndexDirectory.exists() == false) { log.error("Index source directory: {} does not exist!", sourceIndexDirectory); System.exit(1); } } else { usage(options); System.exit(1); } if (line.hasOption('d')) { String value = line.getOptionValue('d'); destIndexDirectory = new File(value); if (destIndexDirectory.exists() == true) { log.error("Index destination directory: {} already exist", destIndexDirectory); System.exit(1); } } else { usage(options); System.exit(1); } if (line.hasOption('v')) { try { String value = line.getOptionValue('v'); version = Version.parseLeniently(value); } catch (Exception e) { log.error("Unrecognized index version, exiting"); usage(options); System.exit(1); } } if (line.hasOption('r')) { String value = line.getOptionValue('r'); String[] fields = value.trim().split(" *, *"); if (fields == null || fields.length == 0) { log.error("No fields were given, exiting"); usage(options); System.exit(1); } (new FieldRemover()).removeFields(sourceIndexDirectory, destIndexDirectory, fields, version); System.exit(0); } } catch (IndexUtilityException e) { log.error("Failed to work on index:", e); System.exit(1); } catch (MissingOptionException e) { log.error("Mandatory options is missing!"); usage(options); System.exit(1); } catch (ParseException e) { log.error("Failed to parse commandline options!"); usage(options); System.exit(1); } }
From source file:edu.mit.csail.sdg.alloy4.VizGUI.java
/** * @param args//from ww w . ja va 2 s . co m */ public static void main(String[] args) { // create the command line parser CommandLineParser parser = new PosixParser(); // create the Options Options optionsArg = new Options(); Option helpOpt = new Option("?", "help", false, "print this message"); Option inputFileOpt = new Option("f", "file", true, "the name of the xml file to show"); inputFileOpt.setRequired(true); optionsArg.addOption(helpOpt); optionsArg.addOption(inputFileOpt); CommandLine line = null; try { // parse the command line arguments line = parser.parse(optionsArg, args); if (line.hasOption(helpOpt.getOpt())) { // automatically generate the help statement HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("vizgui", optionsArg); return; } } catch (ParseException exp) { System.err.println("Unexpected exception:" + exp.getMessage()); HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("vizgui", optionsArg); return; } String inputFile = null; if (line.hasOption(inputFileOpt.getOpt())) { inputFile = line.getOptionValue(inputFileOpt.getOpt()); if (inputFile == null) { System.err.println("Invalid input file"); } } new edu.mit.csail.sdg.alloy4viz.VizGUI(true, inputFile, null); }
From source file:edu.stanford.muse.Main.java
public static void main(String args[]) throws Exception { Options options = getOpt();//from w w w. j a v a 2 s . c o m CommandLineParser parser = new PosixParser(); CommandLine cmd = parser.parse(options, args); if (cmd.hasOption("help")) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("Muse batch mode", options); return; } if (cmd.hasOption("debug")) PropertyConfigurator.configure("log4j.properties.debug"); else if (cmd.hasOption("debug-address-book")) PropertyConfigurator.configure("log4j.properties.ab"); else if (cmd.hasOption("debug-groups")) PropertyConfigurator.configure("log4j.properties.groups"); String cacheDir = cmd.getOptionValue('c'); if (cacheDir == null) cacheDir = defaultCacheDir; Archive.prepareBaseDir(cacheDir); // prepare default lexicon files etc. String alternateEmailAddrs = cmd.getOptionValue('a'); if (alternateEmailAddrs == null) alternateEmailAddrs = defaultAlternateEmailAddrs; String[] files = cmd.getArgs(); for (String file : files) { if (!new File(file).canRead()) { System.err.println("Sorry, cannot read file: " + file); System.exit(2); } } Archive archive = getMessages(alternateEmailAddrs, cacheDir, files); String sessionName = "default"; // GroupAssigner groupAssigner = doGroups(addressBook, allDocs); archive.postProcess(); // set up results with default # of terms per superdoc // archive.indexer.summarizer.recomputeCards((Collection) archive.getAllDocs(), archive.getAddressBook().getOwnNamesSet(), Summarizer.DEFAULT_N_CARD_TERMS); SimpleSessions.saveArchive(cacheDir, sessionName, archive); }
From source file:com.esri.geoportal.base.metadata.MetadataCLI.java
/** //from w w w . j a v a 2 s .c o m * <h1>run the javascript Evaluators.js scripts</h1> * from command line for testing. * * <div> java com.esri.geoportal.base.metadata.MetadataCLI -md={XMLFile_fullpath} *</div> * * <p><b>Note:</b> This only produces the basic JSON elements seen in the * elastic search json document. Other steps, such as itemID are found in {@link com.esri.geoportal.lib.elastic.request.PublishMetadataRequest#prePublish(ElasticContext, AccessUtil, AppResponse, MetadataDocument)} </p> * *<p><b>Note:</b> mainly tested in JetBrains Intellij</p> * <p><b>Note:</b> mvn command line call is in contrib</p> * * @author David Valentine * */ public static void main(String[] args) { Option help = Option.builder("h").required(false).longOpt("help").desc("HELP").build(); // Option metadataJsDir = // Option.builder("js") // .required(true) // .hasArg() // .longOpt("jsdir") // .desc("Base metadata javascript directory") // // .type(File.class) // test if this is a directory // .build(); ; /* not needed. js read from classpath, metadata/js/Evaluator.js required to be on classpath. TODO: test if this works in/on a jar, if not might need to test if running in a jar, and set appropriate resource location */ Option metadataFile = Option.builder("md").required(true).hasArg().longOpt("metdatafile") .desc("Metadata File") // .type(File.class) .build(); ; Option verbose = Option.builder("v").required(false) .longOpt("verboase").build(); ; Options options = new Options(); options.addOption(help); //options.addOption(metadataJsDir); options.addOption(metadataFile); ; options.addOption(verbose); ; // create the parser CommandLineParser parser = new DefaultParser(); try { // parse the command line arguments CommandLine line = parser.parse(options, args); Boolean v = line.hasOption("v"); String mds = line.getOptionValue("md"); File md = new File(mds); if (!md.isFile()) System.err.println("Md Metadata must be a file"); testScriptEvaluator(md, v); } catch (ParseException exp) { // oops, something went wrong System.err.println("Parsing failed. Reason: " + exp.getMessage()); } catch (Exception ex) { System.err.println("Metadata Evaluation Failed. Reason: " + ex.getMessage()); } }
From source file:com.asual.lesscss.LessEngineCli.java
public static void main(String[] args) throws LessException, URISyntaxException { Options cmdOptions = new Options(); cmdOptions.addOption(LessOptions.CHARSET_OPTION, true, "Input file charset encoding. Defaults to UTF-8."); cmdOptions.addOption(LessOptions.COMPRESS_OPTION, false, "Flag that enables compressed CSS output."); cmdOptions.addOption(LessOptions.CSS_OPTION, false, "Flag that enables compilation of .css files."); cmdOptions.addOption(LessOptions.LESS_OPTION, true, "Path to a custom less.js for Rhino version."); try {/*from www . java 2 s . com*/ CommandLineParser cmdParser = new GnuParser(); CommandLine cmdLine = cmdParser.parse(cmdOptions, args); LessOptions options = new LessOptions(); if (cmdLine.hasOption(LessOptions.CHARSET_OPTION)) { options.setCharset(cmdLine.getOptionValue(LessOptions.CHARSET_OPTION)); } if (cmdLine.hasOption(LessOptions.COMPRESS_OPTION)) { options.setCompress(true); } if (cmdLine.hasOption(LessOptions.CSS_OPTION)) { options.setCss(true); } if (cmdLine.hasOption(LessOptions.LESS_OPTION)) { options.setLess(new File(cmdLine.getOptionValue(LessOptions.LESS_OPTION)).toURI().toURL()); } LessEngine engine = new LessEngine(options); if (System.in.available() != 0) { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringWriter sw = new StringWriter(); char[] buffer = new char[1024]; int n = 0; while (-1 != (n = in.read(buffer))) { sw.write(buffer, 0, n); } String src = sw.toString(); if (!src.isEmpty()) { System.out.println(engine.compile(src, null, options.isCompress())); System.exit(0); } } String[] files = cmdLine.getArgs(); if (files.length == 1) { System.out.println(engine.compile(new File(files[0]), options.isCompress())); System.exit(0); } if (files.length == 2) { engine.compile(new File(files[0]), new File(files[1]), options.isCompress()); System.exit(0); } } catch (IOException ioe) { System.err.println("Error opening input file."); } catch (ParseException pe) { System.err.println("Error parsing arguments."); } String[] paths = LessEngine.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath() .split(File.separator); HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("java -jar " + paths[paths.length - 1] + " input [output] [options]", cmdOptions); System.exit(1); }
From source file:mx.unam.fesa.isoo.msp.MSPMain.java
/** * @param args/*from w w w .j a va2 s . c o m*/ * @throws Exception */ @SuppressWarnings("static-access") public static void main(String[] args) { // // creating options // Options options = new Options(); // help option // options.addOption( OptionBuilder.withDescription("Prints this message.").withLongOpt("help").create(OPT_HELP)); // server option // options.addOption(OptionBuilder.withDescription("The server this MineSweeperPlayer will connect to.") .hasArg().withArgName("SERVER").withLongOpt("server").create(OPT_SERVER)); // port option // options.addOption(OptionBuilder.withDescription("The port this MineSweeperPlayer will connect to.").hasArg() .withType(new Integer(0)).withArgName("PORT").withLongOpt("port").create(OPT_PORT)); // parsing options // String hostname = DEFAULT_SERVER; int port = DEFAULT_PORT; try { // using GNU standard // CommandLine line = new GnuParser().parse(options, args); if (line.hasOption(OPT_HELP)) { new HelpFormatter().printHelp("msc [options]", options); return; } if (line.hasOption(OPT_PORT)) { try { port = (Integer) line.getOptionObject(OPT_PORT); } catch (Exception e) { } } if (line.hasOption(OPT_SERVER)) { hostname = line.getOptionValue(OPT_PORT); } } catch (ParseException e) { System.err.println("Could not parse command line options correctly: " + e.getMessage()); return; } // // configuring logging services // try { LogManager.getLogManager() .readConfiguration(ClassLoader.getSystemResourceAsStream(DEFAULT_LOGGING_CONFIG_FILE)); } catch (Exception e) { throw new Error("Could not load logging properties file.", e); } // // setting up Mine Sweeper client // try { new MSClient(hostname, port); } catch (Exception e) { System.err.println("Could not execute MineSweeper client: " + e.getMessage()); } }
From source file:com.uber.stream.kafka.mirrormaker.manager.ManagerStarter.java
public static void main(String[] args) throws Exception { CommandLineParser parser = new DefaultParser(); CommandLine cmd = parser.parse(ManagerConf.constructManagerOptions(), args); if (cmd.getOptions().length == 0 || cmd.hasOption("help")) { HelpFormatter f = new HelpFormatter(); f.printHelp("OptionsTip", ManagerConf.constructManagerOptions()); System.exit(0);//from w w w . j a v a 2s . c o m } final ManagerStarter managerStarter = ManagerStarter.init(cmd); Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { try { managerStarter.stop(); } catch (Exception e) { LOGGER.error("Caught error during shutdown! ", e); } } }); try { managerStarter.start(); } catch (Exception e) { LOGGER.error("Cannot start uReplicator-Manager: ", e); } }
From source file:edu.msu.cme.rdp.readseq.utils.RmDupSeqs.java
public static void main(String[] args) throws Exception { String inFile;//www. j a v a 2 s . c o m String outFile; int length = 0; boolean debug = false; boolean removeDuplicates = false; try { CommandLine line = new PosixParser().parse(options, args); if (line.hasOption("duplicates")) { removeDuplicates = true; } if (line.hasOption("min_seq_length")) { length = Integer.parseInt(line.getOptionValue("min_seq_length")); } if (line.hasOption("infile")) { inFile = line.getOptionValue("infile"); } else { throw new Exception("infile is required"); } if (line.hasOption("outfile")) { outFile = line.getOptionValue("outfile"); } else { throw new Exception("outfile is required"); } if (line.hasOption("debug")) { debug = true; } } catch (Exception e) { new HelpFormatter().printHelp(120, "RmRedundantSeqs [options]", "", options, ""); System.err.println("ERROR: " + e.getMessage()); return; } if (!removeDuplicates) { filterByLength(inFile, outFile, length); } else { filterDuplicates(inFile, outFile, length, debug); } }
From source file:eu.scape_project.cdx_creator.CDXCreator.java
/** * Main entry point./*w w w . j a va 2 s . c om*/ * * @param args * @throws java.io.IOException * @throws org.apache.commons.cli.ParseException */ public static void main(String[] args) throws IOException, ParseException { Configuration conf = new Configuration(); // Command line interface config = new CDXCreatorConfig(); CommandLineParser cmdParser = new PosixParser(); GenericOptionsParser gop = new GenericOptionsParser(conf, args); CDXCreatorOptions cdxCreatorOpts = new CDXCreatorOptions(); CommandLine cmd = cmdParser.parse(cdxCreatorOpts.options, gop.getRemainingArgs()); if ((args.length == 0) || (cmd.hasOption(cdxCreatorOpts.HELP_OPT))) { cdxCreatorOpts.exit("Help", 0); } else { cdxCreatorOpts.initOptions(cmd, config); } // configuration properties if (config.getPropertiesFilePath() != null) { pu = new PropertyUtil(config.getPropertiesFilePath(), true); } else { pu = new PropertyUtil("/eu/scape_project/cdx_creator/config.properties", false); } config.setCdxfileCsColumns(pu.getProp("cdxfile.cscolumns")); config.setCdxfileCsHeader(pu.getProp("cdxfile.csheader")); CDXCreator cdxCreator = new CDXCreator(); File input = new File(config.getInputStr()); if (input.isDirectory()) { config.setDirectoryInput(true); cdxCreator.traverseDir(input); } else { CDXCreationTask cdxCreationTask = new CDXCreationTask(config, input, input.getName()); cdxCreationTask.createIndex(); } System.exit(0); }