Example usage for org.apache.commons.cli GnuParser GnuParser

List of usage examples for org.apache.commons.cli GnuParser GnuParser

Introduction

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

Prototype

GnuParser

Source Link

Usage

From source file:it.drwolf.ridire.utility.IndexQuery.java

private void parseOptions(String[] args) {
    HelpFormatter formatter = new HelpFormatter();
    CommandLineParser parser = new GnuParser();
    org.apache.commons.cli.CommandLine cmdline = null;
    try {//from w w w .j  av a2s  .c om
        // parse the command line arguments
        cmdline = parser.parse(this.options, args);
    } catch (ParseException exp) {
        // oops, something went wrong
        System.err.println("Index query.  Reason: " + exp.getMessage());
        formatter.printHelp("Index query.", this.options);
        System.exit(-1);
    }
    if (cmdline != null) {
        this.dirName = cmdline.getOptionValue("i");
        if (this.dirName == null) {
            System.err.println("No directory provided.");
            formatter.printHelp("Index query.", this.options);
            System.exit(-1);
        }
        this.term = cmdline.getOptionValue("t");
        if (this.term == null) {
            System.err.println("No term provided.");
            formatter.printHelp("Index query.", this.options);
            System.exit(-1);
        }
    }
}

From source file:com.googlecode.icegem.cacheutils.updater.UpdateTool.java

protected void parseCommandLineArguments(String[] commandLineArguments) {
    Options options = constructGnuOptions();
    if (commandLineArguments.length < 1) {
        printHelp(options);//  ww  w  . j  a  v  a2  s .c o m
        Utils.exitWithSuccess();
    }
    CommandLineParser parser = new GnuParser();
    try {
        CommandLine line = parser.parse(options, commandLineArguments);
        if (line.hasOption("regions")) {
            withSubRegionsOption = line.hasOption("subregions");
            regionsOption = line.getOptionValue("regions");
            locatorOption = line.getOptionValue("locator");
            serverOption = line.getOptionValue("server");
            if (line.hasOption("packages"))
                scanPackagesOption = Arrays.asList(line.getOptionValue("packages").split(","));
        } else if (line.hasOption("all")) {
            regionsOption = "all";
            locatorOption = line.getOptionValue("locator");
            serverOption = line.getOptionValue("server");
            if (line.hasOption("packages"))
                scanPackagesOption = Arrays.asList(line.getOptionValue("packages").split(","));
        } else if (line.hasOption("help")) {
            printHelp(options);
            Utils.exitWithSuccess();
        } else {
            printHelp(options);
            Utils.exitWithSuccess();
        }
    } catch (ParseException exp) {
        System.err.println("Parsing options failed. " + exp.getMessage());
        printHelp(options);
        Utils.exitWithSuccess();
    }
}

From source file:com.zimbra.cs.store.file.BlobDeduperUtil.java

private void parseArgs(String[] args) throws ParseException {
    GnuParser parser = new GnuParser();
    CommandLine cl = parser.parse(options, args);

    if (CliUtil.hasOption(cl, LO_HELP)) {
        usage(null);//from www  .  ja  v  a 2  s  .co  m
    }
    // Require the "start" command, so that someone doesn't inadvertently
    // kick of a dedupe.
    if (cl.getArgs().length == 0) {
        usage(null);
    } else if (cl.getArgs()[0].equals("stop")) {
        action = DedupeBlobsRequest.DedupAction.stop;
        return;
    } else if (cl.getArgs()[0].equals("status")) {
        action = DedupeBlobsRequest.DedupAction.status;
        return;
    } else if (cl.getArgs()[0].equals("start")) {
        action = DedupeBlobsRequest.DedupAction.start;
    } else if (cl.getArgs()[0].equals("reset")) {
        if (CliUtil.confirm("This will remove all the metadata used by dedupe process. Continue?")) {
            action = DedupeBlobsRequest.DedupAction.reset;
        } else {
            System.exit(0);
        }
    } else {
        usage(null);
    }

    verbose = CliUtil.hasOption(cl, LO_VERBOSE);

    String volumeList = CliUtil.getOptionValue(cl, LO_VOLUMES);
    if (volumeList != null) {
        for (String id : volumeList.split(",")) {
            try {
                volumeIds.add(Short.parseShort(id));
            } catch (NumberFormatException e) {
                usage("Invalid volume id: " + id);
            }
        }
    }
}

From source file:com.digitalpebble.behemoth.mahout.util.ClusterDocIDDumper.java

