Example usage for org.apache.commons.cli OptionBuilder withArgName

List of usage examples for org.apache.commons.cli OptionBuilder withArgName

Introduction

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

Prototype

public static OptionBuilder withArgName(String name) 

Source Link

Document

The next Option created will have the specified argument value name.

Usage

From source file:cc.wikitools.lucene.hadoop.SearchWikipediaHdfs.java

@SuppressWarnings("static-access")
@Override// w w w. jav a2  s  .  c  om
public int run(String[] args) throws Exception {
    Options options = new Options();
    options.addOption(
            OptionBuilder.withArgName("path").hasArg().withDescription("index location").create(INDEX_OPTION));
    options.addOption(
            OptionBuilder.withArgName("string").hasArg().withDescription("query text").create(QUERY_OPTION));
    options.addOption(OptionBuilder.withArgName("num").hasArg().withDescription("number of results to return")
            .create(NUM_RESULTS_OPTION));

    options.addOption(new Option(VERBOSE_OPTION, "print out complete document"));
    options.addOption(new Option(TITLE_OPTION, "search title"));
    options.addOption(new Option(ARTICLE_OPTION, "search article"));

    CommandLine cmdline = null;
    CommandLineParser parser = new GnuParser();
    try {
        cmdline = parser.parse(options, args);
    } catch (ParseException exp) {
        System.err.println("Error parsing command line: " + exp.getMessage());
        System.exit(-1);
    }

    if (!cmdline.hasOption(QUERY_OPTION) || !cmdline.hasOption(INDEX_OPTION)
            || !cmdline.hasOption(QUERY_OPTION)) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp(SearchWikipediaHdfs.class.getName(), options);
        System.exit(-1);
    }

    String indexLocation = cmdline.getOptionValue(INDEX_OPTION);
    String queryText = cmdline.getOptionValue(QUERY_OPTION);
    int numResults = cmdline.hasOption(NUM_RESULTS_OPTION)
            ? Integer.parseInt(cmdline.getOptionValue(NUM_RESULTS_OPTION))
            : DEFAULT_NUM_RESULTS;
    boolean verbose = cmdline.hasOption(VERBOSE_OPTION);
    boolean searchArticle = !cmdline.hasOption(TITLE_OPTION);

    PrintStream out = new PrintStream(System.out, true, "UTF-8");

    HdfsWikipediaSearcher searcher = new HdfsWikipediaSearcher(new Path(indexLocation), getConf());
    TopDocs rs = null;
    if (searchArticle) {
        rs = searcher.searchArticle(queryText, numResults);
    } else {
        rs = searcher.searchTitle(queryText, numResults);
    }

    int i = 1;
    for (ScoreDoc scoreDoc : rs.scoreDocs) {
        Document hit = searcher.doc(scoreDoc.doc);

        out.println(String.format("%d. %s (id = %s) %f", i, hit.getField(IndexField.TITLE.name).stringValue(),
                hit.getField(IndexField.ID.name).stringValue(), scoreDoc.score));
        if (verbose) {
            out.println("# " + hit.toString().replaceAll("[\\n\\r]+", " "));
        }
        i++;
    }

    searcher.close();
    out.close();

    return 0;
}

From source file:eu.stratosphere.myriad.driver.MyriadDriverFrontend.java

public MyriadDriverFrontend() {
    // dgen-install-dir
    this.options = new Options();

    // scaling-factor
    OptionBuilder.hasArg();//w  w  w . j av  a2s  . c o  m
    OptionBuilder.withArgName("double");
    OptionBuilder.withDescription("scaling factor (s=1 generates 1GB)");
    OptionBuilder.withLongOpt("scaling-factor");
    this.options.addOption(OptionBuilder.create('s'));

    // dataset-id
    OptionBuilder.hasArg();
    OptionBuilder.withArgName("string");
    OptionBuilder.withDescription("ID of the generated Myriad dataset");
    OptionBuilder.withLongOpt("dataset-id");
    this.options.addOption(OptionBuilder.create('m'));

    // node-count
    OptionBuilder.hasArg();
    OptionBuilder.withArgName("int");
    OptionBuilder.withDescription("degree of parallelism (i.e. total number of partitions)");
    OptionBuilder.withArgName("node-count");
    this.options.addOption(OptionBuilder.create('N'));

    // output-base
    OptionBuilder.hasArg();
    OptionBuilder.withArgName("path");
    OptionBuilder.withDescription("base path for writing the output");
    OptionBuilder.withLongOpt("output-base");
    this.options.addOption(OptionBuilder.create('o'));

    // execute-stages
    OptionBuilder.hasArgs();
    OptionBuilder.withArgName("stagename");
    OptionBuilder.withDescription("specify specific stages to be executed");
    OptionBuilder.withLongOpt("execute-stage");
    this.options.addOption(OptionBuilder.create('x'));
}

