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:de.l3s.content.timex.extracting.WikiTimex.java

@SuppressWarnings("static-access")
@Override//from  w  w w.  ja va 2  s . c  o  m
public int run(String[] args) throws Exception {
    Options options = new Options();
    options.addOption(
            OptionBuilder.withArgName("path").hasArg().withDescription("XML dump file").create(INPUT_OPTION));
    options.addOption(OptionBuilder.withArgName("en|sv|de|cs|es|zh|ar|tr").hasArg()
            .withDescription("two-letter language code").create(LANGUAGE_OPTION));

    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_OPTION)) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp(this.getClass().getName(), options);
        ToolRunner.printGenericCommandUsage(System.out);
        return -1;
    }

    String language = "en"; // Assume 'en' by default.
    if (cmdline.hasOption(LANGUAGE_OPTION)) {
        language = cmdline.getOptionValue(LANGUAGE_OPTION);
        if (language.length() != 2) {
            System.err.println("Error: \"" + language + "\" unknown language!");
            return -1;
        }
    }

    String inputPath = cmdline.getOptionValue(INPUT_OPTION);

    LOG.info("Tool name: " + this.getClass().getName());
    LOG.info(" - XML dump file: " + inputPath);
    LOG.info(" - language: " + language);

    Job job = Job.getInstance(getConf());
    job.setJarByClass(WikiTimex.class);
    job.setJobName(String.format("CountWikipediaPages[%s: %s, %s: %s]", INPUT_OPTION, inputPath,
            LANGUAGE_OPTION, language));

    job.setNumReduceTasks(0);

    FileInputFormat.setInputPaths(job, new Path(inputPath));

    if (language != null) {
        job.getConfiguration().set("wiki.language", language);
    }

    job.setInputFormatClass(WikipediaPageInputFormat.class);
    job.setOutputFormatClass(NullOutputFormat.class);

    job.setMapperClass(TMapper.class);

    job.waitForCompletion(true);

    return 0;
}

From source file:at.newmedialab.ldpath.template.LDTemplate.java

private static Options buildOptions() {
    Options result = new Options();

    Option context = OptionBuilder.withArgName("uri").hasArg()
            .withDescription("URI of the context node to start from").create("context");
    context.setRequired(true);//  ww w .jav a  2  s.  com
    result.addOption(context);

    Option template = OptionBuilder.withArgName("file").hasArg()
            .withDescription("the template file to apply to the context resource").create("template");
    template.setRequired(true);
    result.addOption(template);

    Option out = OptionBuilder.withArgName("file").hasArg()
            .withDescription("file where to write the output; if not given, will write to stdout")
            .create("out");
    out.setRequired(false);
    result.addOption(out);

    Option loglevel = OptionBuilder.withArgName("level").hasArg()
            .withDescription("set the log level; default is 'warn'").create("loglevel");
    result.addOption(loglevel);

    Option store = OptionBuilder.withArgName("dir").hasArg()
            .withDescription("cache the retrieved data in this directory").create("store");
    result.addOption(store);

    return result;
}

From source file:com.google.code.linkedinapi.client.examples.OAuthAuthenticationExample.java

/**
  * Build command line options object.//  w  w  w.j a  v  a 2  s.c om
  */
private static Options buildOptions() {

    Options opts = new Options();

    String helpMsg = "Print this message.";
    Option help = new Option(HELP_OPTION, helpMsg);
    opts.addOption(help);

    String consumerKeyMsg = "You API Consumer Key.";
    OptionBuilder.withArgName("consumerKey");
    OptionBuilder.hasArg();
    OptionBuilder.withDescription(consumerKeyMsg);
    Option consumerKey = OptionBuilder.create(CONSUMER_KEY_OPTION);
    opts.addOption(consumerKey);

    String consumerSecretMsg = "You API Consumer Secret.";
    OptionBuilder.withArgName("consumerSecret");
    OptionBuilder.hasArg();
    OptionBuilder.withDescription(consumerSecretMsg);
    Option consumerSecret = OptionBuilder.create(CONSUMER_SECRET_OPTION);
    opts.addOption(consumerSecret);

    return opts;
}

From source file:ca.uqac.info.trace.generation.PetriNetGenerator.java

