Example usage for org.apache.commons.cli HelpFormatter printHelp

List of usage examples for org.apache.commons.cli HelpFormatter printHelp

Introduction

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

Prototype

public void printHelp(String cmdLineSyntax, Options options) 

Source Link

Document

Print the help for options with the specified command line syntax.

Usage

From source file:net.mybox.mybox.ClientSetup.java

/**
 * Handle command line arguments//  ww w  .  j  a v  a2s  .  c o  m
 * @param args
 */
public static void main(String[] args) {

    Options options = new Options();
    options.addOption("a", "apphome", true, "application home directory");
    options.addOption("h", "help", false, "show help screen");
    options.addOption("V", "version", false, "print the Mybox version");

    CommandLineParser line = new GnuParser();
    CommandLine cmd = null;

    String configDir = Client.defaultConfigDir;

    try {
        cmd = line.parse(options, args);
    } catch (Exception exp) {
        System.err.println(exp.getMessage());
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp(Client.class.getName(), options);
        return;
    }

    if (cmd.hasOption("h")) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp(Client.class.getName(), options);
        return;
    }

    if (cmd.hasOption("V")) {
        Client.printMessage("version " + Common.appVersion);
        return;
    }

    if (cmd.hasOption("a")) {
        String appHomeDir = cmd.getOptionValue("a");
        try {
            Common.updatePaths(appHomeDir);
        } catch (FileNotFoundException e) {
            Client.printErrorExit(e.getMessage());
        }

        Client.updatePaths();
    }

    ClientSetup setup = new ClientSetup();

}

From source file:SequentialPersonalizedPageRank.java

@SuppressWarnings({ "static-access" })
public static void main(String[] args) throws IOException {
    Options options = new Options();

    options.addOption(OptionBuilder.withArgName("path").hasArg().withDescription("input path").create(INPUT));
    options.addOption(// w ww.  j a  va2 s. c o  m
            OptionBuilder.withArgName("val").hasArg().withDescription("random jump factor").create(JUMP));
    options.addOption(OptionBuilder.withArgName("node").hasArg()
            .withDescription("source node (i.e., destination of the random jump)").create(SOURCE));

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

    String infile = cmdline.getOptionValue(INPUT);
    final String source = cmdline.getOptionValue(SOURCE);
    float alpha = cmdline.hasOption(JUMP) ? Float.parseFloat(cmdline.getOptionValue(JUMP)) : 0.15f;

    int edgeCnt = 0;
    DirectedSparseGraph<String, Integer> graph = new DirectedSparseGraph<String, Integer>();

    BufferedReader data = new BufferedReader(new InputStreamReader(new FileInputStream(infile)));

    String line;
    while ((line = data.readLine()) != null) {
        line.trim();
        String[] arr = line.split("\\t");

        for (int i = 1; i < arr.length; i++) {
            graph.addEdge(new Integer(edgeCnt++), arr[0], arr[i]);
        }
    }

    data.close();

    if (!graph.containsVertex(source)) {
        System.err.println("Error: source node not found in the graph!");
        System.exit(-1);
    }

    WeakComponentClusterer<String, Integer> clusterer = new WeakComponentClusterer<String, Integer>();

    Set<Set<String>> components = clusterer.transform(graph);
    int numComponents = components.size();
    System.out.println("Number of components: " + numComponents);
    System.out.println("Number of edges: " + graph.getEdgeCount());
    System.out.println("Number of nodes: " + graph.getVertexCount());
    System.out.println("Random jump factor: " + alpha);

    // Compute personalized PageRank.
    PageRankWithPriors<String, Integer> ranker = new PageRankWithPriors<String, Integer>(graph,
            new Transformer<String, Double>() {
                @Override
                public Double transform(String vertex) {
                    return vertex.equals(source) ? 1.0 : 0;
                }
            }, alpha);

    ranker.evaluate();

    // Use priority queue to sort vertices by PageRank values.
    PriorityQueue<Ranking<String>> q = new PriorityQueue<Ranking<String>>();
    int i = 0;
    for (String pmid : graph.getVertices()) {
        q.add(new Ranking<String>(i++, ranker.getVertexScore(pmid), pmid));
    }

    // Print PageRank values.
    System.out.println("\nPageRank of nodes, in descending order:");
    Ranking<String> r = null;
    while ((r = q.poll()) != null) {
        System.out.println(r.rankScore + "\t" + r.getRanked());
    }
}

