Search in sources :

Example 61 with PosixParser

use of org.apache.commons.cli.PosixParser in project tomee by apache.

the class MainImpl method main.

public void main(String[] args) {
    args = processSystemProperties(args);
    finder = new ResourceFinder(BASE_PATH);
    locale = Locale.getDefault().getLanguage();
    descriptionI18n = descriptionBase + "." + locale;
    final CommandLineParser parser = new PosixParser();
    // create the Options
    final Options options = new Options();
    options.addOption(null, "version", false, "Display version");
    options.addOption("h", "help", false, "Display help");
    options.addOption("e", "errors", false, "Produce execution error messages");
    CommandLine line = null;
    String commandName = null;
    try {
        // parse the arguments up until the first
        // command, then let the rest fall into
        // the arguments array.
        line = parser.parse(options, args, true);
        // Get and remove the commandName (first arg)
        final List<String> list = line.getArgList();
        if (list.size() > 0) {
            commandName = list.get(0);
            list.remove(0);
        }
        // The rest of the args will be passed to the command
        args = line.getArgs();
    } catch (final ParseException exp) {
        exp.printStackTrace();
        System.exit(-1);
    }
    if (line.hasOption("version")) {
        OpenEjbVersion.get().print(System.out);
        System.exit(0);
    } else if (line.hasOption("help") || commandName == null || commandName.equals("help")) {
        help();
        System.exit(0);
    }
    Properties props = null;
    try {
        props = finder.findProperties(commandName);
    } catch (final IOException e1) {
        System.out.println("Unavailable command: " + commandName);
        help(false);
        System.exit(1);
    }
    if (props == null) {
        System.out.println("Unavailable command: " + commandName);
        help(false);
        System.exit(1);
    }
    // Shift the command name itself off the args list
    final String mainClass = props.getProperty(MAIN_CLASS_PROPERTY_NAME);
    if (mainClass == null) {
        throw new NullPointerException("Command " + commandName + " did not specify a " + MAIN_CLASS_PROPERTY_NAME + " property");
    }
    Class<?> clazz = null;
    try {
        clazz = Thread.currentThread().getContextClassLoader().loadClass(mainClass);
    } catch (final ClassNotFoundException cnfe) {
        throw new IllegalStateException("Main class of command " + commandName + " does not exist: " + mainClass, cnfe);
    }
    Method mainMethod = null;
    try {
        mainMethod = clazz.getMethod("main", String[].class);
    } catch (final Exception e) {
        throw new IllegalStateException("Main class of command " + commandName + " does not have a static main method: " + mainClass, e);
    }
    try {
        // WARNING, Definitely do *not* unwrap 'new Object[]{args}' to 'args'
        mainMethod.invoke(clazz, new Object[] { args });
    } catch (final Throwable e) {
        if (line.hasOption("errors")) {
            e.printStackTrace();
        }
        System.exit(-10);
    }
}
Also used : ResourceFinder(org.apache.xbean.finder.ResourceFinder) Options(org.apache.commons.cli.Options) PosixParser(org.apache.commons.cli.PosixParser) IOException(java.io.IOException) Method(java.lang.reflect.Method) Properties(java.util.Properties) IOException(java.io.IOException) ParseException(org.apache.commons.cli.ParseException) CommandLine(org.apache.commons.cli.CommandLine) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException)

Example 62 with PosixParser

use of org.apache.commons.cli.PosixParser in project opennms by OpenNMS.

the class VmwareCimQuery method main.

public static void main(String[] args) throws ParseException {
    String hostname, username, password;
    final Options options = new Options();
    final CommandLineParser parser = new PosixParser();
    final CommandLine cmd = parser.parse(options, args);
    @SuppressWarnings("unchecked") List<String> arguments = (List<String>) cmd.getArgList();
    if (arguments.size() < 3) {
        usage(options, cmd);
        System.exit(1);
    }
    hostname = arguments.remove(0);
    username = arguments.remove(0);
    password = arguments.remove(0);
    cimQuery(hostname, username, password);
}
Also used : Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) PosixParser(org.apache.commons.cli.PosixParser) List(java.util.List) CommandLineParser(org.apache.commons.cli.CommandLineParser)

