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:BuildInvertedIndex.java

/**
 * Runs this tool./*  w w  w .  j ava 2s . c  o  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));
    options.addOption(OptionBuilder.withArgName("num").hasArg().withDescription("number of reducers")
            .create(NUM_REDUCERS));

    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);
    int reduceTasks = cmdline.hasOption(NUM_REDUCERS) ? Integer.parseInt(cmdline.getOptionValue(NUM_REDUCERS))
            : 1;

    LOG.info("Tool name: " + BuildInvertedIndex.class.getSimpleName());
    LOG.info(" - input path: " + inputPath);
    LOG.info(" - output path: " + outputPath);
    LOG.info(" - num reducers: " + reduceTasks);

    Job job = Job.getInstance(getConf());
    job.setJobName(BuildInvertedIndex.class.getSimpleName());
    job.setJarByClass(BuildInvertedIndex.class);

    job.setNumReduceTasks(reduceTasks);

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

    job.setMapOutputKeyClass(Text.class);
    job.setMapOutputValueClass(PairOfInts.class);
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(PairOfWritables.class);
    job.setOutputFormatClass(MapFileOutputFormat.class);

    job.setMapperClass(MyMapper.class);
    job.setReducerClass(MyReducer.class);

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

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

    return 0;
}

From source file:edu.uchicago.lowasser.flaginjection.Flags.java

public static Injector bootstrapFlagInjector(final String[] args, String mainClassName, List<String> packages,
        Module... baseModules) {/*from   w ww  .j ava 2 s . co m*/
    Logger logger = Logger.getLogger("org.learningu.scheduling.flags.Flags");
    AbstractModule linkingModule = new AbstractModule() {

        @Override
        protected void configure() {
        }

        @Provides
        @RuntimeArguments
        String[] commandLineArguments() {
            return args;
        }

        @Provides
        @Singleton
        Options options(Map<Flag, Type> flagsMap) {
            Options options = new Options();
            for (Flag flag : flagsMap.keySet()) {
                OptionBuilder.hasArgs();
                OptionBuilder.withLongOpt(flag.name());
                OptionBuilder.withDescription(flag.description());
                OptionBuilder.withArgName(flagsMap.get(flag).toString());
                options.addOption(OptionBuilder.create());
            }
            return options;
        }

        @Provides
        @Singleton
        CommandLine commandLine(Options options, @RuntimeArguments String[] args) {
            try {
                return new PosixParser().parse(options, args);
            } catch (ParseException e) {
                throw Throwables.propagate(e);
            }
        }
    };
    logger.fine("Built Options module");
    Injector baseInjector = Guice.createInjector(new FlagErrorModule(mainClassName),
            Modules.combine(Iterables.concat(Arrays.asList(baseModules), ImmutableList.of(linkingModule))));
    logger.fine("Bootstrapping flag injector with command line arguments");
    Injector createdInjector = baseInjector
            .createChildInjector(baseInjector.getInstance(FlagBootstrapModule.class));
    // Use reflection to instantiate the variables in FlagClass classes
    for (String packageName : packages) {
        Reflections reflections = new Reflections(packageName);
        Set<Class<? extends FlagsClass>> classes = reflections.getSubTypesOf(FlagsClass.class);
        for (Class<? extends FlagsClass> flagClass : classes) {
            createdInjector.getInstance(flagClass);
        }
    }
    return createdInjector;
}

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

/**
 * Runs this tool./*from ww w .j a  v a2  s  .c o  m*/
 */
