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:com.github.hexosse.memworth.Cli.java

/**
 * @return Cli/*ww w .  ja v a 2 s.c  o  m*/
 */
public Cli parse() {
    CommandLineParser parser = new DefaultParser();
    CommandLine cmd = null;

    try {
        cmd = parser.parse(options, args);

        if (cmd.hasOption("h"))
            help();
        if (cmd.hasOption("c"))
            csvFile = cmd.getOptionValue("c");
        if (cmd.hasOption("w"))
            ymlFile = cmd.getOptionValue("w");
        if (cmd.hasOption("n"))
            useName = true;
        if (cmd.hasOption("i"))
            useName = false;

    } catch (ParseException e) {
        log.log(Level.SEVERE, "Failed to parse comand line properties", e);
        help();
    }
    return this;
}

From source file:com.jonasjberg.voltdivx.arguments.ArgumentParser.java

public void parse() {
    CommandLineParser parser = new DefaultParser();
    CommandLine cmd = null;// ww w . j av a  2  s . co m

    SiUnitParser siParser = new SiUnitParser();

    try {
        cmd = parser.parse(ArgumentOptions.getOptions(), args);

        if (cmd.hasOption("h") || cmd.getOptions().length == 0) {
            Main.printLogo();
            Help.displayHelp();
            Main.exit(0);
        }

        else {
            if (cmd.hasOption("i")) {
                double opt = siParser.parse(cmd.getOptionValue("i"));
                contents.setInputVoltage(opt);
            }

            if (cmd.hasOption("o")) {
                double opt = siParser.parse(cmd.getOptionValue("o"));
                contents.setOutputVoltage(opt);
            }

            if (cmd.hasOption("g")) {
                double opt = siParser.parse(cmd.getOptionValue("g"));
                contents.setGroundVoltage(opt);
            }

            if (cmd.hasOption("r")) {
                double opt = siParser.parse(cmd.getOptionValue("r"));
                contents.setR1Resistance(opt);
            }

            if (cmd.hasOption("t")) {
                double opt = siParser.parse(cmd.getOptionValue("t"));
                contents.setR2Resistance(opt);
            }

            if (cmd.hasOption("s")) {
                String opt = cmd.getOptionValue("s");
                contents.setValueSeries(parseValueSeries(opt));

                /* Set tolerancePercent to the tolerance of the selected value
                 * series as default if the error option is not used. */
                //  if (!cmd.hasOption("e")) {
                //      double tolerancePercent = 100 * contents.getValueSeries().getTolerance();
                //      System.out.println("tolerancePercent = " + tolerancePercent);
                //      contents.setMaxErrorPercent(tolerancePercent);
                //  }

            } else {
                contents.setValueSeries(null);
            }

            if (cmd.hasOption("l")) {
                String opt = cmd.getOptionValue("l");
                int i = Integer.parseInt(opt);

                contents.setNumberOfLines(i);
            }

            if (cmd.hasOption("c")) {
                double opt = siParser.parse(cmd.getOptionValue("c"));
                contents.setMaxCurrent(opt);
            }

            if (cmd.hasOption("e")) {
                String opt = cmd.getOptionValue("e");

                if (opt.contains("%")) {
                    opt = opt.replaceAll("%", "").trim();
                    contents.setMaxErrorPercent(Double.parseDouble(opt));
                } else {
                    contents.setMaxErrorVoltage(siParser.parse(opt));
                }
            }

            if (cmd.hasOption("q")) {
                contents.setFlagQuiet(true);
            }
        }

    } catch (ParseException e) {
        // e.printStackTrace();
        System.err.println("Caught exception:" + e.getMessage());
    }
}

From source file:alluxio.cli.AbstractCommand.java

@Override
public CommandLine parseAndValidateArgs(String... args) throws InvalidArgumentException {
    Options opts = getOptions();/* ww  w.ja  v  a 2  s.co m*/
    CommandLineParser parser = new DefaultParser();
    CommandLine cmd;

    try {
        cmd = parser.parse(opts, args);
    } catch (ParseException e) {
        throw new InvalidArgumentException(String.format("Failed to parse args for %s", getCommandName()), e);
    }

    validateArgs(cmd.getArgs());
    return cmd;
}

From source file:net.librec.tool.driver.RecDriver.java