Example 63 with PosixParser

use of org.apache.commons.cli.PosixParser in project opennms by OpenNMS.

the class Installer method parseArguments.

/**
 * <p>parseArguments</p>
 *
 * @param argv an array of {@link java.lang.String} objects.
 * @throws java.lang.Exception if any.
 */
public void parseArguments(String[] argv) throws Exception {
    options.addOption("h", "help", false, "this help");
    // database-related options
    options.addOption("d", "do-database", false, "perform database actions");
    options.addOption("Z", "remove-database", false, "remove the OpenNMS database");
    options.addOption("u", "username", true, "username of the database account (default: 'opennms')");
    options.addOption("p", "password", true, "password of the database account (default: 'opennms')");
    options.addOption("a", "admin-username", true, "username of the database administrator (default: 'postgres')");
    options.addOption("A", "admin-password", true, "password of the database administrator (default: '')");
    options.addOption("D", "database-url", true, "JDBC database URL (default: jdbc:postgresql://localhost:5432/");
    options.addOption("P", "database-name", true, "name of the PostgreSQL database (default: opennms)");
    options.addOption("c", "clean-database", false, "this option does nothing");
    options.addOption("i", "insert-data", false, "insert (or upgrade) default data including database and XML configuration");
    options.addOption("s", "stored-procedure", false, "add the IPLIKE stored procedure if it's missing");
    options.addOption("U", "unicode", false, "upgrade the database to Unicode (deprecated, does nothing)");
    options.addOption("v", "vacuum", false, "vacuum (optimize) the database");
    options.addOption("f", "vacuum-full", false, "vacuum full the database (recovers unused disk space)");
    options.addOption("N", "ignore-not-null", false, "ignore NOT NULL constraint when transforming data");
    options.addOption("Q", "ignore-database-version", false, "disable the database version check");
    options.addOption("x", "database-debug", false, "turn on debugging for the database data transformation");
    options.addOption("R", "do-not-revert", false, "do not revert a table to the original if an error occurs");
    options.addOption("n", "skip-constraint", false, "");
    options.addOption("C", "repair-constraint", true, "fix rows that violate the specified constraint (sets key column to NULL)");
    options.addOption("X", "drop-constraint", false, "drop rows that match the constraint specified in -C, instead of fixing them");
    options.addOption("e", "extended-repairs", false, "enable extended repairs of old schemas");
    // tomcat-related options
    options.addOption("y", "do-webapp", false, "install web application (see '-w')");
    options.addOption("T", "tomcat-conf", true, "location of tomcat.conf");
    options.addOption("w", "tomcat-context", true, "location of the tomcat context (eg, conf/Catalina/localhost)");
    // general installation options
    options.addOption("l", "library-path", true, "library search path (directories separated by '" + File.pathSeparator + "')");
    options.addOption("r", "rpm-install", false, "RPM install (deprecated)");
    // upgrade tools options
    options.addOption("S", "skip-upgrade-tools", false, "Skip the execution of the upgrade tools (post-processing tasks)");
    CommandLineParser parser = new PosixParser();
    m_commandLine = parser.parse(options, argv);
    if (m_commandLine.hasOption("h")) {
        usage(options, m_commandLine);
        System.exit(0);
    }
    options.addOption("u", "username", true, "replaced by opennms-datasources.xml");
    options.addOption("p", "password", true, "replaced by opennms-datasources.xml");
    options.addOption("a", "admin-username", true, "replaced by opennms-datasources.xml");
    options.addOption("A", "admin-password", true, "replaced by opennms-datasources.xml");
    options.addOption("D", "database-url", true, "replaced by opennms-datasources.xml");
    options.addOption("P", "database-name", true, "replaced by opennms-datasources.xml");
    if (m_commandLine.hasOption("c")) {
        usage(options, m_commandLine, "The 'c' option was deprecated in 1.6, and disabled in 1.8.  You should backup and then drop the database before running install to reset your data.", null);
        System.exit(1);
    }
    if (m_commandLine.hasOption("u") || m_commandLine.hasOption("p") || m_commandLine.hasOption("a") || m_commandLine.hasOption("A") || m_commandLine.hasOption("D") || m_commandLine.hasOption("P")) {
        usage(options, m_commandLine, "The 'u', 'p', 'a', 'A', 'D', and 'P' options have all been superceded.\nPlease edit $OPENNMS_HOME/etc/opennms-datasources.xml instead.", null);
        System.exit(1);
    }
    // m_force = m_commandLine.hasOption("c");
    m_fix_constraint = m_commandLine.hasOption("C");
    m_fix_constraint_name = m_commandLine.getOptionValue("C");
    if (m_commandLine.hasOption("e")) {
        System.setProperty("opennms.contexts", "production,repair");
    }
    m_update_database = m_commandLine.hasOption("d");
    m_remove_database = m_commandLine.hasOption("Z");
    m_do_full_vacuum = m_commandLine.hasOption("f");
    m_do_inserts = m_commandLine.hasOption("i");
    m_library_search_path = m_commandLine.getOptionValue("l", m_library_search_path);
    m_skip_constraints = m_commandLine.hasOption("n");
    m_ignore_not_null = m_commandLine.hasOption("N");
    m_ignore_database_version = m_commandLine.hasOption("Q");
    m_do_not_revert = m_commandLine.hasOption("R");
    m_update_iplike = m_commandLine.hasOption("s");
    m_tomcat_conf = m_commandLine.getOptionValue("T", m_tomcat_conf);
    m_update_unicode = m_commandLine.hasOption("U");
    m_do_vacuum = m_commandLine.hasOption("v");
    m_webappdir = m_commandLine.getOptionValue("w", m_webappdir);
    m_installerDb.setDebug(m_commandLine.hasOption("x"));
    if (m_commandLine.hasOption("x")) {
        m_migrator.enableDebug();
    }
    m_fix_constraint_remove_rows = m_commandLine.hasOption("X");
    m_install_webapp = m_commandLine.hasOption("y");
    m_skip_upgrade_tools = m_commandLine.hasOption("S");
    if (m_commandLine.getArgList().size() > 0) {
        usage(options, m_commandLine, "Unknown command-line arguments: " + Arrays.toString(m_commandLine.getArgs()), null);
        System.exit(1);
    }
}
Also used : PosixParser(org.apache.commons.cli.PosixParser) CommandLineParser(org.apache.commons.cli.CommandLineParser)