@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));
    options.addOption(OptionBuilder.withArgName("path").hasArg()
            .withDescription("(optional) output file to write the number of records").create(COUNT_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: " + CountTrecDocuments.class.getSimpleName());
    LOG.info(" - input: " + inputPath);
    LOG.info(" - output dir: " + outputPath);
    LOG.info(" - docno mapping file: " + mappingFile);

    Job job = new Job(getConf(), CountTrecDocuments.class.getSimpleName());
    job.setJarByClass(CountTrecDocuments.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.");

    if (cmdline.hasOption(COUNT_OPTION)) {
        String f = cmdline.getOptionValue(COUNT_OPTION);
        FileSystem fs = FileSystem.get(getConf());
        FSDataOutputStream out = fs.create(new Path(f));
        out.write(new Integer(numDocs).toString().getBytes());
        out.close();
    }

    return numDocs;
}

From source file:edu.umd.cloud9.collection.clue.ClueWarcForwardIndexBuilder.java

/**
 * Runs this tool./*from   w  w  w. j  av a2s . co  m*/
 */
@SuppressWarnings("static-access")
public int run(String[] args) throws Exception {
    Options options = new Options();
    options.addOption(OptionBuilder.withArgName("path").hasArg()
            .withDescription("(required) collection path (must be block-compressed SequenceFiles)")
            .create(COLLECTION_OPTION));
    options.addOption(OptionBuilder.withArgName("path").hasArg().withDescription("(required) output index path")
            .create(INDEX_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(INDEX_OPTION)) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp(this.getClass().getName(), options);
        ToolRunner.printGenericCommandUsage(System.out);
        return -1;
    }

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

    String collectionPath = cmdline.getOptionValue(COLLECTION_OPTION);
    String indexFile = cmdline.getOptionValue(INDEX_OPTION);

    LOG.info("Tool name: " + ClueWarcForwardIndexBuilder.class.getSimpleName());
    LOG.info(" - collection path: " + collectionPath);
    LOG.info(" - index file: " + indexFile);
    LOG.info("Note: This tool only works on block-compressed SequenceFiles!");

    Random random = new Random();
    Path outputPath = new Path(
            "tmp-" + ClueWarcForwardIndexBuilder.class.getSimpleName() + "-" + random.nextInt(10000));

    conf.setJobName(ClueWarcForwardIndexBuilder.class.getSimpleName() + ":" + collectionPath);

    conf.setNumMapTasks(100);
    conf.setNumReduceTasks(1);

    // Note, we have to add the files one by one, otherwise, SequenceFileInputFormat
    // thinks its a MapFile.
    for (FileStatus status : fs.listStatus(new Path(collectionPath))) {
        FileInputFormat.addInputPath(conf, status.getPath());
    }
    FileOutputFormat.setOutputPath(conf, 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(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...");
    LineReader reader = new LineReader(fs.open(new Path(outputPath + "/part-00000")));
    FSDataOutputStream out = fs.create(new Path(indexFile), true);

    out.writeUTF(ClueWarcForwardIndex.class.getCanonicalName());
    out.writeUTF(collectionPath);
    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!");
    }

    fs.delete(outputPath, true);
    return 0;
}

From source file:com.cloudera.sqoop.tool.CodeGenTool.java

@Override
/** Configure the command-line arguments we expect to receive */
public void configureOptions(ToolOptions toolOptions) {

    toolOptions.addUniqueOptions(getCommonOptions());

    RelatedOptions codeGenOpts = getCodeGenOpts(false);
    codeGenOpts.addOption(OptionBuilder.withArgName("table-name").hasArg()
            .withDescription("Table to generate code for").withLongOpt(TABLE_ARG).create());
    toolOptions.addUniqueOptions(codeGenOpts);

    toolOptions.addUniqueOptions(getOutputFormatOptions());
    toolOptions.addUniqueOptions(getInputFormatOptions());
    toolOptions.addUniqueOptions(getHiveOptions(true));
}

From source file:com.cloudera.sqoop.tool.EvalSqlTool.java

@Override
/** Configure the command-line arguments we expect to receive */
public void configureOptions(ToolOptions toolOptions) {
    toolOptions.addUniqueOptions(getCommonOptions());

    RelatedOptions evalOpts = new RelatedOptions("SQL evaluation arguments");
    evalOpts.addOption(OptionBuilder.withArgName("statement").hasArg()
            .withDescription("Execute 'statement' in SQL and exit").withLongOpt(SQL_QUERY_ARG)
            .create(SQL_QUERY_SHORT_ARG));

    toolOptions.addUniqueOptions(evalOpts);
}

From source file:BooleanRetrieval.java

/**
 * Runs this tool./*from ww  w. ja v  a 2 s. c  o 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(INDEX));
    options.addOption(
            OptionBuilder.withArgName("path").hasArg().withDescription("output path").create(COLLECTION));

    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(INDEX) || !cmdline.hasOption(COLLECTION)) {
        System.out.println("args: " + Arrays.toString(args));
        HelpFormatter formatter = new HelpFormatter();
        formatter.setWidth(120);
        formatter.printHelp(LookupPostings.class.getName(), options);
        ToolRunner.printGenericCommandUsage(System.out);
        System.exit(-1);
    }

    String indexPath = cmdline.getOptionValue(INDEX);
    String collectionPath = cmdline.getOptionValue(COLLECTION);

    if (collectionPath.endsWith(".gz")) {
        System.out.println("gzipped collection is not seekable: use compressed version!");
        System.exit(-1);
    }

    FileSystem fs = FileSystem.get(new Configuration());

    initialize(indexPath, collectionPath, fs);

    String[] queries = { "outrageous fortune AND", "white rose AND", "means deceit AND",
            "white red OR rose AND pluck AND", "unhappy outrageous OR good your AND OR fortune AND" };

    for (String q : queries) {
        System.out.println("Query: " + q);

        runQuery(q);
        System.out.println("");
    }

    return 1;
}

From source file:eu.skqs.bertie.standalone.BertieStandalone.java

public static void main(String[] args) {

    // Options/*from   w  w w .  j  a  v  a  2 s . c om*/
    Option file = OptionBuilder.withArgName("file").withLongOpt("file").hasArg()
            .withDescription("File to annotate").create("f");

    Option directory = OptionBuilder.withArgName("directory").withLongOpt("directory").hasArg()
            .withDescription("Directory to annotate").create("d");

    Option owl = OptionBuilder.withArgName("owl").withLongOpt("owl").hasArg()
            .withDescription("OWL file to use in annotation").create("o");

    Option plain = OptionBuilder.withLongOpt("plain").withDescription("Plain text file format").create("p");

    Option tei = OptionBuilder.withLongOpt("tei").withDescription("TEI file format").create("t");

    Option mode = OptionBuilder.withArgName("extract|minimal|maximal|prosody").withLongOpt("mode").hasArg()
            .withDescription("Mode to operate in").create("m");

    Option clean = OptionBuilder.withArgName("T0,T1,T3").withLongOpt("clean").hasArg()
            .withDescription("Remove gives types, MUST START UPPERCASE").create("c");

    Options options = new Options();
    options.addOption(file);
    options.addOption(directory);
    options.addOption(owl);
    options.addOption(plain);
    options.addOption(tei);
    options.addOption(mode);
    options.addOption(clean);

    CommandLineParser parser = new GnuParser();
    HelpFormatter formatter = new HelpFormatter();
    CommandLine cmdline = null;

    try {
        cmdline = parser.parse(options, args);
    } catch (ParseException e) {
        e.printStackTrace();
        System.exit(-1);
    }

    BertieStandalone standalone = new BertieStandalone();
    String documentPath = null;

    // Check for custom OWL
    if (cmdline.hasOption("owl")) {
        owlPath = cmdline.getOptionValue("owl");
    }

    // Check for clean
    if (cmdline.hasOption("clean")) {
        typesToRemove = cmdline.getOptionValue("clean");
    }

    // Check for mode
    if (cmdline.hasOption("mode")) {
        String currentMode = cmdline.getOptionValue("mode");

        if (currentMode.equals("extract")) {
            extractMode = true;
        } else if (currentMode.equals("poetry")) {
            poetryMode = true;
        }

        analysisMode = currentMode;
    }

    // Check for directory option
    if (cmdline.hasOption("directory")) {
        // We support TEI directorys only
        if (!cmdline.hasOption("tei")) {
            logger.log(Level.WARNING, "TEI file format must be selected with directory argument");
            System.exit(-1);
        }

        String directoryPath = cmdline.getOptionValue("directory");

        if (extractMode) {
            try {
                standalone.extractWithCollectionReader(directoryPath);
            } catch (Exception e) {
            }

            System.exit(0);
        }

        try {
            standalone.processWithCollectionReader(directoryPath);
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(-1);
        }

        System.exit(0);
    }

    // Check for file option
    if (cmdline.hasOption("file")) {
        // TODO: clean this up
        documentPath = cmdline.getOptionValue("file");
        filePath = cmdline.getOptionValue("file");

        // TEI
        if (cmdline.hasOption("tei")) {
            try {
                processWithFile();
            } catch (Exception e) {
                e.printStackTrace();
                System.exit(-1);
            }
            System.exit(0);
        }

        // Check for plain option
        if (!cmdline.hasOption("plain")) {
            logger.log(Level.WARNING, "Plain text format must be selected with file argument");
            System.exit(-1);
        }
    } else {
        logger.log(Level.WARNING, "No file argument given. Quitting.");
        formatter.printHelp("bertie", options);
        System.exit(-1);
    }

    // Make sure we have a document path
    if (documentPath == null) {
        logger.log(Level.WARNING, "Document path is empty. Quitting.");
        System.exit(-1);
    }

    // Read the document
    try {
        String encodingType = "UTF-8";

        BufferedReader fileReader = new BufferedReader(
                new InputStreamReader(new FileInputStream(documentPath), encodingType));

        StringBuilder sb = new StringBuilder();
        String line = null;

        while ((line = fileReader.readLine()) != null) {
            sb.append(line + newLine);
        }

        String input = sb.toString();
        fileReader.close();

        String output = standalone.process(input);
        if (output == null) {
            logger.log(Level.WARNING, "Empty processing result.");
            System.exit(-1);
        }

        PrintWriter fileWriter = new PrintWriter(documentPath, encodingType);
        fileWriter.write(output);
        fileWriter.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.boundary.sdk.event.EventCLI.java

@SuppressWarnings("static-access")
private void addOrganizationIdOption() {
    optionOrganizationId = OptionBuilder.withArgName("org_id").hasArg()
            .withDescription("Boundary organization Id").withLongOpt("org-id").create(OPTION_ORG_ID);
    options.addOption(optionOrganizationId);
}

From source file:acromusashi.stream.client.DrpcRequestClient.java

/**
 * ???/*w w  w.j a  v  a2  s .  co  m*/
 * 
 * @return ??
 */
public static Options createOptions() {
    Options cliOptions = new Options();

    // DRPCServer Host
    OptionBuilder.hasArg(true);
    OptionBuilder.withArgName("DRPCServer Host");
    OptionBuilder.withDescription("DRPCServer Host");
    OptionBuilder.isRequired(true);
    Option serverOption = OptionBuilder.create("h");

    // DRPCServer Port
    OptionBuilder.hasArg(true);
    OptionBuilder.withArgName("DRPCServer Port");
    OptionBuilder.withDescription("DRPCServer Port");
    OptionBuilder.isRequired(false);
    Option portOption = OptionBuilder.create("p");

    // 
    OptionBuilder.hasArg(true);
    OptionBuilder.withArgName("DRPC Request Timeout");
    OptionBuilder.withDescription("DRPC Request Timeout");
    OptionBuilder.isRequired(false);
    Option timeoutOption = OptionBuilder.create("t");

    // DRPC Function
    OptionBuilder.hasArg(true);
    OptionBuilder.withArgName("DRPC Function");
    OptionBuilder.withDescription("DRPC Function");
    OptionBuilder.isRequired(true);
    Option functionOption = OptionBuilder.create("f");

    // DRPC Function Arg
    OptionBuilder.hasArg(true);
    OptionBuilder.withArgName("DRPC Function Arg");
    OptionBuilder.withDescription("DRPC Function Arg");
    OptionBuilder.isRequired(true);
    Option funcArgOption = OptionBuilder.create("a");

    // 
    OptionBuilder.withDescription("show help");
    Option helpOption = OptionBuilder.create("sh");

    cliOptions.addOption(serverOption);
    cliOptions.addOption(portOption);
    cliOptions.addOption(timeoutOption);
    cliOptions.addOption(functionOption);
    cliOptions.addOption(funcArgOption);
    cliOptions.addOption(helpOption);
    return cliOptions;
}