@SuppressWarnings("static-access")
@Override/*from w  w  w.  j a  v a  2 s  . co  m*/
public Options getCommandLineOptions() {
    Options options = super.getCommandLineOptions();
    Option opt;
    opt = OptionBuilder.withArgName("x").hasArg().withDescription("Set Petri net to x (surrounded by quotes)")
            .create("f");
    options.addOption(opt);
    return options;
}

From source file:com.kijes.ela.cli.CommandLineOptions.java

private Option createOption(String optionName, boolean hasArg, boolean isRequired, String description) {
    return OptionBuilder.withArgName(optionName).hasArg(hasArg).isRequired(isRequired)
            .withDescription(description).create(optionName);
}

From source file:edu.umd.cloud9.collection.wikipedia.BuildWikipediaForwardIndex.java

@SuppressWarnings("static-access")
@Override/*  w  ww . ja  v  a 2  s  . com*/
public int run(String[] args) throws Exception {
    Options options = new Options();
    options.addOption(OptionBuilder.withArgName("path").hasArg().withDescription("input").create(INPUT_OPTION));
    options.addOption(OptionBuilder.withArgName("path").hasArg().withDescription("tmp output directory")
            .create(OUTPUT_OPTION));
    options.addOption(
            OptionBuilder.withArgName("path").hasArg().withDescription("index file").create(INDEX_FILE_OPTION));

    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_OPTION) || !cmdline.hasOption(OUTPUT_OPTION)
            || !cmdline.hasOption(INDEX_FILE_OPTION)) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp(this.getClass().getName(), options);
        ToolRunner.printGenericCommandUsage(System.out);
        return -1;
    }

    Path inputPath = new Path(cmdline.getOptionValue(INPUT_OPTION));
    String outputPath = cmdline.getOptionValue(OUTPUT_OPTION);
    String indexFile = cmdline.getOptionValue(INDEX_FILE_OPTION);

    if (!inputPath.isAbsolute()) {
        System.err.println("Error: " + INPUT_OPTION + " must be an absolute path!");
        return -1;
    }

    JobConf conf = new JobConf(getConf(), BuildWikipediaForwardIndex.class);
    FileSystem fs = FileSystem.get(conf);

    LOG.info("Tool name: " + this.getClass().getName());
    LOG.info(" - input path: " + inputPath);
    LOG.info(" - output path: " + outputPath);
    LOG.info(" - index file: " + indexFile);
    LOG.info("Note: This tool only works on block-compressed SequenceFiles!");

    conf.setJobName(String.format("BuildWikipediaForwardIndex[%s: %s, %s: %s]", INPUT_OPTION, inputPath,
            INDEX_FILE_OPTION, indexFile));

    conf.setNumReduceTasks(1);

    FileInputFormat.setInputPaths(conf, inputPath);
    FileOutputFormat.setOutputPath(conf, new Path(outputPath));
    FileOutputFormat.setCompressOutput(conf, false);

    conf.setInputFormat(NoSplitSequenceFileInputFormat.class);
    conf.setOutputKeyClass(IntWritable.class);
    conf.setOutputValueClass(Text.class);

    conf.setMapRunnerClass(MyMapRunner.class);
    conf.setReducerClass(IdentityReducer.class);

    // delete the output directory if it exists already
    fs.delete(new Path(outputPath), true);

    RunningJob job = JobClient.runJob(conf);

    Counters counters = job.getCounters();
    int blocks = (int) counters.findCounter(Blocks.Total).getCounter();

    LOG.info("number of blocks: " + blocks);

    LOG.info("Writing index file...");
    FSLineReader reader = new FSLineReader(outputPath + "/part-00000", fs);
    FSDataOutputStream out = fs.create(new Path(indexFile), true);

    out.writeUTF("edu.umd.cloud9.collection.wikipedia.WikipediaForwardIndex");
    out.writeUTF(inputPath.toString());
    out.writeInt(blocks);

    int cnt = 0;
    Text line = new Text();
    while (reader.readLine(line) > 0) {
        String[] arr = line.toString().split("\\s+");

        int docno = Integer.parseInt(arr[0]);
        int offset = Integer.parseInt(arr[1]);
        short fileno = Short.parseShort(arr[2]);

        out.writeInt(docno);
        out.writeInt(offset);
        out.writeShort(fileno);

        cnt++;

        if (cnt % 100000 == 0) {
            LOG.info(cnt + " blocks written");
        }
    }

    reader.close();
    out.close();

    if (cnt != blocks) {
        throw new RuntimeException("Error: mismatch in block count!");
    }

    return 0;
}