Example 64 with PosixParser

use of org.apache.commons.cli.PosixParser in project opennms by OpenNMS.

the class CheckNsc method main.

/**
 * <p>main</p>
 *
 * @param args an array of {@link java.lang.String} objects.
 * @throws org.apache.commons.cli.ParseException if any.
 */
public static void main(String[] args) throws ParseException {
    Options options = new Options();
    options.addOption("port", true, "the port to connect to");
    options.addOption("password", true, "the password to use when connecting");
    options.addOption("warning", true, "treat the response as a warning if the level is above this value");
    options.addOption("critical", true, "treat the response as a critical error if the level is above this value");
    CommandLineParser parser = new PosixParser();
    CommandLine cmd = parser.parse(options, args);
    @SuppressWarnings("unchecked") List<String> arguments = cmd.getArgList();
    if (arguments.size() < 2) {
        usage(options, cmd);
        System.exit(1);
    }
    NsclientManager client = null;
    NsclientPacket response = null;
    NsclientCheckParams params = null;
    String host = arguments.remove(0);
    String command = arguments.remove(0);
    int warningLevel = 0;
    int criticalLevel = 0;
    int port = 1248;
    if (cmd.hasOption("warning")) {
        warningLevel = Integer.parseInt(cmd.getOptionValue("warning"));
    }
    if (cmd.hasOption("critical")) {
        criticalLevel = Integer.parseInt(cmd.getOptionValue("critical"));
    }
    if (cmd.hasOption("port")) {
        port = Integer.parseInt(cmd.getOptionValue("port"));
    }
    /* whatever's left gets merged into "arg1&arg2&arg3" */
    final StringBuilder clientParams = new StringBuilder();
    if (!arguments.isEmpty()) {
        for (Iterator<String> i = arguments.iterator(); i.hasNext(); ) {
            clientParams.append(i.next());
            if (i.hasNext()) {
                clientParams.append("&");
            }
        }
    }
    try {
        client = new NsclientManager(host, port);
    } catch (Throwable e) {
        usage(options, cmd, "An error occurred creating a new NsclientManager.", e);
    }
    if (cmd.hasOption("password")) {
        client.setPassword(cmd.getOptionValue("password"));
    }
    try {
        client.setTimeout(5000);
        client.init();
    } catch (Throwable e) {
        usage(options, cmd, "An error occurred initializing the NsclientManager.", e);
    }
    try {
        params = new NsclientCheckParams(warningLevel, criticalLevel, clientParams.toString());
    } catch (Throwable e) {
        usage(options, cmd, "An error occurred creating the parameter object.", e);
    }
    try {
        response = client.processCheckCommand(NsclientManager.convertStringToType(command), params);
    } catch (Throwable e) {
        usage(options, cmd, "An error occurred processing the command.", e);
    }
    if (response == null) {
        usage(options, cmd, "No response was returned.", null);
    } else {
        System.out.println("NsclientPlugin: " + command + ": " + NsclientPacket.convertStateToString(response.getResultCode()) + /* response.getResultCode() */
        " (" + response.getResponse() + ")");
    }
}
Also used : Options(org.apache.commons.cli.Options) PosixParser(org.apache.commons.cli.PosixParser) CommandLine(org.apache.commons.cli.CommandLine) CommandLineParser(org.apache.commons.cli.CommandLineParser)

