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

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

Introduction

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

Prototype

public static OptionBuilder hasArg() 

Source Link

Document

The next Option created will require an argument value.

Usage

From source file:org.apache.nutch.scoring.webgraph.NodeDumper.java

/**
 * Runs the node dumper tool.//from   w ww. ja v  a  2  s.  c  om
 */
public int run(String[] args) throws Exception {

    Options options = new Options();
    OptionBuilder.withArgName("help");
    OptionBuilder.withDescription("show this help message");
    Option helpOpts = OptionBuilder.create("help");
    options.addOption(helpOpts);

    OptionBuilder.withArgName("webgraphdb");
    OptionBuilder.hasArg();
    OptionBuilder.withDescription("the web graph database to use");
    Option webGraphDbOpts = OptionBuilder.create("webgraphdb");
    options.addOption(webGraphDbOpts);

    OptionBuilder.withArgName("inlinks");
    OptionBuilder.withDescription("show highest inlinks");
    Option inlinkOpts = OptionBuilder.create("inlinks");
    options.addOption(inlinkOpts);

    OptionBuilder.withArgName("outlinks");
    OptionBuilder.withDescription("show highest outlinks");
    Option outlinkOpts = OptionBuilder.create("outlinks");
    options.addOption(outlinkOpts);

    OptionBuilder.withArgName("scores");
    OptionBuilder.withDescription("show highest scores");
    Option scoreOpts = OptionBuilder.create("scores");
    options.addOption(scoreOpts);

    OptionBuilder.withArgName("topn");
    OptionBuilder.hasOptionalArg();
    OptionBuilder.withDescription("show topN scores");
    Option topNOpts = OptionBuilder.create("topn");
    options.addOption(topNOpts);

    OptionBuilder.withArgName("output");
    OptionBuilder.hasArg();
    OptionBuilder.withDescription("the output directory to use");
    Option outputOpts = OptionBuilder.create("output");
    options.addOption(outputOpts);

    OptionBuilder.withArgName("asEff");
    OptionBuilder.withDescription("Solr ExternalFileField compatible output format");
    Option effOpts = OptionBuilder.create("asEff");
    options.addOption(effOpts);

    OptionBuilder.hasArgs(2);
    OptionBuilder.withDescription("group <host|domain> <sum|max>");
    Option groupOpts = OptionBuilder.create("group");
    options.addOption(groupOpts);

    OptionBuilder.withArgName("asSequenceFile");
    OptionBuilder.withDescription("whether to output as a sequencefile");
    Option sequenceFileOpts = OptionBuilder.create("asSequenceFile");
    options.addOption(sequenceFileOpts);

    CommandLineParser parser = new GnuParser();
    try {

        CommandLine line = parser.parse(options, args);
        if (line.hasOption("help") || !line.hasOption("webgraphdb")) {
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp("NodeDumper", options);
            return -1;
        }

        String webGraphDb = line.getOptionValue("webgraphdb");
        boolean inlinks = line.hasOption("inlinks");
        boolean outlinks = line.hasOption("outlinks");

        long topN = (line.hasOption("topn") ? Long.parseLong(line.getOptionValue("topn")) : Long.MAX_VALUE);

        // get the correct dump type
        String output = line.getOptionValue("output");
        DumpType type = (inlinks ? DumpType.INLINKS : outlinks ? DumpType.OUTLINKS : DumpType.SCORES);

        NameType nameType = null;
        AggrType aggrType = null;
        String[] group = line.getOptionValues("group");
        if (group != null && group.length == 2) {
            nameType = (group[0].equals("host") ? NameType.HOST
                    : group[0].equals("domain") ? NameType.DOMAIN : null);
            aggrType = (group[1].equals("sum") ? AggrType.SUM : group[1].equals("sum") ? AggrType.MAX : null);
        }

        // Use ExternalFileField?
        boolean asEff = line.hasOption("asEff");
        boolean asSequenceFile = line.hasOption("asSequenceFile");

        dumpNodes(new Path(webGraphDb), type, topN, new Path(output), asEff, nameType, aggrType,
                asSequenceFile);
        return 0;
    } catch (Exception e) {
        LOG.error("NodeDumper: " + StringUtils.stringifyException(e));
        return -2;
    }
}

From source file:org.apache.nutch.scoring.webgraph.NodeReader.java

/**
 * Runs the NodeReader tool. The command line arguments must contain a
 * webgraphdb path and a url. The url must match the normalized url that is
 * contained in the NodeDb of the WebGraph.
 *///from  ww  w .j a v  a  2s. co m