From source file:com.example.geomesa.cassandra.CassandraQuickStart.java

/**
 * Creates a common set of command-line options for the parser.  Each option
 * is described separately.// w  w w.  j a  va 2  s.  co m
 */
static Options getCommonRequiredOptions() {
    Options options = new Options();

    Option contactPTOpt = OptionBuilder.withArgName(CONTACT_POINT).hasArg().isRequired()
            .withDescription("HOST:PORT to Cassandra").create(CONTACT_POINT);
    options.addOption(contactPTOpt);

    Option keySpaceOpt = OptionBuilder.withArgName(KEY_SPACE).hasArg().isRequired()
            .withDescription("Cassandra Keyspace").create(KEY_SPACE);
    options.addOption(keySpaceOpt);

    Option catalogOpt = OptionBuilder.withArgName(CATALOG).hasArg()
            .withDescription("Name of GeoMesa catalog table").create(CATALOG);
    options.addOption(catalogOpt);

    Option userOpt = OptionBuilder.withArgName(USER).hasArg().withDescription("Username to connect with")
            .create(USER);
    options.addOption(userOpt);

    Option passwordOpt = OptionBuilder.withArgName(PASSWORD).hasArg()
            .withDescription("Password to connect with").create(PASSWORD);
    options.addOption(passwordOpt);

    return options;
}

From source file:DumpRecordsExtended.java

/**
 * Runs this tool.//ww w .ja  v a2s . com
 */
@SuppressWarnings({ "static-access" })
public int run(String[] args) throws Exception {
    Options options = new Options();

    options.addOption(OptionBuilder.withArgName("path").hasArg().withDescription("input path").create(INPUT));
    options.addOption(OptionBuilder.withArgName("path").hasArg().withDescription("output path").create(OUTPUT));

    CommandLine cmdline;
    CommandLineParser parser = new GnuParser();

    try {
        cmdline = parser.parse(options, args);
    } catch (ParseException exp) {
        System.err.println("Error parsing command line: " + exp.getMessage());
        return -1;
    }

    if (!cmdline.hasOption(INPUT) || !cmdline.hasOption(OUTPUT)) {
        System.out.println("args: " + Arrays.toString(args));
        HelpFormatter formatter = new HelpFormatter();
        formatter.setWidth(120);
        formatter.printHelp(this.getClass().getName(), options);
        ToolRunner.printGenericCommandUsage(System.out);
        return -1;
    }

    String inputPath = cmdline.getOptionValue(INPUT);
    String outputPath = cmdline.getOptionValue(OUTPUT);

    LOG.info("Tool name: " + DumpRecordsExtended.class.getSimpleName());
    LOG.info(" - input: " + inputPath);
    LOG.info(" - output: " + outputPath);

    Configuration conf = new Configuration();
    conf.setInt("mapred.min.split.size", 1024 * 1024 * 1024);

    Job job = Job.getInstance(conf);
    job.setJobName(DumpRecordsExtended.class.getSimpleName());
    job.setJarByClass(DumpRecordsExtended.class);

    job.setNumReduceTasks(0);

    FileInputFormat.addInputPath(job, new Path(inputPath));
    FileOutputFormat.setOutputPath(job, new Path(outputPath));

    job.setInputFormatClass(SequenceFileInputFormat.class);
    job.setOutputFormatClass(TextOutputFormat.class);

    job.setMapOutputKeyClass(IntWritable.class);
    job.setMapOutputValueClass(PageRankNode.class);

    // Delete the output directory if it exists already.
    FileSystem.get(conf).delete(new Path(outputPath), true);

    job.waitForCompletion(true);

    return 0;
}