public int run(String[] args) throws Exception {

    Options options = new Options();
    // automatically generate the help statement
    HelpFormatter formatter = new HelpFormatter();
    // create the parser
    CommandLineParser parser = new GnuParser();

    options.addOption("h", "help", false, "print this message");
    options.addOption("i", "input", true, "input clusteredPoints");
    options.addOption("o", "output", true, "output doc cluster IDs");

    // parse the command line arguments
    CommandLine line = null;/* w  w w.j a va  2  s . c o  m*/
    try {
        line = parser.parse(options, args);
        if (line.hasOption("help")) {
            formatter.printHelp("ClusterDocIDDumper", options);
            return 0;
        }
        if (!line.hasOption("o") | !line.hasOption("i")) {
            formatter.printHelp("ClusterDocIDDumper", options);
            return -1;
        }
    } catch (ParseException e) {
        formatter.printHelp("ClusterDocIDDumper", options);
    }

    Path inPath = new Path(line.getOptionValue("i"));
    Path outPath = new Path(line.getOptionValue("o"));

    // extracts the string representations from the vectors
    int retVal = extract(inPath, outPath);
    if (retVal != 0) {
        HadoopUtil.delete(getConf(), outPath);
        return retVal;
    }

    return 0;
}

From source file:com.digitalpebble.behemoth.ClassifierJob.java

public int run(String[] args) throws Exception {

    Options options = new Options();
    // automatically generate the help statement
    HelpFormatter formatter = new HelpFormatter();
    // create the parser
    CommandLineParser parser = new GnuParser();

    options.addOption("h", "help", false, "print this message");
    options.addOption("i", "input", true, "input Behemoth corpus");
    options.addOption("o", "output", true, "output Behemoth corpus");
    options.addOption("m", "model", true, "location of the model");

    // parse the command line arguments
    CommandLine line = null;/*from  w w  w. j a v  a  2  s .co m*/
    try {
        line = parser.parse(options, args);
        String input = line.getOptionValue("i");
        String output = line.getOptionValue("o");
        String model = line.getOptionValue("m");
        if (line.hasOption("help")) {
            formatter.printHelp("ClassifierJob", options);
            return 0;
        }
        if (model == null | input == null | output == null) {
            formatter.printHelp("ClassifierJob", options);
            return -1;
        }
    } catch (ParseException e) {
        formatter.printHelp("ClassifierJob", options);
    }

    final FileSystem fs = FileSystem.get(getConf());

    Path inputPath = new Path(line.getOptionValue("i"));
    Path outputPath = new Path(line.getOptionValue("o"));
    String modelPath = line.getOptionValue("m");

    JobConf job = new JobConf(getConf());

    // push the model file to the DistributedCache
    DistributedCache.addCacheArchive(new URI(modelPath), job);

    job.setJarByClass(this.getClass());

    job.setJobName("ClassifierJob : " + inputPath.toString());

    job.setInputFormat(SequenceFileInputFormat.class);
    job.setOutputFormat(SequenceFileOutputFormat.class);

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

    job.setMapperClass(TextClassifierMapper.class);
    job.setNumReduceTasks(0);

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

    job.set(modelNameParam, modelPath);

    try {
        JobClient.runJob(job);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
    }

    return 0;
}

From source file:gruifo.CmdOptions.java

public CmdOptions(final String[] args) throws ParseException {
    options = new Options();
    options.addOption(HELP_OPTION);/* w ww. jav  a 2s .c  o  m*/
    options.addOption(JSNI_OPTION);
    options.addOption(JSINTEROP_OPTION);
    SRC_PATH_OPTION.setArgName(SRC_PATH_ARG);
    SRC_PATH_OPTION.setRequired(true);
    options.addOption(SRC_PATH_OPTION);
    TARGET_PATH_OPTION.setArgName(TARGET_PATH_ARG);
    TARGET_PATH_OPTION.setRequired(true);
    options.addOption(TARGET_PATH_OPTION);
    options.addOption(TYPE_MAPPING_OPTION);
    TYPE_MAPPING_OPTION.setArgName(TYPE_MAPPING_ARG);
    final CommandLineParser parser = new GnuParser();
    cmd = parser.parse(options, args);
}

From source file:com.plabadie.mockmail.server.MockMailServer.java

private void initWithCommandLine(String[] args) {
    Option help = new Option("help", "print this message");

    Option port = OptionBuilder.withArgName("portnumber").hasArg()
            .withDescription("Port number where mockmail serves smtp requests. Default : 2525.").create("port");

    Option outdir = OptionBuilder.withArgName("path").hasArg().withDescription(
            "Folder where the email files will be stored. Can be relative to the working dir. Default : 'mail'")
            .create("outdir");

    Options options = new Options();

    options.addOption(help);/*from w ww. ja  va  2 s  .  c o  m*/
    options.addOption(port);
    options.addOption(outdir);

    // create the parser
    CommandLineParser parser = new GnuParser();
    CommandLine line = null;

    try {
        // parse the command line arguments
        line = parser.parse(options, args);
    } catch (ParseException exp) {
        // oops, something went wrong
        System.err.println(exp.getMessage());
        System.exit(1);
    }

    if (line.hasOption("help")) {
        // automatically generate the help statement
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("java -jar mockmail.jar", options);
        System.exit(0);
    }

    if (line.hasOption("port")) {
        String portStringValue = line.getOptionValue("port");

        if (StringUtils.isNotBlank(portStringValue) && StringUtils.isNumeric(portStringValue)) {

            this.setPort(Integer.parseInt(portStringValue));

        }

    }
    if (line.hasOption("outdir")) {

        String outDirStringValue = line.getOptionValue("outdir");

        if (StringUtils.isNotBlank(outDirStringValue)) {

            this.setOutDirPath(outDirStringValue);

        }
    }
}

