List of usage examples for org.apache.commons.cli DefaultParser DefaultParser
DefaultParser
From source file:com.github.lindenb.j4make.J4Make.java
public int instanceMain(final String args[]) { FORMAT fmt = FORMAT.DOT;/* w ww .j a v a 2 s.c o m*/ BufferedReader in = null; FileOutputStream fos = null; try { final Options options = new Options(); options.addOption(Option.builder("f").longOpt("format").argName("FORMAT") .desc("output format one of " + Arrays.toString(FORMAT.values())).hasArg().build()); options.addOption( Option.builder("h").longOpt("help").desc("Print Help and exit").hasArg(false).build()); options.addOption( Option.builder("v").longOpt("version").desc("Print version and exit").hasArg(false).build()); options.addOption( Option.builder("o").longOpt("out").desc("Output file (default stdout)").hasArg().build()); final CommandLineParser parser = new DefaultParser(); final CommandLine cmdLine = parser.parse(options, args); final List<String> arglist = cmdLine.getArgList(); if (cmdLine.hasOption("h")) { HelpFormatter format = new HelpFormatter(); format.printHelp("j4make. Pierre Lindenbaum PhD 2015 @yokofakun", options); return 0; } if (cmdLine.hasOption("v")) { System.out.println("Version: " + getVersion()); return 0; } if (cmdLine.hasOption("f")) { fmt = FORMAT.valueOf(cmdLine.getOptionValue("f").toUpperCase()); } if (arglist.isEmpty()) { LOG.debug("parsing stdin"); in = new BufferedReader(new InputStreamReader(System.in)); } else if (arglist.size() == 1) { LOG.debug("parsing " + arglist.get(0)); in = new BufferedReader(new FileReader(arglist.get(0))); } else { LOG.error("Illegal number of arguments."); return -1; } this.graph = Graph.parse(in); in.close(); LOG.debug("parsing done"); GraphWriter gw = new DotWriter(); switch (fmt) { case XML: gw = new XmlWriter(); break; case GEXF: gw = new GexfWriter(); break; case DOT: gw = new DotWriter(); break; case RDF: gw = new RdfWriter(); break; default: throw new IllegalArgumentException("" + fmt); } final OutputStream out; if (cmdLine.hasOption("o")) { final File fileout = new File(cmdLine.getOptionValue("o")); LOG.info("opening " + fileout); fos = new FileOutputStream(fileout); out = fos; } else { out = System.out; } gw.write(this.graph, out); out.flush(); if (fos != null) { fos.flush(); fos.close(); fos = null; } return 0; } catch (final Exception e) { LOG.error("FAILURE", e); return -1; } finally { if (fos != null) try { fos.close(); } catch (Exception er2) { } } }
From source file:edu.cmu.sv.modelinference.eventtool.charting.AREventChartHandler.java
@Override public ValueTrackerProducer<?, DataPointCollection, ?> process(String logFile, String logType, String[] additionalCmdArgs) throws LogProcessingException { CommandLineParser parser = new DefaultParser(); CommandLine cmd;//from w w w . j a v a 2 s .c o m try { cmd = parser.parse(cmdOpts, additionalCmdArgs, false); } catch (ParseException e) { throw new LogProcessingException(e); } ARValueTracker.FIELD trackedFieldAR = null; try { trackedFieldAR = ARValueTracker.FIELD.valueOf(cmd.getOptionValue(FIELD_OPTS_ARG).toUpperCase()); } catch (Exception e) { logger.error(e.getMessage()); logger.error( "Must be supplied a field to be tracked (e.g., pos_x) to [" + FIELD_OPTS_ARG + "] arg option"); System.exit(-1); } LogReader<AutoresolverEntry> readerAR = new SequentialLogReader<>(new AutoresolverParser()); return new ARValueTracker.ARDataPointsGenerator(trackedFieldAR, readerAR); }
From source file:channellistmaker.main.Main.java
public void start(String[] args) throws ParseException { final Option charSetOption = Option.builder("c").required(false).longOpt("charset").desc( "?????????????") .hasArg().type(String.class).build(); final Option directoryNameOption = Option.builder("d").required().longOpt("directoryname") .desc("??").hasArg().type(String.class).build(); final Option destFileNameOption = Option.builder("f").required().longOpt("destname") .desc("???").hasArg().type(String.class).build(); Options opts = new Options(); opts.addOption(charSetOption);// w w w . j a v a 2 s. com opts.addOption(directoryNameOption); opts.addOption(destFileNameOption); CommandLineParser parser = new DefaultParser(); HelpFormatter help = new HelpFormatter(); CommandLine cl; try { cl = parser.parse(opts, args); } catch (ParseException ex) { LOG.fatal("??????", ex); help.printHelp("My Java Application", opts); throw ex; } final Charset charSet; try { final String chersetstr = cl.getOptionValue(charSetOption.getOpt()); if (chersetstr == null || "".equals(chersetstr)) { charSet = Charset.defaultCharset(); LOG.info( "???????????"); } else { charSet = Charset.forName(chersetstr); } } catch (IllegalArgumentException e) { throw new IllegalArgumentException("????????", e); } LOG.info(" = " + charSet); final File dirName = new File(cl.getOptionValue(directoryNameOption.getOpt())); if (!dirName.isDirectory()) { throw new IllegalArgumentException( "???????????"); } LOG.info("?? = " + dirName.getAbsolutePath()); final File destFile = new File(cl.getOptionValue(destFileNameOption.getOpt())); LOG.info("??? = " + destFile.getAbsolutePath()); final Set<Document> docs = new EPGListMaker(dirName, charSet).seek(); final Map<MultiKey<Integer>, Channel> channels = new AllChannelDataExtractor(docs).getAllEPGRecords(); final ChannelDocumentMaker dm = new ChannelDocumentMaker(channels); try (FileOutputStream fos = new FileOutputStream(destFile); OutputStreamWriter os = new OutputStreamWriter(fos, charSet); BufferedWriter bw = new BufferedWriter(os);) { bw.write(dm.getChannelList()); bw.flush(); os.flush(); fos.flush(); LOG.info("?????"); } catch (IOException ex) { LOG.fatal("???", ex); } }
From source file:com.hp.mqm.clt.CliParserTest.java
@Test public void testArgs_invalidOption() throws ParseException { CommandLineParser parser = new DefaultParser(); try {/* w w w . ja va 2 s .c o m*/ parser.parse(options, new String[] { "-i", "-xyz" }); Assert.fail(); } catch (UnrecognizedOptionException e) { Assert.assertEquals("Unrecognized option: -xyz", e.getMessage()); } }
From source file:com.ibasco.agql.examples.base.ExampleRunner.java
private void processArguments(String[] args) throws Exception { // create the parser CommandLineParser parser = new DefaultParser(); try {//w w w. j a v a 2 s. co m // parse the command line arguments CommandLine line = parser.parse(options, args); if (line.hasOption(nameOption.getOpt())) { runExample(line.getOptionValue(nameOption.getOpt())); } else { log.error("No example specified"); } } catch (ParseException exp) { formatter.printHelp("mvn exec:java -Dexec.args=\"-e <example-key>\"", options, true); } }
From source file:edu.cmu.tetrad.cli.AbstractApplicationCli.java
protected void parseOptions() { try {//from w w w . j a v a2s . c o m CommandLineParser cmdParser = new DefaultParser(); CommandLine cmd = cmdParser.parse(MAIN_OPTIONS, args); parseCommonRequiredOptions(cmd); parseCommonOptionalOptions(cmd); parseRequiredOptions(cmd); parseOptionalOptions(cmd); } catch (Exception exception) { System.err.println(exception.getLocalizedMessage()); System.exit(-127); } }
From source file:com.teradata.tempto.runner.TemptoRunnerCommandLineParser.java
public TemptoRunnerOptions parseCommandLine(String[] argv) { Options options = buildOptions();//from w w w .jav a 2s. c o m CommandLineParser parser = new DefaultParser(); try { CommandLine commandLine = parser.parse(options, argv); return commandLineToOptions(commandLine); } catch (ParseException e) { throw new ParsingException(e.getMessage(), e); } }
From source file:gobblin.cli.JobCommand.java
@Override public void execute(Cli.GlobalOptions globalOptions, String[] otherArgs) { this.options = createCommandLineOptions(); DefaultParser parser = new DefaultParser(); AdminClient adminClient = null;/* w ww. j a v a2 s .com*/ try { CommandLine parsedOpts = parser.parse(options, otherArgs); int resultLimit = parseResultsLimit(parsedOpts); adminClient = new AdminClient(globalOptions.getAdminServerHost(), globalOptions.getAdminServerPort()); try { getAction(parsedOpts).execute(parsedOpts, adminClient, resultLimit); } catch (CommandException e) { printHelpAndExit(e.getMessage()); } } catch (ParseException e) { printHelpAndExit("Failed to parse jobs arguments: " + e.getMessage()); } finally { if (adminClient != null) adminClient.close(); } }
From source file:com.netflix.subtitles.cli.TtmlConverterCmdLineParser.java
/** * Parses command line./*from w w w.j a va 2s.c o m*/ * * @param args cli arguments * @return parsed parameters object * @throws ParseException */ public TtmlConverterCmdLineParams parse(String[] args) throws ParseException { TtmlConverterCmdLineParams params = new TtmlConverterCmdLineParams(); CommandLineParser parser = new DefaultParser(); CommandLine line; try { line = parser.parse(options, args); } catch (org.apache.commons.cli.ParseException e) { throw new ParseException(e); } // help if (line.hasOption(help.getOpt())) { // automatically generate the help statement HelpFormatter formatter = new HelpFormatter(); formatter.setWidth(120); formatter.printHelp("ttml2stl", options); return null; } // outputFile if (line.hasOption(out.getOpt())) { params.setOutputFile(line.getOptionValue(out.getOpt())); } else { throw new ParseException("Output file option must be provided."); } // frameRate if (line.hasOption(frameRate.getOpt())) { params.setFrameRate(parseFrameRate(line.getOptionValue(frameRate.getOpt()))); } // ttml params.getTtmlOptions().addAll(Stream.of(line.getOptions()).filter((o) -> o.equals(ttml)).map((o) -> { TtmlOption ttmlOption = new TtmlOption(); try { ttmlOption.setFileName(o.getValue(0)); ttmlOption.setOffsetMS(parseTtmlParameter(o, 1, "offsetMS")); ttmlOption.setStartMS(parseTtmlParameter(o, 2, "startMS")); ttmlOption.setEndMS(parseTtmlParameter(o, 3, "endMS")); } catch (IndexOutOfBoundsException e) { //It is error only if don't have file name //For required file it may not be thrown. We will check it later. } if (ttmlOption.getFileName() == null) { throw new ParseException("--ttml parameter must have at least <file> attribute defined."); } return ttmlOption; }).collect(Collectors.toCollection(ArrayList::new))); if (params.getTtmlOptions().isEmpty()) { throw new ParseException("At least one input TTML file must be provided."); } return params; }
From source file:descriptordump.Main.java
public void start(String args[]) throws DecoderException, ParseException { File inputFile = null;/*ww w. j av a 2 s . c om*/ Charset cs = null; final Option charSetOption = Option.builder("c").required(false).longOpt("charset").desc( "?????????????") .hasArg().type(String.class).build(); final Option fileNameOption = Option.builder("f").required().longOpt("filename") .desc("??").hasArg().type(String.class).build(); Options opts = new Options(); opts.addOption(fileNameOption); CommandLineParser parser = new DefaultParser(); CommandLine cl; HelpFormatter help = new HelpFormatter(); try { cl = parser.parse(opts, args); try { cs = Charset.forName(cl.getOptionValue(charSetOption.getOpt())); } catch (Exception e) { LOG.error( "?????????", e); cs = Charset.defaultCharset(); } inputFile = new File(cl.getOptionValue(fileNameOption.getOpt())); if (!inputFile.isFile()) { throw new ParseException("?????????"); } } catch (ParseException e) { // print usage. help.printHelp("My Java Application", opts); } final Set<TABLE_ID> tids = new HashSet<>(); tids.add(TABLE_ID.SDT_THIS_STREAM); tids.add(TABLE_ID.SDT_OTHER_STREAM); tids.add(TABLE_ID.NIT_THIS_NETWORK); tids.add(TABLE_ID.NIT_OTHER_NETWORK); tids.add(TABLE_ID.EIT_THIS_STREAM_8_DAYS); tids.add(TABLE_ID.EIT_THIS_STREAM_NOW_AND_NEXT); tids.add(TABLE_ID.EIT_OTHER_STREAM_8_DAYS); tids.add(TABLE_ID.EIT_OTHER_STREAM_NOW_AND_NEXT); LOG.info("Starting application..."); LOG.info("ts filename : " + inputFile.getAbsolutePath()); LOG.info("charset : " + cs); Set<Section> sections = new HashSet<>(); try { FileInputStream input = new FileInputStream(inputFile); InputStreamReader stream = new InputStreamReader(input, cs); BufferedReader buffer = new BufferedReader(stream); String line; long lines = 0; while ((line = buffer.readLine()) != null) { byte[] b = Hex.decodeHex(line.toCharArray()); Section s = new Section(b); if (s.checkCRC() == CRC_STATUS.NO_CRC_ERROR && tids.contains(s.getTable_id_const())) { sections.add(s); LOG.trace("1????"); } else { LOG.error("?????? = " + s); } lines++; } LOG.info( "?? = " + lines + " ? = " + sections.size()); input.close(); stream.close(); buffer.close(); } catch (FileNotFoundException ex) { LOG.fatal("???????", ex); } catch (IOException ex) { LOG.fatal("???????", ex); } SdtDescGetter sde = new SdtDescGetter(); NitDescGetter nide = new NitDescGetter(); EitDescGetter eide = new EitDescGetter(); for (Section s : sections) { sde.process(s); nide.process(s); eide.process(s); } Set<Descriptor> descs = new HashSet<>(); descs.addAll(sde.getUnmodifiableDest()); descs.addAll(nide.getUnmodifiableDest()); descs.addAll(eide.getUnmodifiableDest()); Set<Integer> dtags = new TreeSet<>(); for (Descriptor d : descs) { dtags.add(d.getDescriptor_tag()); } for (Integer i : dtags) { LOG.info(Integer.toHexString(i)); } }