From source file:cc.slda.DisplayTopic.java

@SuppressWarnings("unchecked")
public int run(String[] args) throws Exception {
    Options options = new Options();

    options.addOption(Settings.HELP_OPTION, false, "print the help message");
    options.addOption(OptionBuilder.withArgName(Settings.PATH_INDICATOR).hasArg()
            .withDescription("input beta file").create(Settings.INPUT_OPTION));
    options.addOption(OptionBuilder.withArgName(Settings.PATH_INDICATOR).hasArg()
            .withDescription("term index file").create(ParseCorpus.INDEX));
    options.addOption(OptionBuilder.withArgName(Settings.INTEGER_INDICATOR).hasArg()
            .withDescription("display top terms only (default - 10)").create(TOP_DISPLAY_OPTION));

    String betaString = null;//w w  w  .  ja v a 2s . c o m
    String indexString = null;
    int topDisplay = TOP_DISPLAY;

    CommandLineParser parser = new GnuParser();
    HelpFormatter formatter = new HelpFormatter();
    try {
        CommandLine line = parser.parse(options, args);

        if (line.hasOption(Settings.HELP_OPTION)) {
            formatter.printHelp(ParseCorpus.class.getName(), options);
            System.exit(0);
        }

        if (line.hasOption(Settings.INPUT_OPTION)) {
            betaString = line.getOptionValue(Settings.INPUT_OPTION);
        } else {
            throw new ParseException("Parsing failed due to " + Settings.INPUT_OPTION + " not initialized...");
        }

        if (line.hasOption(ParseCorpus.INDEX)) {
            indexString = line.getOptionValue(ParseCorpus.INDEX);
        } else {
            throw new ParseException("Parsing failed due to " + ParseCorpus.INDEX + " not initialized...");
        }

        if (line.hasOption(TOP_DISPLAY_OPTION)) {
            topDisplay = Integer.parseInt(line.getOptionValue(TOP_DISPLAY_OPTION));
        }
    } catch (ParseException pe) {
        System.err.println(pe.getMessage());
        formatter.printHelp(ParseCorpus.class.getName(), options);
        System.exit(0);
    } catch (NumberFormatException nfe) {
        System.err.println(nfe.getMessage());
        System.exit(0);
    }

    JobConf conf = new JobConf(DisplayTopic.class);
    FileSystem fs = FileSystem.get(conf);

    Path indexPath = new Path(indexString);
    Preconditions.checkArgument(fs.exists(indexPath) && fs.isFile(indexPath), "Invalid index path...");

    Path betaPath = new Path(betaString);
    Preconditions.checkArgument(fs.exists(betaPath) && fs.isFile(betaPath), "Invalid beta path...");

    SequenceFile.Reader sequenceFileReader = null;
    try {
        IntWritable intWritable = new IntWritable();
        Text text = new Text();
        Map<Integer, String> termIndex = new HashMap<Integer, String>();
        sequenceFileReader = new SequenceFile.Reader(fs, indexPath, conf);
        while (sequenceFileReader.next(intWritable, text)) {
            termIndex.put(intWritable.get(), text.toString());
        }

        PairOfIntFloat pairOfIntFloat = new PairOfIntFloat();
        // HMapIFW hmap = new HMapIFW();
        HMapIDW hmap = new HMapIDW();
        TreeMap<Double, Integer> treeMap = new TreeMap<Double, Integer>();
        sequenceFileReader = new SequenceFile.Reader(fs, betaPath, conf);
        while (sequenceFileReader.next(pairOfIntFloat, hmap)) {
            treeMap.clear();

            System.out.println("==============================");
            System.out.println(
                    "Top ranked " + topDisplay + " terms for Topic " + pairOfIntFloat.getLeftElement());
            System.out.println("==============================");

            Iterator<Integer> itr1 = hmap.keySet().iterator();
            int temp1 = 0;
            while (itr1.hasNext()) {
                temp1 = itr1.next();
                treeMap.put(-hmap.get(temp1), temp1);
                if (treeMap.size() > topDisplay) {
                    treeMap.remove(treeMap.lastKey());
                }
            }

            Iterator<Double> itr2 = treeMap.keySet().iterator();
            double temp2 = 0;
            while (itr2.hasNext()) {
                temp2 = itr2.next();
                if (termIndex.containsKey(treeMap.get(temp2))) {
                    System.out.println(termIndex.get(treeMap.get(temp2)) + "\t\t" + -temp2);
                } else {
                    System.out.println("How embarrassing! Term index not found...");
                }
            }
        }
    } finally {
        IOUtils.closeStream(sequenceFileReader);
    }

    return 0;
}