public static void main(String[] args) throws Exception {

    Options options = new Options();
    OptionBuilder.withArgName("help");
    OptionBuilder.withDescription("show this help message");
    Option helpOpts = OptionBuilder.create("help");
    options.addOption(helpOpts);

    OptionBuilder.withArgName("webgraphdb");
    OptionBuilder.hasArg();
    OptionBuilder.withDescription("the webgraphdb to use");
    Option webGraphOpts = OptionBuilder.create("webgraphdb");
    options.addOption(webGraphOpts);

    OptionBuilder.withArgName("url");
    OptionBuilder.hasOptionalArg();
    OptionBuilder.withDescription("the url to dump");
    Option urlOpts = OptionBuilder.create("url");
    options.addOption(urlOpts);

    CommandLineParser parser = new GnuParser();
    try {

        // command line must take a webgraphdb and a url
        CommandLine line = parser.parse(options, args);
        if (line.hasOption("help") || !line.hasOption("webgraphdb") || !line.hasOption("url")) {
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp("WebGraphReader", options);
            return;
        }

        // dump the values to system out and return
        String webGraphDb = line.getOptionValue("webgraphdb");
        String url = line.getOptionValue("url");
        NodeReader reader = new NodeReader(NutchConfiguration.create());
        reader.dumpUrl(new Path(webGraphDb), url);

        return;
    } catch (Exception e) {
        e.printStackTrace();
        return;
    }
}

From source file:org.apache.nutch.scoring.webgraph.ScoreUpdater.java

/**
 * Runs the ScoreUpdater tool./*ww w. j  a v  a  2 s. c  o  m*/
 */
public int run(String[] args) throws Exception {

    Options options = new Options();
    OptionBuilder.withArgName("help");
    OptionBuilder.withDescription("show this help message");
    Option helpOpts = OptionBuilder.create("help");
    options.addOption(helpOpts);

    OptionBuilder.withArgName("crawldb");
    OptionBuilder.hasArg();
    OptionBuilder.withDescription("the crawldb to use");
    Option crawlDbOpts = OptionBuilder.create("crawldb");
    options.addOption(crawlDbOpts);

    OptionBuilder.withArgName("webgraphdb");
    OptionBuilder.hasArg();
    OptionBuilder.withDescription("the webgraphdb to use");
    Option webGraphOpts = OptionBuilder.create("webgraphdb");
    options.addOption(webGraphOpts);

    CommandLineParser parser = new GnuParser();
    try {

        CommandLine line = parser.parse(options, args);
        if (line.hasOption("help") || !line.hasOption("webgraphdb") || !line.hasOption("crawldb")) {
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp("ScoreUpdater", options);
            return -1;
        }

        String crawlDb = line.getOptionValue("crawldb");
        String webGraphDb = line.getOptionValue("webgraphdb");
        update(new Path(crawlDb), new Path(webGraphDb));
        return 0;
    } catch (Exception e) {
        LOG.error("ScoreUpdater: " + StringUtils.stringifyException(e));
        return -1;
    }
}

From source file:org.apache.nutch.tools.ResolveUrls.java

/**
 * Runs the resolve urls tool.//  www .  j a  v a 2s.  c  om
 */
public static void main(String[] args) {

    Options options = new Options();
    OptionBuilder.withArgName("help");
    OptionBuilder.withDescription("show this help message");
    Option helpOpts = OptionBuilder.create("help");
    options.addOption(helpOpts);

    OptionBuilder.withArgName("urls");
    OptionBuilder.hasArg();
    OptionBuilder.withDescription("the urls file to check");
    Option urlOpts = OptionBuilder.create("urls");
    options.addOption(urlOpts);

    OptionBuilder.withArgName("numThreads");
    OptionBuilder.hasArgs();
    OptionBuilder.withDescription("the number of threads to use");
    Option numThreadOpts = OptionBuilder.create("numThreads");
    options.addOption(numThreadOpts);

    CommandLineParser parser = new GnuParser();
    try {
        // parse out common line arguments
        CommandLine line = parser.parse(options, args);
        if (line.hasOption("help") || !line.hasOption("urls")) {
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp("ResolveUrls", options);
            return;
        }

        // get the urls and the number of threads and start the resolver
        String urls = line.getOptionValue("urls");
        int numThreads = 100;
        String numThreadsStr = line.getOptionValue("numThreads");
        if (numThreadsStr != null) {
            numThreads = Integer.parseInt(numThreadsStr);
        }
        ResolveUrls resolve = new ResolveUrls(urls, numThreads);
        resolve.resolveUrls();
    } catch (Exception e) {
        LOG.error("ResolveUrls: " + StringUtils.stringifyException(e));
    }
}