From source file:ab.demo.MainEntry.java

public static void main(String args[]) {

    LoggingHandler.initConsoleLog();//from www  .jav a2s  . c  o m

    //args = new String[]{"-su"};
    Options options = new Options();
    options.addOption("s", "standalone", false, "runs the reinforcement learning agent in standalone mode");
    options.addOption("p", "proxyPort", true, "the port which is to be used by the proxy");
    options.addOption("h", "help", false, "displays this help");
    options.addOption("n", "naiveAgent", false, "runs the naive agent in standalone mode");
    options.addOption("c", "competition", false, "runs the naive agent in the server/client competition mode");
    options.addOption("u", "updateDatabaseTables", false, "executes CREATE TABLE IF NOT EXIST commands");
    options.addOption("l", "level", true, "if set the agent is playing only in this one level");
    options.addOption("m", "manual", false,
            "runs the empirical threshold determination agent in standalone mode");
    options.addOption("r", "real", false, "shows the recognized shapes in a new frame");

    CommandLineParser parser = new DefaultParser();
    CommandLine cmd;
    StandaloneAgent agent;

    Properties properties = new Properties();
    InputStream configInputStream = null;

    try {
        Class.forName("org.sqlite.JDBC");
        //parse configuration file
        configInputStream = new FileInputStream("config.properties");

        properties.load(configInputStream);

    } catch (IOException exception) {
        exception.printStackTrace();
    } catch (ClassNotFoundException exception) {
        exception.printStackTrace();
    } finally {
        if (configInputStream != null) {
            try {
                configInputStream.close();
            } catch (IOException exception) {
                exception.printStackTrace();
            }
        }
    }

    String dbPath = properties.getProperty("db_path");
    String dbUser = properties.getProperty("db_user");
    String dbPass = properties.getProperty("db_pass");
    DBI dbi = new DBI(dbPath, dbUser, dbPass);

    QValuesDAO qValuesDAO = dbi.open(QValuesDAO.class);
    GamesDAO gamesDAO = dbi.open(GamesDAO.class);
    MovesDAO movesDAO = dbi.open(MovesDAO.class);
    ProblemStatesDAO problemStatesDAO = dbi.open(ProblemStatesDAO.class);

    try {
        cmd = parser.parse(options, args);
        if (cmd.hasOption("help")) {
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp("help", options);
            return;
        }

        int proxyPort = 9000;
        if (cmd.hasOption("proxyPort")) {
            proxyPort = Integer.parseInt(cmd.getOptionValue("proxyPort"));
            logger.info("Set proxy port to " + proxyPort);
        }
        Proxy.setPort(proxyPort);

        LoggingHandler.initFileLog();

        if (cmd.hasOption("standalone")) {
            agent = new ReinforcementLearningAgent(gamesDAO, movesDAO, problemStatesDAO, qValuesDAO);
        } else if (cmd.hasOption("naiveAgent")) {
            agent = new NaiveStandaloneAgent();
        } else if (cmd.hasOption("manual")) {
            agent = new ManualGamePlayAgent(gamesDAO, movesDAO, problemStatesDAO);
        } else if (cmd.hasOption("competition")) {
            System.out.println("We haven't implemented a competition ready agent yet.");
            return;
        } else {
            System.out.println("Please specify which solving strategy we should be using.");
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp("help", options);
            return;
        }

        if (cmd.hasOption("updateDatabaseTables")) {
            qValuesDAO.createTable();
            gamesDAO.createTable();
            movesDAO.createTable();
            problemStatesDAO.createTable();
            problemStatesDAO.createObjectsTable();
        }

        if (cmd.hasOption("level")) {
            agent.setFixedLevel(Integer.parseInt(cmd.getOptionValue("level")));
        }

        if (cmd.hasOption("real")) {
            ShowSeg.useRealshape = true;
            Thread thread = new Thread(new ShowSeg());
            thread.start();
        }

    } catch (UnrecognizedOptionException e) {
        System.out.println("Unrecognized commandline option: " + e.getOption());
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("help", options);
        return;
    } catch (ParseException e) {
        System.out.println(
                "There was an error while parsing your command line input. Did you rechecked your syntax before running?");
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("help", options);
        return;
    }

    agent.run();
}

From source file:edu.umd.shrawanraina.SequentialPersonalizedPageRank.java

