List of usage examples for org.apache.commons.cli HelpFormatter printHelp
public void printHelp(String cmdLineSyntax, Options options)
options
with the specified command line syntax. From source file:com.ilopez.jasperemail.JasperEmail.java
public static void main(String[] args) throws Exception { JasperEmail runJasperReports = new JasperEmail(); // Set up command line parser Options options = new Options(); Option reports = OptionBuilder.withArgName("reportlist").hasArg() .withDescription("Comma separated list of JasperReport XML input files").create("reports"); options.addOption(reports);// ww w. ja v a 2 s .c om Option emailTo = OptionBuilder.withArgName("emailaddress").hasArg() .withDescription("Email address to send generated reports to").create("emailto"); options.addOption(emailTo); Option emailFrom = OptionBuilder.withArgName("emailaddress").hasArg() .withDescription("Sender email address").create("emailfrom"); options.addOption(emailFrom); Option emailSubjectLine = OptionBuilder.withArgName("emailsubject").hasArg() .withDescription("Subject line of email").create("emailsubject"); options.addOption(emailSubjectLine); Option smtpHostOption = OptionBuilder.withArgName("smtphost").hasArg() .withDescription("Address of email server").create("smtphost"); options.addOption(smtpHostOption); Option smtpUserOption = OptionBuilder.withArgName("smtpuser").hasArg() .withDescription("Username if email server requires authentication").create("smtpuser"); options.addOption(smtpUserOption); Option smtpPassOption = OptionBuilder.withArgName("smtppass").hasArg() .withDescription("Password if email server requires authentication").create("smtppass"); options.addOption(smtpPassOption); Option smtpAuthOption = OptionBuilder.withArgName("smtpauth").hasArg() .withDescription("Set SMTP Authentication").create("smtpauth"); options.addOption(smtpAuthOption); Option smtpPortOption = OptionBuilder.withArgName("smtpport").hasArg() .withDescription("Port for the SMTP server 25/468/587/2525/2526").create("smtpport"); options.addOption(smtpPortOption); Option smtpTypeOption = OptionBuilder.withArgName("smtptype").hasArg() .withDescription("Define SMTP Type, one of: " + Arrays.asList(OptionValues.SMTPType.values())) .create("smtptype"); options.addOption(smtpTypeOption); Option outputFolder = OptionBuilder.withArgName("foldername").hasArg() .withDescription( "Folder to write generated reports to, with trailing separator (slash or backslash)") .create("folder"); options.addOption(outputFolder); Option dbJDBCClass = OptionBuilder.withArgName("jdbcclass").hasArg() .withDescription("Provide the JDBC Database Class ").create("jdbcclass"); options.addOption(dbJDBCClass); Option dbJDBCURL = OptionBuilder.withArgName("jdbcurl").hasArg() .withDescription("Provide the JDBC Database URL").create("jdbcurl"); options.addOption(dbJDBCURL); Option dbUserOption = OptionBuilder.withArgName("username").hasArg() .withDescription("Username to connect to databasewith").create("dbuser"); options.addOption(dbUserOption); Option dbPassOption = OptionBuilder.withArgName("password").hasArg().withDescription("Database password") .create("dbpass"); options.addOption(dbPassOption); Option outputTypeOption = OptionBuilder.withArgName("outputtype").hasArg() .withDescription("Output type, one of: " + Arrays.asList(OutputType.values())).create("output"); options.addOption(outputTypeOption); Option outputFilenameOption = OptionBuilder.withArgName("outputfilename").hasArg() .withDescription("Output filename (excluding filetype suffix)").create("filename"); options.addOption(outputFilenameOption); Option paramsOption = OptionBuilder.withArgName("parameters").hasArg().withDescription( "Parameters, e.g. param1=boolean:true,param2=string:ABC,param3=double:134.2,param4=integer:85") .create("params"); options.addOption(paramsOption); // Parse command line CommandLineParser parser = new GnuParser(); CommandLine commandLine = parser.parse(options, args); String reportsDefinitionFileNamesCvs = commandLine.getOptionValue("reports"); if (reportsDefinitionFileNamesCvs == null) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("java -jar JasperEmail.jar", options); System.out.println(); System.out.println("See http://github.com/ilopez/JasperEmail for further documentation"); System.out.println(); throw new IllegalArgumentException("No reports specified"); } String outputPath = commandLine.getOptionValue("folder"); List<String> reportDefinitionFileNames = Arrays.asList(reportsDefinitionFileNamesCvs.split(",")); List<String> outputFileNames = new ArrayList<String>(); String jdbcClass = commandLine.getOptionValue("jdbcclass"); String jdbcURL = commandLine.getOptionValue("jdbcurl"); String databaseUsername = commandLine.getOptionValue("dbuser"); String databasePassword = commandLine.getOptionValue("dbpass"); OutputType outputType = OutputType.PDF; String outputTypeString = commandLine.getOptionValue("output"); if (outputTypeString != null) { outputType = OutputType.valueOf(outputTypeString.toUpperCase()); } String parametersString = commandLine.getOptionValue("params"); Map parameters = runJasperReports.prepareParameters(parametersString); String outputFilenameSpecified = commandLine.getOptionValue("filename"); if (outputFilenameSpecified == null) { outputFilenameSpecified = ""; } // SMTP PORT Integer smtpport = Integer.parseInt(commandLine.getOptionValue("smtpport")); Boolean smtpauth = Boolean.parseBoolean(commandLine.getOptionValue("smtpauth")); OptionValues.SMTPType smtptype; String smtptypestring = commandLine.getOptionValue("smtpenc"); if (smtptypestring != null) { smtptype = OptionValues.SMTPType.valueOf(smtptypestring.toUpperCase()); } else { smtptype = OptionValues.SMTPType.PLAIN; } // SMTP TLS // SMTP // Iterate over reports, generating output for each for (String reportsDefinitionFileName : reportDefinitionFileNames) { String outputFilename = null; if ((reportDefinitionFileNames.size() == 1) && (!outputFilenameSpecified.equals(""))) { outputFilename = outputFilenameSpecified; } else { outputFilename = outputFilenameSpecified + reportsDefinitionFileName.replaceAll("\\..*$", ""); outputFilename = outputFilename.replaceAll("^.*\\/", ""); outputFilename = outputFilename.replaceAll("^.*\\\\", ""); } outputFilename = outputFilename.replaceAll("\\W", "").toLowerCase() + "." + outputType.toString().toLowerCase(); if (outputPath != null) { if (!outputPath.endsWith("\\") && !outputPath.endsWith("/")) { outputPath += java.io.File.separator; } outputFilename = outputPath + outputFilename; } System.out.println("Going to generate report " + outputFilename); runJasperReports.generateReport(reportsDefinitionFileName, jdbcClass, jdbcURL, databaseUsername, databasePassword, jdbcURL, parameters); outputFileNames.add(outputFilename); } String emailRecipientList = commandLine.getOptionValue("emailto"); if (emailRecipientList != null) { Set<String> emailRecipients = new HashSet<String>(Arrays.asList(emailRecipientList.split(","))); String emailSender = commandLine.getOptionValue("emailfrom"); String emailSubject = commandLine.getOptionValue("emailsubject"); if (emailSubject == null) { emailSubject = "Report attached"; } String emailHost = commandLine.getOptionValue("smtphost"); if (emailHost == null) { emailHost = "localhost"; } String emailUser = commandLine.getOptionValue("smtpuser"); String emailPass = commandLine.getOptionValue("smtppass"); System.out.println("Emailing reports to " + emailRecipients); runJasperReports.emailReport(emailHost, emailUser, emailPass, emailRecipients, emailSender, emailSubject, outputFileNames, smtpauth, smtptype, smtpport); } else { System.out.println("Email not generated (no recipients specified)"); } }
From source file:com.ingby.socbox.bischeck.Execute.java
public static void main(String[] args) { // create the command line parser CommandLineParser parser = new GnuParser(); CommandLine line = null;//from www . j av a 2 s .com // create the Options Options options = new Options(); options.addOption("u", "usage", false, "show usage."); options.addOption("d", "deamon", false, "start as a deamon"); try { // parse the command line arguments line = parser.parse(options, args); } catch (org.apache.commons.cli.ParseException e) { System.out.println("Command parse error:" + e.getMessage()); HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("Bischeck", options); Util.ShellExit(Util.FAILED); } if (line.hasOption("usage")) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("Bischeck", options); Util.ShellExit(Util.OKAY); } dumpthread = new Thread() { public void run() { try { CacheFactory.destroy(); } catch (CacheException e) { LOGGER.warn("Cache could not be destoryed", e); } } }; dumpthread.setName("dumpcache"); int retStat = Util.OKAY; do { try { if (line.hasOption("deamon")) { ConfigurationManager.init(); } else { ConfigurationManager.initonce(); } } catch (Exception e) { LOGGER.error("Creating bischeck Configuration Manager failed with: {}", e.getMessage(), e); Util.ShellExit(Util.FAILED); } retStat = Execute.getInstance().deamon(); LOGGER.debug("Method Execute returned {}", retStat); } while (retStat == RESTART); dumpthread.start(); LOGGER.info("******************* Shutdown ********************"); Util.ShellExit(retStat); }