Java Code Examples for org.apache.commons.cli.OptionBuilder#hasOptionalArg()

The following examples show how to use org.apache.commons.cli.OptionBuilder#hasOptionalArg() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CliOption.java    From tracecompass with Eclipse Public License 2.0 5 votes vote down vote up
Option toCliOption() {
    OptionBuilder.withArgName(fArgumentName);
    // Number of arguments
    if (fMaxArguments == 0) {
        // No arguments
        OptionBuilder.hasArg(false);
    } else if (fMaxArguments == 1) {
        // 1 argument, optional or mandatory
        if (fMinArguments == 0) {
            OptionBuilder.hasOptionalArg();
        } else {
            OptionBuilder.hasArg();
        }
    } else if (fMaxArguments < Integer.MAX_VALUE) {
        // Many arguments, optional or mandatory
        if (fMinArguments == 0) {
            OptionBuilder.hasOptionalArgs(fMaxArguments);
        } else {
            OptionBuilder.hasArgs(fMaxArguments);
        }
    } else {
        // Unbounded number of optional or mandatory arguments
        if (fMinArguments == 0) {
            OptionBuilder.hasOptionalArgs();
        } else {
            OptionBuilder.hasArgs();
        }
    }
    if (fLongOption != null) {
        OptionBuilder.withLongOpt(fLongOption);
    }
    if (fDescription != null) {
        OptionBuilder.withDescription(fDescription);
    }
    return Objects.requireNonNull(OptionBuilder.create(fShortOption));
}
 
Example 2
Source File: LoopReader.java    From nutch-htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * Runs the LoopReader tool.  For this tool to work the loops job must have
 * already been run on the corresponding WebGraph.
 */
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 {

    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;
    }

    String webGraphDb = line.getOptionValue("webgraphdb");
    String url = line.getOptionValue("url");
    LoopReader reader = new LoopReader(NutchConfiguration.create());
    reader.dumpUrl(new Path(webGraphDb), url);
    return;
  }
  catch (Exception e) {
    e.printStackTrace();
    return;
  }
}
 
Example 3
Source File: NodeReader.java    From nutch-htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * 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.
 */
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;
  }
}