From source file:com.alibaba.wasp.master.FMasterCommandLine.java

public int run(String args[]) throws Exception {
    Options opt = new Options();
    opt.addOption("minServers", true, "Minimum FServers needed to host user tables");
    opt.addOption("backup", false, "Do not try to become FMaster until the primary fails");

    CommandLine cmd;/*from w w  w  .java  2  s .  co m*/
    try {
        cmd = new GnuParser().parse(opt, args);
    } catch (ParseException e) {
        LOG.error("Could not parse: ", e);
        usage(null);
        return -1;
    }

    if (cmd.hasOption("minServers")) {
        String val = cmd.getOptionValue("minServers");
        getConf().setInt("wasp.fserver.count.min", Integer.valueOf(val));
        LOG.debug("minServers set to " + val);
    }

    // check if we are the backup master - override the conf if so
    if (cmd.hasOption("backup")) {
        getConf().setBoolean(FConstants.MASTER_TYPE_BACKUP, true);
    }

    List<String> remainingArgs = cmd.getArgList();
    if (remainingArgs.size() != 1) {
        usage(null);
        return -1;
    }

    String command = remainingArgs.get(0);

    if ("start".equals(command)) {
        return startMaster();
    } else if ("stop".equals(command)) {
        return stopMaster();
    } else if ("clear".equals(command)) {
        return (ZNodeClearer.clear(getConf()) ? 0 : -1);
    } else {
        usage("Invalid command: " + command);
        return -1;
    }
}

From source file:com.fuerve.villageelder.client.commandline.commands.Command.java

/**
 * This method parses the command line and returns the results
 * in a {@link CommandLine} object.//from  w w  w . j a  v  a2 s  .c  o m
 * @param args The arguments to parse.
 * @return The {@link CommandLine} object containing the set
 * command line arguments.
 */
protected CommandLine parseCommandLine(final String[] args) {
    final CommandLineParser parser = new GnuParser();
    CommandLine result;

    try {
        result = parser.parse(options, args);
    } catch (ParseException e) {
        printUsage(new PrintWriter(System.out));
        throw new IllegalArgumentException(e);
    }

    return result;
}

From source file:basic.PartitionGraph.java

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

    options.addOption(new Option(RANGE, "use range partitioner"));

    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 nodes").create(NUM_NODES));
    options.addOption(OptionBuilder.withArgName("num").hasArg().withDescription("number of partitions")
            .create(NUM_PARTITIONS));

    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) || !cmdline.hasOption(NUM_NODES)
            || !cmdline.hasOption(NUM_PARTITIONS)) {
        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 inPath = cmdline.getOptionValue(INPUT);
    String outPath = cmdline.getOptionValue(OUTPUT);
    int nodeCount = Integer.parseInt(cmdline.getOptionValue(NUM_NODES));
    int numParts = Integer.parseInt(cmdline.getOptionValue(NUM_PARTITIONS));
    boolean useRange = cmdline.hasOption(RANGE);

    LOG.info("Tool name: " + PartitionGraph.class.getSimpleName());
    LOG.info(" - input dir: " + inPath);
    LOG.info(" - output dir: " + outPath);
    LOG.info(" - num partitions: " + numParts);
    LOG.info(" - node cnt: " + nodeCount);
    LOG.info(" - use range partitioner: " + useRange);

    Configuration conf = getConf();
    conf.setInt("NodeCount", nodeCount);

    Job job = Job.getInstance(conf);
    job.setJobName(PartitionGraph.class.getSimpleName() + ":" + inPath);
    job.setJarByClass(PartitionGraph.class);

    job.setNumReduceTasks(numParts);

    FileInputFormat.setInputPaths(job, new Path(inPath));
    FileOutputFormat.setOutputPath(job, new Path(outPath));

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

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

    job.setOutputKeyClass(IntWritable.class);
    job.setOutputValueClass(PageRankNode.class);

    if (useRange) {
        job.setPartitionerClass(RangePartitioner.class);
    }

    FileSystem.get(conf).delete(new Path(outPath), true);

    job.waitForCompletion(true);

    return 0;
}