@SuppressWarnings({ "static-access" })
public static void main(String[] args) throws IOException {
    Options options = new Options();

    options.addOption(OptionBuilder.withArgName("path").hasArg().withDescription("input path").create(INPUT));
    options.addOption(//ww  w .  j a v  a  2  s  .  c o m
            OptionBuilder.withArgName("val").hasArg().withDescription("random jump factor").create(JUMP));
    options.addOption(OptionBuilder.withArgName("node").hasArg()
            .withDescription("source node (i.e., destination of the random jump)").create(SOURCE));

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

    String infile = cmdline.getOptionValue(INPUT);
    final String source = cmdline.getOptionValue(SOURCE);
    float alpha = cmdline.hasOption(JUMP) ? Float.parseFloat(cmdline.getOptionValue(JUMP)) : 0.15f;

    int edgeCnt = 0;
    DirectedSparseGraph<String, Integer> graph = new DirectedSparseGraph<String, Integer>();

    BufferedReader data = new BufferedReader(new InputStreamReader(new FileInputStream(infile)));

    String line;
    while ((line = data.readLine()) != null) {
        line.trim();
        String[] arr = line.split("\\t");

        for (int i = 1; i < arr.length; i++) {
            graph.addEdge(new Integer(edgeCnt++), arr[0], arr[i]);
        }
    }

    data.close();

    if (!graph.containsVertex(source)) {
        System.err.println("Error: source node not found in the graph!");
        System.exit(-1);
    }

    WeakComponentClusterer<String, Integer> clusterer = new WeakComponentClusterer<String, Integer>();

    Set<Set<String>> components = clusterer.transform(graph);
    int numComponents = components.size();
    System.out.println("Number of components: " + numComponents);
    System.out.println("Number of edges: " + graph.getEdgeCount());
    System.out.println("Number of nodes: " + graph.getVertexCount());
    System.out.println("Random jump factor: " + alpha);

    // Compute personalized PageRank.
    PageRankWithPriors<String, Integer> ranker = new PageRankWithPriors<String, Integer>(graph,
            new Transformer<String, Double>() {
                public Double transform(String vertex) {
                    return vertex.equals(source) ? 1.0 : 0;
                }
            }, alpha);

    ranker.evaluate();

    // Use priority queue to sort vertices by PageRank values.
    PriorityQueue<Ranking<String>> q = new PriorityQueue<Ranking<String>>();
    int i = 0;
    for (String pmid : graph.getVertices()) {
        q.add(new Ranking<String>(i++, ranker.getVertexScore(pmid), pmid));
    }

    // Print PageRank values.
    System.out.println("\nPageRank of nodes, in descending order:");
    Ranking<String> r = null;
    while ((r = q.poll()) != null) {
        System.out.println(r.rankScore + "\t" + r.getRanked());
    }
}

From source file:de.prozesskraft.pkraft.Checkconsistency.java

