Example usage for org.apache.commons.cli CommandLine hasOption

List of usage examples for org.apache.commons.cli CommandLine hasOption

Introduction

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

Prototype

public boolean hasOption(char opt) 

Source Link

Document

Query to see if an option has been set.

Usage

From source file:edu.upc.eetac.dsa.exercices.java.lang.exceptions.App.java

public static void main(String[] args) throws FileNotFoundException {
    String filename = null;//from   ww  w .ja  va2s . c  om
    String maxInteger = null;

    Options options = new Options();
    Option optionFile = OptionBuilder.withArgName("file").hasArg().withDescription("file with integers")
            .withLongOpt("file").create("f");
    options.addOption(optionFile);
    Option optionMax = OptionBuilder.withArgName("max").hasArg()
            .withDescription("maximum integer allowed in the file").withLongOpt("max").create("M");
    options.addOption(optionFile);
    options.addOption(optionMax);

    options.addOption("h", "help", false, "prints this message");

    CommandLineParser parser = new GnuParser();
    CommandLine line = null;
    try {
        // parse the command line arguments
        line = parser.parse(options, args);
        if (line.hasOption("h")) { // No hace falta preguntar por el parmetro "help". Ambos son sinnimos
            new HelpFormatter().printHelp(App.class.getCanonicalName(), options);
            return;
        }
        filename = line.getOptionValue("f");
        if (filename == null) {
            throw new org.apache.commons.cli.ParseException(
                    "You must provide the path to the file with numbers.");
        }
        maxInteger = line.getOptionValue("M");
    } catch (ParseException exp) {
        System.err.println(exp.getMessage());
        new HelpFormatter().printHelp(App.class.getCanonicalName(), options);
        return;
    }

    try {
        int[] numbers = (maxInteger != null) ? FileNumberReader.readFile(filename, Integer.parseInt(maxInteger))
                : FileNumberReader.readFile(filename);
        for (int i = 0; i < numbers.length; i++) {
            System.out.println("Integer read: " + numbers[i]);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (BigNumberException e) {
        e.printStackTrace();
    }
}

From source file:com.dasasian.chok.tool.ZkTool.java

public static void main(String[] args) {
    final Options options = new Options();

    Option lsOption = new Option("ls", true, "list zp path contents");
    lsOption.setArgName("path");
    Option readOption = new Option("read", true, "read and print zp path contents");
    readOption.setArgName("path");
    Option rmOption = new Option("rm", true, "remove zk files");
    rmOption.setArgName("path");
    Option rmrOption = new Option("rmr", true, "remove zk directories");
    rmrOption.setArgName("path");

    OptionGroup actionGroup = new OptionGroup();
    actionGroup.setRequired(true);/*  w w w.j  a va2s .com*/
    actionGroup.addOption(lsOption);
    actionGroup.addOption(readOption);
    actionGroup.addOption(rmOption);
    actionGroup.addOption(rmrOption);
    options.addOptionGroup(actionGroup);

    final CommandLineParser parser = new GnuParser();
    HelpFormatter formatter = new HelpFormatter();
    try {
        final CommandLine line = parser.parse(options, args);
        ZkTool zkTool = new ZkTool();
        if (line.hasOption(lsOption.getOpt())) {
            String path = line.getOptionValue(lsOption.getOpt());
            zkTool.ls(path);
        } else if (line.hasOption(readOption.getOpt())) {
            String path = line.getOptionValue(readOption.getOpt());
            zkTool.read(path);
        } else if (line.hasOption(rmOption.getOpt())) {
            String path = line.getOptionValue(rmOption.getOpt());
            zkTool.rm(path, false);
        } else if (line.hasOption(rmrOption.getOpt())) {
            String path = line.getOptionValue(rmrOption.getOpt());
            zkTool.rm(path, true);
        }
        zkTool.close();
    } catch (ParseException e) {
        System.out.println(e.getClass().getSimpleName() + ": " + e.getMessage());
        formatter.printHelp(ZkTool.class.getSimpleName(), options);
    }

}

From source file:com.etsy.arbiter.Arbiter.java

public static void main(String[] args) throws ParseException, ConfigurationException, IOException,
        ParserConfigurationException, TransformerException {
    Options options = getOptions();// w ww . j  av a  2s . c om

    CommandLineParser cmd = new GnuParser();
    CommandLine parsed = cmd.parse(options, args);

    if (parsed.hasOption("h")) {
        printUsage(options);
    }

    if (!parsed.hasOption("i")) {
        throw new ParseException("Missing required argument: i");
    }

    if (!parsed.hasOption("o")) {
        throw new ParseException("Missing required argument: o");
    }

    String[] configFiles = parsed.getOptionValues("c");
    String[] lowPrecedenceConfigFiles = parsed.getOptionValues("l");

    String[] inputFiles = parsed.getOptionValues("i");
    String outputDir = parsed.getOptionValue("o");

    List<Config> parsedConfigFiles = readConfigFiles(configFiles, false);
    parsedConfigFiles.addAll(readConfigFiles(lowPrecedenceConfigFiles, true));
    Config merged = ConfigurationMerger.mergeConfiguration(parsedConfigFiles);

    List<Workflow> workflows = readWorkflowFiles(inputFiles);

    boolean generateGraphviz = parsed.hasOption("g");
    String graphvizFormat = parsed.getOptionValue("g", "svg");

    OozieWorkflowGenerator generator = new OozieWorkflowGenerator(merged);
    generator.generateOozieWorkflows(outputDir, workflows, generateGraphviz, graphvizFormat);
}

From source file:com.pinterest.secor.main.LogFilePrinterMain.java

public static void main(String[] args) {
    try {//from   www  .  j  a va 2 s  .  c om
        CommandLine commandLine = parseArgs(args);
        SecorConfig config = SecorConfig.load();
        FileUtil.configure(config);
        LogFilePrinter printer = new LogFilePrinter(commandLine.hasOption("print_offsets_only"));
        printer.printFile(commandLine.getOptionValue("file"));
    } catch (Throwable t) {
        LOG.error("Log file printer failed", t);
        System.exit(1);
    }
}

From source file:com.knewton.mapreduce.example.SSTableMRExample.java

public static void main(String[] args)
        throws IOException, InterruptedException, ClassNotFoundException, URISyntaxException, ParseException {

    long startTime = System.currentTimeMillis();
    Options options = buildOptions();/*from  w  w w.  j  ava 2 s.  com*/

    CommandLineParser cliParser = new BasicParser();
    CommandLine cli = cliParser.parse(options, args);
    if (cli.getArgs().length < 2 || cli.hasOption('h')) {
        printUsage(options);
    }
    Job job = getJobConf(cli);

    job.setJarByClass(SSTableMRExample.class);
    job.setOutputKeyClass(LongWritable.class);
    job.setOutputValueClass(StudentEventWritable.class);

    job.setMapperClass(StudentEventMapper.class);
    job.setReducerClass(StudentEventReducer.class);

    job.setInputFormatClass(SSTableColumnInputFormat.class);
    job.setOutputFormatClass(TextOutputFormat.class);
    // input arg
    String inputPaths = cli.getArgs()[0];
    LOG.info("Setting initial input paths to {}", inputPaths);
    SSTableInputFormat.addInputPaths(job, inputPaths);
    // output arg
    FileOutputFormat.setOutputPath(job, new Path(cli.getArgs()[1]));
    if (cli.hasOption('c')) {
        LOG.info("Using compression for output.");
        FileOutputFormat.setOutputCompressorClass(job, GzipCodec.class);
        FileOutputFormat.setCompressOutput(job, true);
    }
    job.waitForCompletion(true);
    LOG.info("Total runtime: {}s", (System.currentTimeMillis() - startTime) / 1000);
}

From source file:jlite.cli.ProxyDelegate.java

public static void main(String[] args) {
    System.out.println(); // extra line
    CommandLineParser parser = new GnuParser();
    Options options = setupOptions();/*  w w  w  .  j  ava  2s . c  om*/
    HelpFormatter helpFormatter = new HelpFormatter();
    helpFormatter.setSyntaxPrefix("Usage: ");
    CommandLine line = null;
    try {
        line = parser.parse(options, args);
        if (line.hasOption("help")) {
            helpFormatter.printHelp(100, COMMAND, "\noptions:", options, "\n" + CLI.FOOTER, false);
            System.out.println(); // extra line
            System.exit(0);
        }
        if (line.hasOption("xml")) {
            System.out.println("<output>");
        }
        run(line);
    } catch (ParseException e) {
        System.err.println(e.getMessage() + "\n");
        helpFormatter.printHelp(100, COMMAND, "\noptions:", options, "\n" + CLI.FOOTER, false);
        System.out.println(); // extra line
        System.exit(-1);
    } catch (Exception e) {
        if ((line != null) && (line.hasOption("xml"))) {
            System.out.println("<error>" + e.getMessage() + "</error>");
        } else {
            System.err.println(e.getMessage());
        }
    } finally {
        if (line.hasOption("xml")) {
            System.out.println("</output>");
        }
    }
    System.out.println(); // extra line
}

From source file:fr.liglab.consgap.Main.java

public static void main(String[] args) throws IOException {
    Options options = new Options();
    CommandLineParser parser = new PosixParser();

    options.addOption("s", false, "Sparse: use int lists instead of bitsets to represent positions");
    options.addOption("b", false, "Benchmark mode : sequences are not outputted at all");
    options.addOption("h", false, "Show help");
    options.addOption("w", false,
            "Use breadth first exploration instead of depth first. Usually less efficient.");
    options.addOption("t", true,
            "How many threads will be launched (defaults to your machine's processors count)");
    options.addOption("l", false, "Use lcm style, read dataset to generate candidates");
    options.addOption("f", true,
            "Sequences filtering frequency, expressed in number of outputs. Recommended value is 100, avoids some redundant explorations.");
    options.addOption("sep", true, "separator in the dataset files (defaults to tabulation)");
    try {/*from w w w  . j  a v  a  2 s .  co m*/
        CommandLine cmd = parser.parse(options, args);

        if (cmd.getArgs().length != 5 || cmd.hasOption('h')) {
            printMan(options);
        } else {
            standalone(cmd);
        }
    } catch (ParseException e) {
        printMan(options);
    }
}

From source file:br.usp.poli.lta.cereda.wsn2spa.Main.java

public static void main(String[] args) {

    Utils.printBanner();/*  w w  w  .j av  a2 s.  com*/
    CommandLineParser parser = new DefaultParser();

    try {

        CommandLine line = parser.parse(Utils.getOptions(), args);

        if (line.hasOption("g")) {
            System.out.println("Flag '-g' found, overriding other flags.");
            System.out.println("Please, wait...");
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (Exception nothandled) {
            }
            SwingUtilities.invokeLater(() -> {
                Editor e = new Editor();
                e.setVisible(true);
            });
        } else {
            enableCLI(line);
        }
    } catch (ParseException nothandled) {
        Utils.printHelp();
    } catch (Exception exception) {
        Utils.printException(exception);
    }
}

From source file:cnxchecker.Server.java

public static void main(String[] args) {
    Options options = new Options();
    options.addOption("p", "port", true, "TCP port to bind to (random by default)");
    options.addOption("h", "help", false, "Print help");

    CommandLineParser parser = new GnuParser();
    try {//from   w  w  w. j av a  2  s  .co m
        CommandLine cmd = parser.parse(options, args);

        if (cmd.hasOption("h")) {
            HelpFormatter hf = new HelpFormatter();
            hf.printHelp("server", options);
            System.exit(0);
        }

        int port = 0;
        if (cmd.hasOption("p")) {
            try {
                port = Integer.parseInt(cmd.getOptionValue("p"));
            } catch (NumberFormatException e) {
                printAndExit("Invalid port number " + cmd.getOptionValue("p"));
            }

            if (port < 0 || port > 65535) {
                printAndExit("Invalid port number " + port);
            }
        }

        Server server = new Server(port);
        server.doit();
    } catch (ParseException e) {
        printAndExit("Failed to parse options: " + e.getMessage());
    }
}

From source file:com.mvdb.etl.actions.FetchConfigValue.java

/**
 * @param args/*  www  .  j a  v  a  2 s  .c om*/
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {
    String customerName = null;
    String valueName = null;
    String outputFileName = null;

    ActionUtils.setUpInitFileProperty();

    final CommandLineParser cmdLinePosixParser = new PosixParser();
    final Options posixOptions = constructPosixOptions();
    CommandLine commandLine;
    try {
        commandLine = cmdLinePosixParser.parse(posixOptions, args);
        if (commandLine.hasOption("customer")) {
            customerName = commandLine.getOptionValue("customer");
        }
        if (commandLine.hasOption("name")) {
            valueName = commandLine.getOptionValue("name");
        }
        if (commandLine.hasOption("outputFile")) {
            outputFileName = commandLine.getOptionValue("outputFile");
        }

        System.out.println("customerName:" + customerName);
        System.out.println("valueName:" + valueName);
        System.out.println("outputFileName:" + outputFileName);

    } catch (ParseException parseException) // checked exception
    {
        System.err.println(
                "Encountered exception while parsing using PosixParser:\n" + parseException.getMessage());
    }

    if (customerName == null) {
        System.err.println("Could not find customerName. Aborting...");
        System.exit(1);
    }

    if (valueName == null) {
        System.err.println("Could not find valueName. Aborting...");
        System.exit(1);
    }

    if (outputFileName == null) {
        System.err.println("Could not find outputFileName. Aborting...");
        System.exit(1);
    }

    String value = ActionUtils.getConfigurationValue(customerName, valueName);
    FileUtils.writeStringToFile(new File(outputFileName), value);

}