/**
 * Execute the command with the given arguments.
 *
 * @param args command specific arguments.
 * @return exit code.//from ww w  .  ja  v a 2 s .c o m
 * @throws Exception if error occurs
 */
public int run(String[] args) throws Exception {
    // init options
    Options options = new Options();
    options.addOption("build", false, "build model");
    options.addOption("load", false, "load model");
    options.addOption("save", false, "save model");
    options.addOption("exec", false, "run job");
    options.addOption("conf", true, "the path of configuration file");
    options.addOption("jobconf", true, "a specified key-value pair for configuration");
    options.addOption("D", true, "a specified key-value pair for configuration");
    // parse options
    CommandLineParser parser = new DefaultParser();
    CommandLine cmd = parser.parse(options, args, false);
    // init configuration
    Configuration conf = new Configuration();
    if (cmd.hasOption("conf")) {
        String confFilePath = cmd.getOptionValue("conf");
        Properties prop = new Properties();
        prop.load(new FileInputStream(confFilePath));
        for (String name : prop.stringPropertyNames()) {
            conf.set(name, prop.getProperty(name));
        }
    }
    if (cmd.hasOption("jobconf")) {
        String[] optionValues = cmd.getOptionValues("jobconf");
        for (String optionValue : optionValues) {
            String[] keyValuePair = optionValue.split("=");
            conf.set(keyValuePair[0], keyValuePair[1]);
        }
    }
    if (cmd.hasOption("D")) {
        String[] optionValues = cmd.getOptionValues("D");
        for (String optionValue : optionValues) {
            String[] keyValuePair = optionValue.split("=");
            conf.set(keyValuePair[0], keyValuePair[1]);
        }
    }
    //run job
    RecommenderJob job = new RecommenderJob(conf);
    job.runJob();
    System.out.print("Finished");
    return 0;
}

From source file:de.static_interface.sinklibrary.api.command.SinkCommandOptions.java

public CommandLineParser getCliParser() {
    if (cliParser == null) {
        cliParser = new DefaultParser();
    }

    return cliParser;
}

From source file:com.ibm.replication.iidr.warehouse.CollectCDCStatsParms.java

public CollectCDCStatsParms(String[] commandLineArguments) throws CollectCDCStatsParmsException {
    // Initialize parameters
    debug = false;/*w w w  .  j  ava 2  s .  c o  m*/
    datastore = "";
    formatter = new HelpFormatter();
    parser = new DefaultParser();
    options = new Options();
    subscriptions = null;

    options.addOption("d", false, "Show debug messages");
    options.addOption("ds", true, "Source datastore");
    options.addOption("s", true, "Subscription(s) to select. If not specified, all subscriptions"
            + " of the selected source datastore will be included");
    options.addOption("p", true,
            "Properties file (must exist in the conf directory). Default is CollectCDCStats.properties");
    options.addOption("l", true,
            "Log4j2 configuration file (must exist in the conf directory). Default is log4j2.xml");

    try {
        commandLine = parser.parse(options, commandLineArguments);
    } catch (ParseException e) {
        sendInvalidParameterException("");
    }

    this.debug = commandLine.hasOption("d");

    // Datastore parameter is mandatory
    if (commandLine.getOptionValue("ds") != null) {
        datastore = commandLine.getOptionValue("ds");
        if (commandLine.getOptionValue("s") != null) {
            subscriptions = commandLine.getOptionValue("s");
            subscriptionList = new ArrayList<String>(Arrays.asList(subscriptions.split(",")));
        }
        if (commandLine.getOptionValue("p") != null) {
            propertiesFile = commandLine.getOptionValue("p");
        } else
            propertiesFile = "CollectCDCStats.properties";

        if (commandLine.getOptionValue("l") != null) {
            loggingConfigurationFile = commandLine.getOptionValue("l");
        } else
            loggingConfigurationFile = "log4j2.xml";

    } else
        sendInvalidParameterException("Datastore (ds parameter) must be specified");

}

From source file:de.topobyte.utilities.apache.commons.cli.TestMulti.java

License:asdf