public static void main(String[] args) throws org.apache.commons.cli.ParseException, IOException {

    /*----------------------------
      get options from ini-file// w ww  . j a  va 2 s .c o m
    ----------------------------*/
    File inifile = new java.io.File(WhereAmI.getInstallDirectoryAbsolutePath(Checkconsistency.class) + "/"
            + "../etc/pkraft-checkconsistency.ini");

    if (inifile.exists()) {
        try {
            ini = new Ini(inifile);
        } catch (InvalidFileFormatException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    } else {
        System.err.println("ini file does not exist: " + inifile.getAbsolutePath());
        System.exit(1);
    }

    /*----------------------------
      create boolean options
    ----------------------------*/
    Option ohelp = new Option("help", "print this message");
    Option ov = new Option("v", "prints version and build-date");

    /*----------------------------
      create argument options
    ----------------------------*/
    Option odefinition = OptionBuilder.withArgName("definition").hasArg()
            .withDescription("[mandatory] process model in xml format.")
            //            .isRequired()
            .create("definition");

    /*----------------------------
      create options object
    ----------------------------*/
    Options options = new Options();

    options.addOption(ohelp);
    options.addOption(ov);
    options.addOption(odefinition);

    /*----------------------------
      create the parser
    ----------------------------*/
    CommandLineParser parser = new GnuParser();
    // parse the command line arguments
    commandline = parser.parse(options, args);

    /*----------------------------
      usage/help
    ----------------------------*/
    if (commandline.hasOption("help")) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("startinstance", options);
        System.exit(0);
    }

    if (commandline.hasOption("v")) {
        System.out.println("author:  alexander.vogel@prozesskraft.de");
        System.out.println("version: [% version %]");
        System.out.println("date:    [% date %]");
        System.exit(0);
    }
    /*----------------------------
      ueberpruefen ob eine schlechte kombination von parametern angegeben wurde
    ----------------------------*/
    if (!(commandline.hasOption("definition"))) {
        System.out.println("option -definition is mandatory.");
        exiter();
    }

    /*----------------------------
      die lizenz ueberpruefen und ggf abbrechen
    ----------------------------*/

    // check for valid license
    ArrayList<String> allPortAtHost = new ArrayList<String>();
    allPortAtHost.add(ini.get("license-server", "license-server-1"));
    allPortAtHost.add(ini.get("license-server", "license-server-2"));
    allPortAtHost.add(ini.get("license-server", "license-server-3"));

    MyLicense lic = new MyLicense(allPortAtHost, "1", "user-edition", "0.1");

    // lizenz-logging ausgeben
    for (String actLine : (ArrayList<String>) lic.getLog()) {
        System.err.println(actLine);
    }

    // abbruch, wenn lizenz nicht valide
    if (!lic.isValid()) {
        System.exit(1);
    }

    /*----------------------------
      die eigentliche business logic
    ----------------------------*/

    Process p1 = new Process();

    p1.setInfilexml(commandline.getOptionValue("definition"));
    Process p2;
    try {
        p2 = p1.readXml();

        if (p2.isProcessConsistent()) {
            System.out.println("process structure is consistent.");
        } else {
            System.out.println("process structure is NOT consistent.");
        }

        p2.printLog();

    } catch (JAXBException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
}

From source file:net.sourceforge.dita4publishers.tools.mapreporter.MapBosReporter.java

/**
 * @param args//from   ww w  .  j  av a  2 s. c  om
 */
public static void main(String[] args) {

    Options cmdlineOptions = configureOptions();

    CommandLineParser parser = new PosixParser();

    CommandLine cmdline = null;
    try {
        // parse the command line arguments
        cmdline = parser.parse(cmdlineOptions, args);
    } catch (ParseException exp) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp(MapBosReporter.class.getSimpleName(), cmdlineOptions);
        System.exit(-1);
    }

    if (!cmdline.hasOption(INPUT_OPTION_ONE_CHAR)) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp(MapBosReporter.class.getSimpleName(), cmdlineOptions);
        System.exit(-1);
    }

    MapBosReporter app = new MapBosReporter(cmdline);
    try {
        app.run();
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
    }

}

From source file:gov.lanl.adore.djatoka.DjatokaCompress.java

/**
 * Uses apache commons cli to parse input args. Passes parsed
 * parameters to ICompress implementation.
 * @param args command line parameters to defined input,output,etc.
 *//*from ww w  .j av a2 s. c o m*/