From source file:DumpPageRankRecordsToPlainText.java

/**
 * Runs this tool./*from  w  w  w . ja v a2 s.  co  m*/
 */
@SuppressWarnings({ "static-access" })
public int run(String[] args) throws Exception {
    Options options = new Options();

    options.addOption(OptionBuilder.withArgName("path").hasArg().withDescription("input path").create(INPUT));
    options.addOption(OptionBuilder.withArgName("path").hasArg().withDescription("output path").create(OUTPUT));

    CommandLine cmdline;
    CommandLineParser parser = new GnuParser();

    try {
        cmdline = parser.parse(options, args);
    } catch (ParseException exp) {
        System.err.println("Error parsing command line: " + exp.getMessage());
        return -1;
    }

    if (!cmdline.hasOption(INPUT) || !cmdline.hasOption(OUTPUT)) {
        System.out.println("args: " + Arrays.toString(args));
        HelpFormatter formatter = new HelpFormatter();
        formatter.setWidth(120);
        formatter.printHelp(this.getClass().getName(), options);
        ToolRunner.printGenericCommandUsage(System.out);
        return -1;
    }

    String inputPath = cmdline.getOptionValue(INPUT);
    String outputPath = cmdline.getOptionValue(OUTPUT);

    LOG.info("Tool name: " + DumpPageRankRecordsToPlainText.class.getSimpleName());
    LOG.info(" - input: " + inputPath);
    LOG.info(" - output: " + outputPath);

    Configuration conf = new Configuration();
    conf.setInt("mapred.min.split.size", 1024 * 1024 * 1024);

    Job job = Job.getInstance(conf);
    job.setJobName(DumpPageRankRecordsToPlainText.class.getSimpleName());
    job.setJarByClass(DumpPageRankRecordsToPlainText.class);

    job.setNumReduceTasks(0);

    FileInputFormat.addInputPath(job, new Path(inputPath));
    FileOutputFormat.setOutputPath(job, new Path(outputPath));

    job.setInputFormatClass(SequenceFileInputFormat.class);
    job.setOutputFormatClass(TextOutputFormat.class);

    job.setMapOutputKeyClass(IntWritable.class);
    job.setMapOutputValueClass(PageRankNode.class);

    // Delete the output directory if it exists already.
    FileSystem.get(conf).delete(new Path(outputPath), true);

    job.waitForCompletion(true);

    return 0;
}

From source file:com.hortonworks.pso.data.generator.mapreduce.DataGenTool.java

private void buildOptions() {
    options = new Options();
    Option help = OptionBuilder.withDescription("This help").create("help");
    Option mappers = OptionBuilder.withArgName("mappers").hasArg().withDescription("parallelism")
            .create("mappers");
    Option sink = OptionBuilder.withArgName("sink").hasArg()
            .withDescription("Target Sink: (HDFS|KAFKA) default-HDFS").create("sink");
    Option outputDir = OptionBuilder.withArgName("output").hasArg().withDescription(
            "Sink output information. HDFS-Output Directory or Kafka-URL: kafka://<kafka host>/<topic> OR kafka://kafka-server:9000,kafka-server2:9000/foobar")
            .create("output");
    Option config = OptionBuilder.withArgName("json config").hasArg().withDescription("control file")
            .create("jsonCfg");
    Option count = OptionBuilder.withArgName("count").hasArg().withDescription("total record count")
            .create("count");
    options.addOption(help);//from  w  w w.  ja  v  a2  s .  c o m
    options.addOption(mappers);
    options.addOption(sink);
    options.addOption(outputDir);
    options.addOption(config);
    options.addOption(count);
}