From source file:org.apache.river.container.admin.app.CommandLineParserTest.java

@Test
public void testParser() throws Exception {
    Options options = new Options();
    Option profile = OptionBuilder.hasArg().withDescription("Profile name to apply this command to")
            .create("p");
    options.addOption(profile);//from   ww  w  . j  a v a2s .  c  om

    String[] args = new String[] { "list", "-p", "admin" };

    CommandLineParser parser = new PosixParser();
    CommandLine cmd = parser.parse(options, args);
    assertTrue(cmd.hasOption("p"));
    assertEquals("Profile value", "admin", cmd.getOptionValue("p"));

    String[] remainingArgs = cmd.getArgs();
    assertEquals("number of remaining arguments", 1, remainingArgs.length);
    assertEquals("Remaining arg", "list", remainingArgs[0]);

}

From source file:org.apache.sqoop.shell.SetOptionFunction.java

@SuppressWarnings("static-access")
public SetOptionFunction() {
    this.addOption(OptionBuilder.hasArg().withDescription(resourceString(Constants.RES_SET_PROMPT_OPT_NAME))
            .withLongOpt(Constants.OPT_NAME).isRequired().create(Constants.OPT_NAME_CHAR));
    this.addOption(OptionBuilder.hasArg().withDescription(resourceString(Constants.RES_SET_PROMPT_OPT_VALUE))
            .withLongOpt(Constants.OPT_VALUE).isRequired().create(Constants.OPT_VALUE_CHAR));
}

From source file:org.apache.sqoop.shell.SetServerFunction.java

@SuppressWarnings("static-access")
public SetServerFunction() {
    this.addOption(OptionBuilder.hasArg().withArgName(Constants.OPT_HOST)
            .withDescription(resourceString(Constants.RES_SET_HOST_DESCRIPTION)).withLongOpt(Constants.OPT_HOST)
            .create(Constants.OPT_HOST_CHAR));
    this.addOption(OptionBuilder.hasArg().withArgName(Constants.OPT_PORT)
            .withDescription(resourceString(Constants.RES_SET_PORT_DESCRIPTION)).withLongOpt(Constants.OPT_PORT)
            .create(Constants.OPT_PORT_CHAR));
    this.addOption(OptionBuilder.hasArg().withArgName(Constants.OPT_WEBAPP)
            .withDescription(resourceString(Constants.RES_WEBAPP_DESCRIPTION)).withLongOpt(Constants.OPT_WEBAPP)
            .create(Constants.OPT_WEBAPP_CHAR));
    this.addOption(OptionBuilder.hasArg().withArgName(Constants.OPT_URL)
            .withDescription(resourceString(Constants.RES_URL_DESCRIPTION)).withLongOpt(Constants.OPT_URL)
            .create(Constants.OPT_URL_CHAR));
}

From source file:org.apache.sqoop.shell.ShowConnectorFunction.java

@SuppressWarnings("static-access")
public ShowConnectorFunction() {
    this.addOption(
            OptionBuilder.withDescription(resourceString(Constants.RES_SHOW_PROMPT_DISPLAY_ALL_CONNECTORS))
                    .withLongOpt(Constants.OPT_ALL).create(Constants.OPT_ALL_CHAR));
    this.addOption(OptionBuilder.hasArg().withArgName("cid")
            .withDescription(resourceString(Constants.RES_SHOW_PROMPT_DISPLAY_CONNECTOR_CID))
            .withLongOpt(Constants.OPT_CID).create(Constants.OPT_CID_CHAR));
}

From source file:org.apache.sqoop.shell.ShowJobFunction.java

@SuppressWarnings("static-access")
public ShowJobFunction() {
    this.addOption(OptionBuilder.withDescription(resourceString(Constants.RES_SHOW_PROMPT_DISPLAY_ALL_JOBS))
            .withLongOpt(Constants.OPT_ALL).create(Constants.OPT_ALL_CHAR));
    this.addOption(OptionBuilder.hasArg().withArgName(Constants.OPT_JID)
            .withDescription(resourceString(Constants.RES_SHOW_PROMPT_DISPLAY_JOB_JID))
            .withLongOpt(Constants.OPT_JID).create(Constants.OPT_JID_CHAR));
}

From source file:org.apache.sqoop.shell.ShowJobStatusFunction.java

@SuppressWarnings("static-access")
public ShowJobStatusFunction() {
    this.addOption(OptionBuilder.hasArg().withArgName(Constants.OPT_JID)
            .withDescription(resourceString(Constants.RES_PROMPT_JOB_ID)).withLongOpt(Constants.OPT_JID)
            .create(Constants.OPT_JID_CHAR));
}