public static void main(String[] args) {
    // create the command line parser
    CommandLineParser parser = new PosixParser();

    // create the Options
    Options options = new Options();
    options.addOption("i", "input", true, "Filepath of the input file or dir.");
    options.addOption("o", "output", true, "Filepath of the output file or dir.");
    options.addOption("r", "rate", true, "Absolute Compression Ratio");
    options.addOption("s", "slope", true,
            "Used to generate relative compression ratio based on content characteristics.");
    options.addOption("y", "Clayers", true, "Number of quality levels.");
    options.addOption("l", "Clevels", true, "Number of DWT levels (reolution levels).");
    options.addOption("v", "Creversible", true, "Use Reversible Wavelet");
    options.addOption("c", "Cprecincts", true, "Precinct dimensions");
    options.addOption("p", "props", true, "Compression Properties File");
    options.addOption("d", "Corder", true, "Progression order");
    options.addOption("g", "ORGgen_plt", true, "Enables insertion of packet length information in the header");
    options.addOption("t", "ORGtparts", true, "Division of each tile's packets into tile-parts");
    options.addOption("b", "Cblk", true, "Codeblock Size");
    options.addOption("a", "AltImpl", true, "Alternate ICompress Implemenation");

    try {
        if (args.length == 0) {
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp("gov.lanl.adore.djatoka.DjatokaCompress", options);
            System.exit(0);
        }

        // parse the command line arguments
        CommandLine line = parser.parse(options, args);
        String input = line.getOptionValue("i");
        String output = line.getOptionValue("o");

        String propsFile = line.getOptionValue("p");
        DjatokaEncodeParam p;
        if (propsFile != null) {
            Properties props = IOUtils.loadConfigByPath(propsFile);
            p = new DjatokaEncodeParam(props);
        } else
            p = new DjatokaEncodeParam();
        String rate = line.getOptionValue("r");
        if (rate != null)
            p.setRate(rate);
        String slope = line.getOptionValue("s");
        if (slope != null)
            p.setSlope(slope);
        String Clayers = line.getOptionValue("y");
        if (Clayers != null)
            p.setLayers(Integer.parseInt(Clayers));
        String Clevels = line.getOptionValue("l");
        if (Clevels != null)
            p.setLevels(Integer.parseInt(Clevels));
        String Creversible = line.getOptionValue("v");
        if (Creversible != null)
            p.setUseReversible(Boolean.parseBoolean(Creversible));
        String Cprecincts = line.getOptionValue("c");
        if (Cprecincts != null)
            p.setPrecincts(Cprecincts);
        String Corder = line.getOptionValue("d");
        if (Corder != null)
            p.setProgressionOrder(Corder);
        String ORGgen_plt = line.getOptionValue("g");
        if (ORGgen_plt != null)
            p.setInsertPLT(Boolean.parseBoolean(ORGgen_plt));
        String Cblk = line.getOptionValue("b");
        if (Cblk != null)
            p.setCodeBlockSize(Cblk);
        String alt = line.getOptionValue("a");

        ICompress jp2 = new KduCompressExe();
        if (alt != null)
            jp2 = (ICompress) Class.forName(alt).newInstance();
        if (new File(input).isDirectory() && new File(output).isDirectory()) {
            ArrayList<File> files = IOUtils.getFileList(input, new SourceImageFileFilter(), false);
            for (File f : files) {
                long x = System.currentTimeMillis();
                File outFile = new File(output, f.getName().substring(0, f.getName().indexOf(".")) + ".jp2");
                compress(jp2, f.getAbsolutePath(), outFile.getAbsolutePath(), p);
                report(f.getAbsolutePath(), x);
            }
        } else {
            long x = System.currentTimeMillis();
            File f = new File(input);
            if (output == null)
                output = f.getName().substring(0, f.getName().indexOf(".")) + ".jp2";
            if (new File(output).isDirectory())
                output = output + f.getName().substring(0, f.getName().indexOf(".")) + ".jp2";
            compress(jp2, input, output, p);
            report(input, x);
        }
    } catch (ParseException e) {
        logger.error("Parse exception:" + e.getMessage(), e);
    } catch (DjatokaException e) {
        logger.error("djatoka Compression exception:" + e.getMessage(), e);
    } catch (InstantiationException e) {
        logger.error("Unable to initialize alternate implemenation:" + e.getMessage(), e);
    } catch (Exception e) {
        logger.error("An exception occured:" + e.getMessage(), e);
    }
}

From source file:com.galois.fiveui.HeadlessRunner.java

/**
 * @param args list of headless run description filenames
 * @throws IOException/* w  w w.  j  a  va2  s . c  om*/
 * @throws URISyntaxException
 * @throws ParseException
 */