From source file:io.bfscan.clueweb12.MergeTermStatistics.java

/**
 * Runs this tool./*from w  w w  . j av a  2s.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_OPTION));
    options.addOption(
            OptionBuilder.withArgName("path").hasArg().withDescription("output path").create(OUTPUT_OPTION));
    options.addOption(
            OptionBuilder.withArgName("num").hasArg().withDescription("minimum df").create(DF_MIN_OPTION));

    CommandLine cmdline;
    CommandLineParser parser = new GnuParser();
    try {
        cmdline = parser.parse(options, args);
    } catch (ParseException exp) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp(this.getClass().getName(), options);
        ToolRunner.printGenericCommandUsage(System.out);
        System.err.println("Error parsing command line: " + exp.getMessage());
        return -1;
    }

    if (!cmdline.hasOption(INPUT_OPTION) || !cmdline.hasOption(OUTPUT_OPTION)) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp(this.getClass().getName(), options);
        ToolRunner.printGenericCommandUsage(System.out);
        return -1;
    }

    String input = cmdline.getOptionValue(INPUT_OPTION);
    String output = cmdline.getOptionValue(OUTPUT_OPTION);

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

    Job job = new Job(getConf(), MergeTermStatistics.class.getSimpleName() + ":" + input);
    job.setJarByClass(MergeTermStatistics.class);

    job.setNumReduceTasks(100);

    if (cmdline.hasOption(DF_MIN_OPTION)) {
        int dfMin = Integer.parseInt(cmdline.getOptionValue(DF_MIN_OPTION));
        LOG.info(" - dfMin: " + dfMin);
        job.getConfiguration().setInt(HADOOP_DF_MIN_OPTION, dfMin);
    }

    FileInputFormat.setInputPaths(job, input);
    FileOutputFormat.setOutputPath(job, new Path(output));

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

    job.setMapOutputKeyClass(Text.class);
    job.setMapOutputValueClass(PairOfIntLong.class);
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(PairOfIntLong.class);

    job.setCombinerClass(MyCombiner.class);
    job.setReducerClass(MyReducer.class);

    FileSystem.get(getConf()).delete(new Path(output), true);

    long startTime = System.currentTimeMillis();
    job.waitForCompletion(true);
    LOG.info("Job Finished in " + (System.currentTimeMillis() - startTime) / 1000.0 + " seconds");

    return 0;
}

From source file:com.google.code.linkedinapi.client.examples.ProfileApiExample.java

/**
  * Build command line options object.//from w  ww .  ja va 2 s .  co  m
  */
private static Options buildOptions() {

    Options opts = new Options();

    String helpMsg = "Print this message.";
    Option help = new Option(HELP_OPTION, helpMsg);
    opts.addOption(help);

    String consumerKeyMsg = "You API Consumer Key.";
    OptionBuilder.withArgName("consumerKey");
    OptionBuilder.hasArg();
    OptionBuilder.withDescription(consumerKeyMsg);
    Option consumerKey = OptionBuilder.create(CONSUMER_KEY_OPTION);
    opts.addOption(consumerKey);

    String consumerSecretMsg = "You API Consumer Secret.";
    OptionBuilder.withArgName("consumerSecret");
    OptionBuilder.hasArg();
    OptionBuilder.withDescription(consumerSecretMsg);
    Option consumerSecret = OptionBuilder.create(CONSUMER_SECRET_OPTION);
    opts.addOption(consumerSecret);

    String accessTokenMsg = "You OAuth Access Token.";
    OptionBuilder.withArgName("accessToken");
    OptionBuilder.hasArg();
    OptionBuilder.withDescription(accessTokenMsg);
    Option accessToken = OptionBuilder.create(ACCESS_TOKEN_OPTION);
    opts.addOption(accessToken);

    String tokenSecretMsg = "You OAuth Access Token Secret.";
    OptionBuilder.withArgName("accessTokenSecret");
    OptionBuilder.hasArg();
    OptionBuilder.withDescription(tokenSecretMsg);
    Option accessTokenSecret = OptionBuilder.create(ACCESS_TOKEN_SECRET_OPTION);
    opts.addOption(accessTokenSecret);

    String idMsg = "ID of the user whose profile is to be fetched.";
    OptionBuilder.withArgName("id");
    OptionBuilder.hasArg();
    OptionBuilder.withDescription(idMsg);
    Option id = OptionBuilder.create(ID_OPTION);
    opts.addOption(id);

    String urlMsg = "Profile URL of the user whose profile is to be fetched.";
    OptionBuilder.withArgName("url");
    OptionBuilder.hasArg();
    OptionBuilder.withDescription(urlMsg);
    Option url = OptionBuilder.create(URL_OPTION);
    opts.addOption(url);

    String typeMsg = "Type of profile. Either standard or public.";
    OptionBuilder.withArgName("type");
    OptionBuilder.hasArg();
    OptionBuilder.withDescription(typeMsg);
    Option type = OptionBuilder.create(TYPE_OPTION);
    opts.addOption(type);

    return opts;
}

