package fmc.prototypes.javamail; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLineParser; import org.apache.commons.cli.HelpFormatter; import org.apache.commons.cli.Option; import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; import org.apache.commons.cli.PosixParser; /** * Various tools for this little app * * @author kreuz */ public class Tools { /** * Arguments help option. */ private static final Option HELP_OPTION = new Option("h", "help", false, "prints this help message"); /** Creates a new instance of Tools */ public Tools() { } /** * Parses the command line from given arguments. * * @param opts options for the parser. * @param args arguments to parse command line from. * * @return parsed command line. */ public static CommandLine parseArgs(Option[] opts, String[] args) { Options options = new Options(); options.addOption(HELP_OPTION); for (Option opt : opts) { options.addOption(opt); } CommandLine cmd = null; CommandLineParser parser = new PosixParser(); try { cmd = parser.parse(options, args); if (cmd.hasOption(HELP_OPTION.getOpt())) { new HelpFormatter().printHelp("usage help :", "\n", options, "\n", true); System.exit(0); } } catch (ParseException ex) { System.err.println("Parsing failed. Reason: " + ex.getMessage()); new HelpFormatter().printHelp("runTests.sh", options); System.exit(1); } return cmd; } }