@SuppressWarnings("static-access")
public static void main(final String[] args) throws IOException, URISyntaxException, ParseException {

    // Setup command line options
    Options options = new Options();
    Option help = new Option("h", "print this help message");
    Option output = OptionBuilder.withArgName("outfile").hasArg().withDescription("write output to file")
            .create("o");
    Option report = OptionBuilder.withArgName("report directory").hasArg()
            .withDescription("write HTML reports to given directory").create("r");
    options.addOption(output);
    options.addOption(report);
    options.addOption("v", false, "verbose output");
    options.addOption("vv", false, "VERY verbose output");
    options.addOption(help);

    // Parse command line options
    CommandLineParser parser = new GnuParser();
    CommandLine cmd = null;
    try {
        cmd = parser.parse(options, args);
    } catch (ParseException e) {
        System.err.println("Command line option parsing failed.  Reason: " + e.getMessage());
        System.exit(1);
    }

    // Display help if requested
    if (cmd.hasOption("h")) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("headless <input file 1> [<input file 2> ...]", options);
        System.exit(1);
    }

    // Set logging levels
    BasicConfigurator.configure();
    Logger fiveuiLogger = Logger.getLogger("com.galois.fiveui");
    Logger rootLogger = Logger.getRootLogger();
    if (cmd.hasOption("v")) {
        fiveuiLogger.setLevel(Level.DEBUG);
        rootLogger.setLevel(Level.ERROR);
    } else if (cmd.hasOption("vv")) {
        fiveuiLogger.setLevel(Level.DEBUG);
        rootLogger.setLevel(Level.DEBUG);
    } else {
        fiveuiLogger.setLevel(Level.ERROR);
        rootLogger.setLevel(Level.ERROR);
    }

    // Setup output file if requested
    PrintWriter outStream = null;
    if (cmd.hasOption("o")) {
        String outfile = cmd.getOptionValue("o");
        try {
            outStream = new PrintWriter(new BufferedWriter(new FileWriter(outfile)));
        } catch (IOException e) {
            System.err.println("Could not open outfile for writing: " + cmd.getOptionValue("outfile"));
            System.exit(1);
        }
    } else {
        outStream = new PrintWriter(new BufferedWriter(new PrintWriter(System.out)));
    }

    // Setup HTML reports directory before the major work happens in case we
    // have to throw an exception.
    PrintWriter summaryFile = null;
    PrintWriter byURLFile = null;
    PrintWriter byRuleFile = null;
    if (cmd.hasOption("r")) {
        String repDir = cmd.getOptionValue("r");
        try {
            File file = new File(repDir);
            if (!file.exists()) {
                file.mkdir();
                logger.info("report directory created: " + repDir);
            } else {
                logger.info("report directory already exists!");
            }
            summaryFile = new PrintWriter(new FileWriter(repDir + File.separator + "summary.html"));
            byURLFile = new PrintWriter(new FileWriter(repDir + File.separator + "byURL.html"));
            byRuleFile = new PrintWriter(new FileWriter(repDir + File.separator + "byRule.html"));
        } catch (IOException e) {
            System.err.println("could not open report directory / files for writing");
            System.exit(1);
        }
    }

    // Major work: process input files
    ImmutableList<Result> results = null;
    for (String in : cmd.getArgs()) {
        HeadlessRunDescription descr = HeadlessRunDescription.parse(in);
        logger.debug("invoking headless run...");
        BatchRunner runner = new BatchRunner();
        results = runner.runHeadless(descr);
        logger.debug("runHeadless returned " + results.size() + " results");
        // write results to the output stream as we go
        for (Result result : results) {
            outStream.println(result.toString());
        }
        outStream.flush();
    }
    outStream.close();

    // Write report files if requested
    if (cmd.hasOption("r") && results != null) {
        Reporter kermit = new Reporter(results);
        summaryFile.write(kermit.getSummary());
        summaryFile.close();
        byURLFile.write(kermit.getByURL());
        byURLFile.close();
        byRuleFile.write(kermit.getByRule());
        byRuleFile.close();
    }
}

From source file:com.flaptor.hounder.indexer.RmiIndexerStub.java

public static void main(String[] args) {

    // create the parser
    CommandLineParser parser = new PosixParser();
    CommandLine line = null;/*from w w w.  j a  v  a  2 s  .c  o  m*/
    Options options = getOptions();
    try {
        // parse the command line arguments
        line = parser.parse(options, args);
    } catch (ParseException exp) {
        // oops, something went wrong
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("RmiIndexerStub -h <hostName> -p <basePort> [options] ", options);
        System.exit(1);
    }

    boolean doOptimize = line.hasOption("optimize");
    boolean doCheckpoint = line.hasOption("checkpoint");
    boolean doStop = line.hasOption("stop");
    Integer port = ((Long) line.getOptionObject("port")).intValue();
    String host = line.getOptionValue("host");

    try {
        RmiIndexerStub stub = new RmiIndexerStub(port, host);

        if (line.hasOption("deleteUrl")) {
            String url = line.getOptionValue("deleteUrl");
            Document dom = generateDeleteDocument(url);
            indexOrFail(stub, dom, "Could not delete " + url);
            System.out.println("delete " + url + " command accepted by indexer");
        }

        if (line.hasOption("deleteFile")) {

            BufferedReader reader = new BufferedReader(new FileReader(line.getOptionValue("deleteFile")));
            while (reader.ready()) {
                String url = reader.readLine();
                if (url.length() > 0 && url.charAt(0) != '#') { // ignore empty lines and comments
                    Document dom = generateDeleteDocument(url);
                    indexOrFail(stub, dom, "Could not delete " + url);
                    System.out.println("delete " + url + " command accepted by indexer");
                }
            }
            reader.close();
        }

        if (doOptimize) {
            Document dom = generateCommandDocument("optimize");
            indexOrFail(stub, dom, "Could not send optimize command.");
            System.out.println("optimize command accepted by indexer");
        }

        if (doCheckpoint) {
            Document dom = generateCommandDocument("checkpoint");
            indexOrFail(stub, dom, "Could not send checkpoint command.");
            System.out.println("checkpoint command accepted by indexer");

        }
        if (doStop) {
            Document dom = generateCommandDocument("close");
            indexOrFail(stub, dom, "Could not send stop command.");
            System.out.println("stop command accepted by indexer");
        }
    } catch (Exception e) {
        System.err.println("An error occurred: " + e.getMessage());
        e.printStackTrace();
    }
}

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