@Test
public void testString() throws ParseException {
    Options options = new Options();
    OptionHelper.addS(options, "foo", true, true, "an option");
    OptionHelper.addS(options, "bar", true, true, "another option");

    String[] arguments = new String[] { "-foo", "asdf", "-bar", "test", "-bar", "more" };
    CommandLine line = new DefaultParser().parse(options, arguments);

    StringOption foo = ArgumentHelper.getString(line, "foo");
    StringOption bar = ArgumentHelper.getString(line, "bar");

    assertTrue(foo.hasValue());//w w  w  .j a  v a 2s . com
    assertTrue(bar.hasValue());
    assertEquals("asdf", foo.getValue());
    assertEquals("test", bar.getValue());

    List<StringOption> bars = ArgumentHelper.getStrings(line, "bar");
    assertEquals(2, bars.size());
    assertEquals("test", bars.get(0).getValue());
    assertEquals("more", bars.get(1).getValue());
}

From source file:io.github.discovermovies.datacollector.movie.Application.java

public void start(String[] args) {
    options = getOptions();//from  w  ww .j  a  v  a 2  s  .c  o m
    CommandLineParser parser = new DefaultParser();
    try {
        CommandLine cmd = parser.parse(options, args);
        executeCommand(cmd);
    } catch (ParseException e) {
        System.err.println(e.getMessage());
        printHelp();
    }
}

From source file:com.twitter.heron.integration_test.common.AbstractTestTopology.java

protected AbstractTestTopology(String[] args) throws MalformedURLException {
    CommandLineParser parser = new DefaultParser();
    HelpFormatter formatter = new HelpFormatter();
    Options options = getArgOptions();/*  www  .  j a v a 2 s . c o  m*/

    try {
        cmd = parser.parse(options, args);
    } catch (ParseException e) {
        formatter.setWidth(120);
        formatter.printHelp("java " + getClass().getCanonicalName(), options, true);
        throw new RuntimeException(e);
    }

    this.topologyName = cmd.getOptionValue(TOPOLOGY_OPTION);
    this.httpServerResultsUrl = pathAppend(cmd.getOptionValue(RESULTS_URL_OPTION), this.topologyName);
    this.httpServerStateUrl = pathAppend(cmd.getOptionValue(STATE_URL_OPTION), this.topologyName);
    this.stateUpdateToken = cmd.getOptionValue(STATE_UPDATE_TOKEN);
}

From source file:com.mapd.parser.server.CalciteServerCaller.java

private void doWork(String[] args) {
    CalciteServerWrapper calciteServerWrapper = null;

    // create Options object
    Options options = new Options();

    Option port = Option.builder("p").hasArg().desc("port number").longOpt("port").build();

    Option data = Option.builder("d").hasArg().desc("data directory").required().longOpt("data").build();

    Option extensions = Option.builder("e").hasArg().desc("extension signatures directory")
            .longOpt("extensions").build();

    options.addOption(port);/*  w  w  w. j a  v  a  2 s.  com*/
    options.addOption(data);
    options.addOption(extensions);

    CommandLineParser parser = new DefaultParser();

    try {
        cmd = parser.parse(options, args);
    } catch (ParseException ex) {
        MAPDLOGGER.error(ex.getLocalizedMessage());
        help(options);
        exit(0);
    }

    int portNum = Integer.valueOf(cmd.getOptionValue("port", "9093"));
    String dataDir = cmd.getOptionValue("data", "data");
    String extensionsDir = cmd.getOptionValue("extensions", "build/QueryEngine");
    final Path extensionFunctionsAstFile = Paths.get(extensionsDir, "ExtensionFunctions.ast");

    //Add logging to our log files directories
    Properties p = new Properties();
    try {
        p.load(getClass().getResourceAsStream("/log4j.properties"));
    } catch (IOException ex) {
        MAPDLOGGER.error("Could not load log4j property file from resources " + ex.getMessage());
    }
    p.put("log.dir", dataDir); // overwrite "log.dir"
    PropertyConfigurator.configure(p);

    calciteServerWrapper = new CalciteServerWrapper(portNum, -1, dataDir, extensionFunctionsAstFile.toString());

    while (true) {
        try {
            Thread t = new Thread(calciteServerWrapper);
            t.start();
            t.join();
            if (calciteServerWrapper.shutdown()) {
                break;
            }
        } catch (Exception x) {
            x.printStackTrace();
        }
    }
}