Example usage for org.apache.commons.cli DefaultParser DefaultParser

List of usage examples for org.apache.commons.cli DefaultParser DefaultParser

Introduction

In this page you can find the example usage for org.apache.commons.cli DefaultParser DefaultParser.

Prototype

DefaultParser

Source Link

Usage

From source file:at.salzburgresearch.vgi.vgianalyticsframework.activityanalysis.application.VgiQuadtreeBuilder.java

public static void launch(Options options, String[] args) {

    CommandLineParser parser = new DefaultParser();
    CommandLine cmd = null;//from   www .  j av  a  2 s  .  co m
    try (ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
            "classpath:/application-context-vgi-quadtree.xml")) {
        cmd = parser.parse(options, args);

        File settingsFile = null;

        if (cmd.hasOption('h')) {
            HelpFormatter helpFormatter = new HelpFormatter();
            helpFormatter.printHelp("java -jar vgi-activity-1.0.jar [OPTION]...", options);
            return;
        }
        if (cmd.hasOption('s')) {
            settingsFile = new File(cmd.getOptionValue('s'));
            if (!settingsFile.exists()) {
                log.warn("Setting file does not exist! (" + settingsFile.getAbsolutePath() + ")");
                settingsFile = null;
            }
        } else {
            log.warn("No setting file specified! Use option -s to specify a settings XML file");
            System.exit(0);
            settingsFile = null;
        }

        IVgiPipeline pipeline = ((IVgiPipeline) ctx.getBean("vgiPipeline"));

        IVgiPipelineSettings settings = ((IVgiPipelineSettings) ctx.getBean("vgiPipelineSettings"));
        if (!settings.loadSettings(settingsFile)) {
            System.exit(1);
        }

        pipeline.start();

    } catch (Exception e) {
        HelpFormatter helpFormatter = new HelpFormatter();
        if (e.getMessage() != null) {
            log.error(e.getMessage() + '\n');
        } else {
            log.error("Unknown error", e);
        }
        helpFormatter.printHelp("java -jar vgi-activity-1.0.jar [OPTION]...", options);
    }
}

From source file:it.lic.cli.Main.java

private Main(String[] args) throws Exception {
    this.options = new Options();
    this.options.addOption("h", false, "help");
    this.options.addOption("l", true, "show named keypair");
    this.options.addOption("L", true, "list licenses of named keypair");
    this.options.addOption("k", true, "new named keypair");
    this.options.addOption("K", true, "new license for named keypair");
    final CommandLineParser parser = new DefaultParser();
    final CommandLine cli = parser.parse(this.options, args);
    this.run(cli);
}

From source file:gobblin.util.limiter.stressTest.StressTestUtils.java

/**
 * Parse command line.// ww  w  .  j a va 2s  .  c o  m
 */
public static CommandLine parseCommandLine(Options options, String[] args) throws ParseException {
    CommandLineParser parser = new DefaultParser();
    CommandLine cli = parser.parse(options, args);

    if (cli.hasOption(StressTestUtils.HELP_OPT.getOpt())) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp(MRStressTest.class.getSimpleName(), OPTIONS);
        System.exit(0);
    }

    return cli;
}

From source file:de.topobyte.osm4j.utils.AbstractExecutable.java

protected void setup(String[] args) {
    try {// w w w  .j  av  a  2s  .  c o  m
        line = new DefaultParser().parse(options, args);
    } catch (ParseException e) {
        System.out.println("unable to parse command line: " + e.getMessage());
        new HelpFormatter().printHelp(getHelpMessage(), options);
        System.exit(1);
    }
}

From source file:hrytsenko.gscripts.AppArgs.java

/**
 * Parse the command-line arguments./*ww w  . j a va 2  s.  com*/
 * 
 * @param args
 *            the list of arguments.
 * 
 * @return the parsed arguments.
 * 
 * @throws ParseException
 *             if command-line arguments could not be parsed.
 */
public static AppArgs parseArgs(String[] args) throws ParseException {
    CommandLineParser parser = new DefaultParser();
    CommandLine line = parser.parse(getOptions(), args);

    String[] scripts = getValues(line, SCRIPTS_OPT_NAME);
    String[] scriptsArgs = getValues(line, SCRIPTS_ARGS_OPT_NAME);

    return new AppArgs(scripts, scriptsArgs);
}

From source file:com.gomoob.embedded.EmbeddedMongo.java

/**
 * Function used to parse the command line.
 * /* w w w.  j av a  2s .  com*/
 * @param args Arguments passed at command line.
 * 
 * @throws Exception
 */
private static void parseCommandLine(String[] args) throws Exception {

    CommandLineParser parser = new DefaultParser();

    Options options = new Options();
    options.addOption("mp", "mongo-port", true, "The port used by Mongo DB.");
    options.addOption("mv", "mongo-version", true, "The version of Mongo DB to use.");
    options.addOption("sp", "socket-port", true, "The port number used by the server socket.");

    CommandLine commandLine = parser.parse(options, args);

    // Read the mongo port
    if (commandLine.hasOption("mongo-port")) {

        context.getMongoContext().setPort(Integer.parseInt(commandLine.getOptionValue("mongo-port")));

    }

    // Reads the socket port
    if (commandLine.hasOption("socket-port")) {

        context.getSocketContext().setPort(Integer.parseInt(commandLine.getOptionValue("socket-port")));

    }

}

