Search in sources :

Example 61 with CommandLine

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

the class Main method parseArguments.

private void parseArguments(String[] args) throws ParseException {
    Options options = new Options();
    options.addOption("h", "help", false, "this help");
    options.addOption("d", "debug", false, "write debug messages to the log");
    options.addOption("g", "gui", false, "start a GUI (default: false)");
    options.addOption("i", "disable-icmp", false, "disable ICMP/ping (overrides -Dorg.opennms.netmgt.icmp.pingerClass=)");
    options.addOption("l", "location", true, "the location name of this remote poller");
    options.addOption("u", "url", true, "the URL for OpenNMS (example: https://server-name/opennms-remoting)");
    options.addOption("n", "name", true, "the name of the user to connect as");
    options.addOption("p", "password", true, "the password to use when connecting");
    options.addOption("s", "scan-report", false, "perform a single scan report instead of running the polling engine");
    CommandLineParser parser = new PosixParser();
    CommandLine cl = parser.parse(options, args);
    if (cl.hasOption("h")) {
        usage(options);
        System.exit(1);
    }
    if (cl.hasOption("d")) {
    }
    if (cl.hasOption("i")) {
        m_disableIcmp = true;
    }
    if (cl.hasOption("l")) {
        m_locationName = cl.getOptionValue("l");
    }
    if (cl.hasOption("u")) {
        String arg = cl.getOptionValue("u").toLowerCase();
        try {
            m_uri = new URI(arg);
        } catch (URISyntaxException e) {
            usage(options);
            e.printStackTrace();
            System.exit(2);
        }
    } else {
        usage(options);
        System.exit(3);
    }
    if (cl.hasOption("g")) {
        m_gui = true;
    }
    if (cl.hasOption("s")) {
        m_scanReport = true;
    }
    if (cl.hasOption("n")) {
        m_username = cl.getOptionValue("n");
        m_password = cl.getOptionValue("p");
        if (m_password == null) {
            m_password = "";
        }
    }
    // to optionally get it from system properties
    if (m_username == null) {
        m_username = System.getProperty("opennms.poller.server.username");
        if (m_username != null) {
            m_password = System.getProperty("opennms.poller.server.password");
            if (m_password == null) {
                m_password = "";
            }
        }
    }
}
Also used : Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) PosixParser(org.apache.commons.cli.PosixParser) CommandLineParser(org.apache.commons.cli.CommandLineParser) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 62 with CommandLine

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

the class Mib2Events method parseCli.

public void parseCli(String[] argv) {
    Options opts = new Options();
    opts.addOption("m", "mib", true, "Pathname or URL of MIB file to scan for traps");
    opts.addOption("b", "ueibase", true, "Base UEI for resulting events");
    opts.addOption("c", "compat", false, "Turn on compatibility mode to create output as similar to mib2opennms as possible");
    CommandLineParser parser = new GnuParser();
    try {
        CommandLine cmd = parser.parse(opts, argv);
        if (cmd.hasOption('m')) {
            m_mibLocation = cmd.getOptionValue('m');
        } else {
            printHelp("You must specify a MIB file pathname or URL");
            System.exit(1);
        }
        if (cmd.hasOption("b")) {
            m_ueiBase = cmd.getOptionValue('b');
        }
        if (cmd.hasOption("c")) {
            m_compat = true;
        }
    } catch (ParseException e) {
        printHelp("Failed to parse command line options");
        System.exit(1);
    }
}
Also used : Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) GnuParser(org.apache.commons.cli.GnuParser) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException)

Example 63 with CommandLine

use of org.apache.commons.cli.CommandLine in project GNS by MobilityFirst.

the class EC2Runner method main.

/**
   * The main routine.
   *
   * @param args
   */