From source file:ch.zhaw.iamp.rct.App.java

static void configureCommandLineInterface() {
    cliOptions = new Options();
    cliOptions.addOption(GRAMMAR_OPTIONS[0], GRAMMAR_OPTIONS[1], false, GRAMMAR_OPTIONS[2]);
    cliOptions.addOption(DEFINITION_OPTIONS[0], DEFINITION_OPTIONS[1], true, DEFINITION_OPTIONS[2]);
    cliOptions.addOption(INIT_OPTIONS[0], INIT_OPTIONS[1], true, INIT_OPTIONS[2]);
    cliOptions.addOption(OUTPUT_OPTIONS[0], OUTPUT_OPTIONS[1], true, OUTPUT_OPTIONS[2]);
    Option weights = OptionBuilder.withArgName(WEIGHTS_OPTIONS[0]).withLongOpt(WEIGHTS_OPTIONS[1])
            .withDescription(WEIGHTS_OPTIONS[2]).hasArgs(3).create(WEIGHTS_OPTIONS[0]);
    cliOptions.addOption(weights);/*from   w  w  w .j  a  v  a2  s.c  o  m*/
}

From source file:com.mosso.client.cloudfiles.sample.FilesMakeContainer.java

@SuppressWarnings("static-access")
private static Options addCommandLineOptions() {
    Option help = new Option("help", "print this message");

    Option container = OptionBuilder.withArgName("container").hasArg(true)
            .withDescription("Name of container to create.").create("container");

    Options options = new Options();

    options.addOption(help);//from   w ww .j a  v  a  2s . c om
    options.addOption(container);

    return options;
}

From source file:edu.umd.shrawanraina.HBaseWordCountFetch.java

/**
 * Runs this tool.//from  w  ww  .j  a  va  2s  .  c  o  m
 */
@SuppressWarnings({ "static-access" })
public int run(String[] args) throws Exception {
    Options options = new Options();

    options.addOption(
            OptionBuilder.withArgName("table").hasArg().withDescription("HBase table name").create(TABLE));
    options.addOption(
            OptionBuilder.withArgName("word").hasArg().withDescription("word to look up").create(WORD));

    CommandLine cmdline;
    CommandLineParser parser = new GnuParser();

    try {
        cmdline = parser.parse(options, args);
    } catch (ParseException exp) {
        System.err.println("Error parsing command line: " + exp.getMessage());
        return -1;
    }

    if (!cmdline.hasOption(TABLE) || !cmdline.hasOption(WORD)) {
        System.out.println("args: " + Arrays.toString(args));
        HelpFormatter formatter = new HelpFormatter();
        formatter.setWidth(120);
        formatter.printHelp(this.getClass().getName(), options);
        ToolRunner.printGenericCommandUsage(System.out);
        return -1;
    }

    String tableName = cmdline.getOptionValue(TABLE);
    String word = cmdline.getOptionValue(WORD);

    Configuration conf = getConf();
    conf.addResource(new Path("/etc/hbase/conf/hbase-site.xml"));

    Configuration hbaseConfig = HBaseConfiguration.create(conf);
    HConnection hbaseConnection = HConnectionManager.createConnection(hbaseConfig);
    HTableInterface table = hbaseConnection.getTable(tableName);

    Get get = new Get(Bytes.toBytes(word));
    Result result = table.get(get);

    int count = Bytes.toInt(result.getValue(HBaseWordCount.CF, HBaseWordCount.COUNT));

    LOG.info("word: " + word + ", count: " + count);
    LOG.info("word: " + word + ", result: " + result.getValue(HBaseWordCount.CF, HBaseWordCount.COUNT));
    return 0;
}