From source file:com.antsdb.saltedfish.util.CommandLineHelper.java

public default CommandLine parse(Options options, String[] args) throws ParseException {
    CommandLineParser parser = new DefaultParser();
    CommandLine line = parser.parse(options, args);
    return line;//from w  ww .jav a2 s.com
}

From source file:com.twcable.grabbit.tools.cli.CliOptions.java

/**
 * Parse the arguments./*from w w w .  j av  a  2  s.c  o m*/
 *
 * @param args the command line arguments
 * @return Right if successfully parsed; otherwise Left with the error message/help
 */
@SuppressWarnings("PMD.UseVarargs")
public static Either<String, CliOptions> create(String[] args) {
    val help = new Option("h", "help", false, "Show usage information");
    val start = new Option("s", "start", false, "Start Grabbit");
    val monitor = new Option("m", "monitor", false, "Monitor Grabbit");

    val options = new Options();
    options.addOption(help);
    options.addOption(start);
    options.addOption(monitor);

    val parser = new DefaultParser();
    try {
        // parse the command line arguments
        val line = parser.parse(options, args);

        if (line.hasOption('h') || !hasValidOptions(line)) {
            val formatter = new HelpFormatter();
            val stringWriter = new StringWriter();
            formatter.printHelp(new PrintWriter(stringWriter), formatter.getWidth(),
                    "grabbit-cli -[h|s|sm|m] [grabbit-job-config-file] [env-config-file] [env] [job-ids-cache-file]",
                    "Starts and/or monitors jobs on the Grabbit client", options, formatter.getLeftPadding(),
                    formatter.getDescPadding(), "", false);
            return Either.left(stringWriter.toString());
        }

        val argList = line.getArgList();

        if (line.hasOption('s')) {
            return Either.right(new CliOptions(true, line.hasOption('m'), argList.get(0), argList.get(1),
                    argList.get(2), null));
        } else {
            return Either
                    .right(new CliOptions(false, true, null, argList.get(0), argList.get(1), argList.get(2)));
        }
    } catch (ParseException exp) {
        return Either.left("Parsing failed.  Reason: " + exp.getMessage());
    }
}

From source file:Jimbo.Cheerlights.Listener.java

/**
 * Setup the appropriate inputs to feed into the target. It parses
 * the command line for the available options and creates them as needed.
 * /*from   w  w w.ja va2 s  .  c o m*/
 * @param args The command line arguments
 * @param target The CheerListener to feed data into
 * 
 * @return If anything was created.
 */
public static boolean setup(String args[], CheerListener target) {
    // Decode the command line arguments
    Options options = new Options();

    options.addOption("b", MQTT_BROKER_KEY, true, "URL of the broker")
            .addOption("c", MQTT_CLIENT_KEY, true, "Client ID")
            .addOption("t", MQTT_TOPIC_KEY, true, "Topic to subscribe to")
            .addOption("m", "multicast", false, "enable multicast listener");

    target.add_options(options);

    CommandLineParser parser = new DefaultParser();
    CommandLine command;

    boolean something_worked = false;

    try {
        // Parse it up
        command = parser.parse(options, args);

        target.handle_args(command);

        MQTTListener mqtt = null;
        String mqtt_topic = Listener.DEFAULT_MQTT_TOPIC;

        if (command.hasOption(Listener.MQTT_BROKER_KEY)) {
            if (!command.hasOption(Listener.MQTT_CLIENT_KEY))
                throw new ParseException("MQTT without client name");

            if (command.hasOption(Listener.MQTT_TOPIC_KEY))
                mqtt_topic = command.getOptionValue(Listener.MQTT_TOPIC_KEY);

            try {
                mqtt = new MQTTListener(command.getOptionValue(Listener.MQTT_BROKER_KEY),
                        command.getOptionValue(Listener.MQTT_CLIENT_KEY), mqtt_topic, target);

                something_worked = true;
            }

            catch (MqttException e) {
                LOG.log(Level.WARNING, "Failed to create MQTT client: {0}", e.getLocalizedMessage());
            }
        } else {
            if (command.hasOption(Listener.MQTT_TOPIC_KEY))
                LOG.warning("MQTT topic supplied but no broker");

            if (command.hasOption(Listener.MQTT_CLIENT_KEY))
                LOG.warning("MQTT client name but no broker");
        }

        if (command.hasOption("multicast")) {
            MessageListener l = new MessageListener(target);
            l.go();
            something_worked = true;
        }
    }

    catch (ParseException e) {
        System.err.println("Command line arguments failed: " + e.getLocalizedMessage());
    }

    return something_worked;
}

From source file:com.geeksaga.light.console.Main.java

CommandLine parseOption(String[] arguments) {
    CommandLineParser commandLineParser = new DefaultParser();

    try {// w  w w  . j a v  a 2 s. co  m
        return commandLineParser.parse(getOptions(), arguments);
    } catch (ParseException parseException) {
        System.err.println("Parsing failed.  Reason: " + parseException.getMessage());
    }

    return null;
}