public static void main(String[] args) {
    try {
        CommandLine parser = initializeOptions(args);
        if (parser.hasOption("help")) {
            printUsage();
            System.exit(1);
        }
        String createRunsetName = parser.getOptionValue("create");
        String terminateRunsetName = parser.getOptionValue("terminate");
        String runsetDescribe = parser.getOptionValue("describe");
        String runSetWriteConfig = parser.getOptionValue("writeConfig");
        String dataStoreName = parser.getOptionValue("datastore");
        if (dataStoreName != null) {
            try {
                dataStoreType = DataStoreType.valueOf(dataStoreName);
            } catch (IllegalArgumentException e) {
                System.out.println("Unknown data store type " + dataStoreName + "; exiting.");
                System.exit(1);
            }
        }
        configName = createRunsetName != null ? createRunsetName : terminateRunsetName != null ? terminateRunsetName : runsetDescribe != null ? runsetDescribe : runSetWriteConfig != null ? runSetWriteConfig : null;
        System.out.println("Config name: " + configName);
        if (configName != null) {
            loadConfig(configName);
        }
        if (createRunsetName != null) {
            createRunSetMulti(createRunsetName);
        } else if (terminateRunsetName != null) {
            terminateRunSet(terminateRunsetName);
        } else if (runsetDescribe != null) {
            describeRunSet(runsetDescribe);
        } else if (runSetWriteConfig != null) {
            writeGNSINstallerConfForRunSet(runSetWriteConfig);
        } else {
            printUsage();
            System.exit(1);
        }
    } catch (ParseException e1) {
        e1.printStackTrace();
        printUsage();
        System.exit(1);
    }
    System.exit(0);
}
Also used : CommandLine(org.apache.commons.cli.CommandLine) ParseException(org.apache.commons.cli.ParseException)

Example 64 with CommandLine

use of org.apache.commons.cli.CommandLine in project GNS by MobilityFirst.

the class ThroughputAsynchMultiClientTest method main.

/**
   * The main routine run from the command line.
   *
   * @param args
   * @throws Exception
   */
public static void main(String[] args) throws Exception {
    try {
        CommandLine parser = initializeOptions(args);
        if (parser.hasOption("help")) {
            printUsage();
            System.exit(1);
        }
        String alias = parser.getOptionValue("alias");
        String host = parser.getOptionValue("host");
        String port = parser.getOptionValue("port");
        boolean disableSSL = parser.hasOption("disableSSL");
        if (parser.hasOption("op") && ("update".equals(parser.getOptionValue("op")) || "write".equals(parser.getOptionValue("op")))) {
            doingReads = false;
            System.out.println("Testing updates");
        } else {
            doingReads = true;
            System.out.println("Testing reads");
        }
        int rate = (parser.hasOption("rate")) ? Integer.parseInt(parser.getOptionValue("rate")) : 1;
        int increment = (parser.hasOption("inc")) ? Integer.parseInt(parser.getOptionValue("inc")) : 10;
        numberOfClients = (parser.hasOption("clients")) ? Integer.parseInt(parser.getOptionValue("clients")) : DEFAULT_NUMBER_OF_CLIENTS;
        int requestsPerClient = (parser.hasOption("requests")) ? Integer.parseInt(parser.getOptionValue("requests")) : DEFAULT_NUMBER_REQUESTS_PER_CLIENT;
        numberOfGuids = (parser.hasOption("guids")) ? Integer.parseInt(parser.getOptionValue("guids")) : 1;
        updateAlias = parser.hasOption("updateAlias") ? parser.getOptionValue("updateAlias") : null;
        updateField = parser.hasOption("updateField") ? parser.getOptionValue("updateField") : DEFAULT_FIELD;
        updateValue = parser.hasOption("updateValue") ? parser.getOptionValue("updateValue") : DEFAULT_VALUE;
        ThroughputAsynchMultiClientTest test = new ThroughputAsynchMultiClientTest(alias);
        test.createSubGuidsAndWriteValue(parser.hasOption("useExistingGuids"));
        // prebuild a packet for each client
        for (int clientIndex = 0; clientIndex < numberOfClients; clientIndex++) {
            for (int i = 0; i < numberOfGuids; i++) {
                if (doingReads) {
                    commmandPackets[i][clientIndex] = createReadCommandPacket(clients[clientIndex], subGuids[i], updateField, masterGuid);
                } else {
                    JSONObject json = new JSONObject();
                    json.put(updateField, updateValue);
                    commmandPackets[i][clientIndex] = createUpdateCommandPacket(clients[clientIndex], subGuids[i], json, masterGuid);
                }
            }
        }
        if (parser.hasOption("inc")) {
            test.ramp(rate, increment, requestsPerClient);
        } else if (parser.hasOption("rate")) {
            test.ramp(rate, 0, requestsPerClient);
        } else {
            // should really do this earlier
            printUsage();
            System.exit(1);
        }
        // cleanup
        test.removeSubGuid();
        for (int i = 0; i < numberOfClients; i++) {
            clients[i].close();
        }
        System.exit(0);
    } catch (HeadlessException e) {
        System.out.println("When running headless you'll need to specify the host and port on the command line");
        printUsage();
        System.exit(1);
    }
}
Also used : CommandLine(org.apache.commons.cli.CommandLine) JSONObject(org.json.JSONObject) HeadlessException(java.awt.HeadlessException) RandomString(edu.umass.cs.gnscommon.utils.RandomString)