Example 65 with PosixParser

use of org.apache.commons.cli.PosixParser in project zookeeper by apache.

the class DeleteAllCommand method parse.

@Override
public CliCommand parse(String[] cmdArgs) throws CliParseException {
    Parser parser = new PosixParser();
    CommandLine cl;
    try {
        cl = parser.parse(options, cmdArgs);
    } catch (ParseException ex) {
        throw new CliParseException(ex);
    }
    args = cl.getArgs();
    if (args.length < 2) {
        throw new CliParseException(getUsageStr());
    }
    return this;
}
Also used : CommandLine(org.apache.commons.cli.CommandLine) PosixParser(org.apache.commons.cli.PosixParser) ParseException(org.apache.commons.cli.ParseException) Parser(org.apache.commons.cli.Parser) PosixParser(org.apache.commons.cli.PosixParser)

Aggregations

PosixParser (org.apache.commons.cli.PosixParser)103 CommandLine (org.apache.commons.cli.CommandLine)97 CommandLineParser (org.apache.commons.cli.CommandLineParser)67 Options (org.apache.commons.cli.Options)56 ParseException (org.apache.commons.cli.ParseException)51 File (java.io.File)24 IOException (java.io.IOException)20 HelpFormatter (org.apache.commons.cli.HelpFormatter)16 Optional (java.util.Optional)10 Test (org.junit.Test)9 List (java.util.List)8 Configuration (org.apache.hadoop.conf.Configuration)8 GenericOptionsParser (org.apache.hadoop.util.GenericOptionsParser)7 SystemExitException (org.apache.openejb.cli.SystemExitException)7 Option (org.apache.commons.cli.Option)6 Parser (org.apache.commons.cli.Parser)6 Properties (java.util.Properties)5 Path (org.apache.hadoop.fs.Path)5 ArrayList (java.util.ArrayList)4 HBaseConfiguration (org.apache.hadoop.hbase.HBaseConfiguration)4