@SuppressWarnings("static-access")
public static void main(String[] args) throws Exception {
    Options options = new Options();
    options.addOption(OptionBuilder.withArgName("path").hasArg().withDescription("bz2 Wikipedia XML dump file")
            .create(INPUT_OPTION));//from w  w w.j  ava 2 s.c o m
    options.addOption(
            OptionBuilder.withArgName("dir").hasArg().withDescription("index location").create(INDEX_OPTION));
    options.addOption(OptionBuilder.withArgName("num").hasArg()
            .withDescription("maximum number of documents to index").create(MAX_OPTION));
    options.addOption(OptionBuilder.withArgName("num").hasArg().withDescription("number of indexing threads")
            .create(THREADS_OPTION));

    options.addOption(new Option(OPTIMIZE_OPTION, "merge indexes into a single segment"));

    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(INPUT_OPTION) || !cmdline.hasOption(INDEX_OPTION)) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp(BuildWarcTrecIdMapping.class.getCanonicalName(), options);
        System.exit(-1);
    }

    String indexPath = cmdline.getOptionValue(INDEX_OPTION);
    int maxdocs = cmdline.hasOption(MAX_OPTION) ? Integer.parseInt(cmdline.getOptionValue(MAX_OPTION))
            : Integer.MAX_VALUE;
    int threads = cmdline.hasOption(THREADS_OPTION) ? Integer.parseInt(cmdline.getOptionValue(THREADS_OPTION))
            : DEFAULT_NUM_THREADS;

    long startTime = System.currentTimeMillis();

    String path = cmdline.getOptionValue(INPUT_OPTION);
    PrintStream out = new PrintStream(System.out, true, "UTF-8");

    Directory dir = FSDirectory.open(new File(indexPath));
    IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_43, ANALYZER);
    config.setOpenMode(OpenMode.CREATE);

    IndexWriter writer = new IndexWriter(dir, config);
    LOG.info("Creating index at " + indexPath);
    LOG.info("Indexing with " + threads + " threads");

    FileInputStream fis = null;
    BufferedReader br = null;

    try {
        fis = new FileInputStream(new File(path));
        byte[] ignoreBytes = new byte[2];
        fis.read(ignoreBytes); // "B", "Z" bytes from commandline tools
        br = new BufferedReader(new InputStreamReader(new CBZip2InputStream(fis), "UTF8"));

        ExecutorService executor = Executors.newFixedThreadPool(threads);
        int cnt = 0;
        String s;
        while ((s = br.readLine()) != null) {
            Runnable worker = new AddDocumentRunnable(writer, s);
            executor.execute(worker);

            cnt++;
            if (cnt % 1000000 == 0) {
                LOG.info(cnt + " articles added");
            }
            if (cnt >= maxdocs) {
                break;
            }
        }

        executor.shutdown();
        // Wait until all threads are finish
        while (!executor.isTerminated()) {
        }

        LOG.info("Total of " + cnt + " articles indexed.");

        if (cmdline.hasOption(OPTIMIZE_OPTION)) {
            LOG.info("Merging segments...");
            writer.forceMerge(1);
            LOG.info("Done!");
        }

        LOG.info("Total elapsed time: " + (System.currentTimeMillis() - startTime) + "ms");
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        writer.close();
        dir.close();
        out.close();
        br.close();
        fis.close();
    }
}