Example 65 with CommandLine

use of org.apache.commons.cli.CommandLine in project wildfly by wildfly.

the class CommandLineMain method main.

/**
     * Creates a JBoss Diagnostic Reporter (JDR) Report. A JDR report response
     * is printed to <code>System.out</code>.
     *
     * @param args ignored
     */
public static void main(String[] args) {
    int port = 9990;
    String host = "localhost";
    String protocol = "http-remoting";
    String config = null;
    try {
        CommandLine line = parser.parse(options, args, false);
        if (line.hasOption("help")) {
            formatter.printHelp(usage, NEW_LINE + JdrLogger.ROOT_LOGGER.jdrDescriptionMessage(), options, null);
            return;
        }
        if (line.hasOption("host")) {
            host = line.getOptionValue("host");
        }
        if (line.hasOption("port")) {
            port = Integer.parseInt(line.getOptionValue("port"));
        }
        if (line.hasOption("protocol")) {
            protocol = line.getOptionValue("protocol");
        }
        if (line.hasOption("config")) {
            config = line.getOptionValue("config");
        }
    } catch (ParseException e) {
        System.out.println(e.getMessage());
        formatter.printHelp(usage, options);
        return;
    } catch (NumberFormatException nfe) {
        System.out.println(nfe.getMessage());
        formatter.printHelp(usage, options);
        return;
    }
    System.out.println("Initializing JBoss Diagnostic Reporter...");
    // Try to run JDR on the Wildfly JVM
    CLI cli = CLI.newInstance();
    boolean embedded = false;
    JdrReport report = null;
    try {
        System.out.println(String.format("Trying to connect to %s %s:%s", protocol, host, port));
        cli.connect(protocol, host, port, null, null);
    } catch (IllegalStateException ex) {
        System.out.println("Starting embedded server");
        String startEmbeddedServer = "embed-server --std-out=echo " + ((config != null && !config.isEmpty()) ? (" --server-config=" + config) : "");
        cli.getCommandContext().handleSafe(startEmbeddedServer);
        embedded = true;
    }
    try {
        Result cmdResult = cli.cmd("/subsystem=jdr:generate-jdr-report()");
        ModelNode response = cmdResult.getResponse();
        if (Operations.isSuccessfulOutcome(response) || !embedded) {
            reportFailure(response);
            ModelNode result = response.get(ClientConstants.RESULT);
            report = new JdrReport(result);
        } else {
            report = standaloneCollect(cli, protocol, host, port);
        }
    } catch (IllegalStateException ise) {
        System.out.println(ise.getMessage());
        report = standaloneCollect(cli, protocol, host, port);
    } finally {
        if (cli != null) {
            try {
                if (embedded)
                    cli.getCommandContext().handleSafe("stop-embedded-server");
                else
                    cli.disconnect();
            } catch (Exception e) {
                System.out.println("Caught exception while disconnecting: " + e.getMessage());
            }
        }
    }
    printJdrReportInfo(report);
    System.exit(0);
}
Also used : CLI(org.jboss.as.cli.scriptsupport.CLI) CommandLine(org.apache.commons.cli.CommandLine) ParseException(org.apache.commons.cli.ParseException) ModelNode(org.jboss.dmr.ModelNode) OperationFailedException(org.jboss.as.controller.OperationFailedException) ParseException(org.apache.commons.cli.ParseException) Result(org.jboss.as.cli.scriptsupport.CLI.Result)

Aggregations

CommandLine (org.apache.commons.cli.CommandLine)968 Options (org.apache.commons.cli.Options)560 ParseException (org.apache.commons.cli.ParseException)464 CommandLineParser (org.apache.commons.cli.CommandLineParser)459 HelpFormatter (org.apache.commons.cli.HelpFormatter)259 GnuParser (org.apache.commons.cli.GnuParser)230 DefaultParser (org.apache.commons.cli.DefaultParser)213 PosixParser (org.apache.commons.cli.PosixParser)177 File (java.io.File)165 IOException (java.io.IOException)162 Test (org.junit.Test)144 Option (org.apache.commons.cli.Option)131 ArrayList (java.util.ArrayList)63 Path (org.apache.hadoop.fs.Path)60 BasicParser (org.apache.commons.cli.BasicParser)50 Configuration (org.apache.hadoop.conf.Configuration)41 Configuration (org.apache.flink.configuration.Configuration)37 HashMap (java.util.HashMap)34 List (java.util.List)34 Properties (java.util.Properties)33