From source file:edu.umd.cloud9.collection.trec.DemoCountTrecDocuments.java

/**
 * Runs this tool./*from ww w . j av a 2  s.  c om*/
 */
@SuppressWarnings("static-access")
public int run(String[] args) throws Exception {
    Options options = new Options();
    options.addOption(OptionBuilder.withArgName("path").hasArg().withDescription("(required) collection path")
            .create(COLLECTION_OPTION));
    options.addOption(OptionBuilder.withArgName("path").hasArg().withDescription("(required) output path")
            .create(OUTPUT_OPTION));
    options.addOption(OptionBuilder.withArgName("path").hasArg().withDescription("(required) DocnoMapping data")
            .create(MAPPING_OPTION));

    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(COLLECTION_OPTION) || !cmdline.hasOption(OUTPUT_OPTION)
            || !cmdline.hasOption(MAPPING_OPTION)) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp(this.getClass().getName(), options);
        ToolRunner.printGenericCommandUsage(System.out);
        return -1;
    }

    String inputPath = cmdline.getOptionValue(COLLECTION_OPTION);
    String outputPath = cmdline.getOptionValue(OUTPUT_OPTION);
    String mappingFile = cmdline.getOptionValue(MAPPING_OPTION);

    LOG.info("Tool: " + DemoCountTrecDocuments.class.getCanonicalName());
    LOG.info(" - input: " + inputPath);
    LOG.info(" - output dir: " + outputPath);
    LOG.info(" - docno mapping file: " + mappingFile);

    Job job = new Job(getConf(), DemoCountTrecDocuments.class.getSimpleName());
    job.setJarByClass(DemoCountTrecDocuments.class);

    job.setNumReduceTasks(0);

    // Pass in the class name as a String; this is makes the mapper general in being able to load
    // any collection of Indexable objects that has docid/docno mapping specified by a DocnoMapping
    // object.
    job.getConfiguration().set("DocnoMappingClass", TrecDocnoMapping.class.getCanonicalName());

    // Put the mapping file in the distributed cache so each map worker will have it.
    DistributedCache.addCacheFile(new URI(mappingFile), job.getConfiguration());

    FileInputFormat.setInputPaths(job, new Path(inputPath));
    FileOutputFormat.setOutputPath(job, new Path(outputPath));
    FileOutputFormat.setCompressOutput(job, false);

    job.setInputFormatClass(TrecDocumentInputFormat.class);
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(IntWritable.class);

    job.setMapperClass(MyMapper.class);

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

    job.waitForCompletion(true);

    Counters counters = job.getCounters();
    int numDocs = (int) counters.findCounter(Count.DOCS).getValue();
    LOG.info("Read " + numDocs + " docs.");

    return 0;
}

From source file:com.github.errantlinguist.latticevisualiser.ArgParser.java

/**
 * Creates and adds a minimum state size option to a given {@link Options}
 * object.//w w  w.  j  a v a 2  s .c  om
 * 
 * @param options
 *            The <code>Options</code> object to add to.
 */
private static void addMinStateSizeOption(final Options options) {
    OptionBuilder.withLongOpt(MIN_STATE_SIZE_KEY_LONG);
    OptionBuilder.withDescription(MIN_STATE_SIZE_DESCR);
    OptionBuilder.hasArg();
    OptionBuilder.withArgName(SIZE_ARG_NAME);
    OptionBuilder.withType(int.class);

    final Option minStateSize = OptionBuilder.create(MIN_STATE_SIZE_KEY);
    options.addOption(minStateSize);
}