Home | History | Annotate | Download | only in index
      1 /*
      2  * CDDL HEADER START
      3  *
      4  * The contents of this file are subject to the terms of the
      5  * Common Development and Distribution License (the "License").
      6  * You may not use this file except in compliance with the License.
      7  *
      8  * See LICENSE.txt included in this distribution for the specific
      9  * language governing permissions and limitations under the License.
     10  *
     11  * When distributing Covered Code, include this CDDL HEADER in each
     12  * file and include the License file at LICENSE.txt.
     13  * If applicable, add the following below this CDDL HEADER, with the
     14  * fields enclosed by brackets "[]" replaced with your own identifying
     15  * information: Portions Copyright [yyyy] [name of copyright owner]
     16  *
     17  * CDDL HEADER END
     18  */
     19 
     20 /*
     21  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
     22  * Use is subject to license terms.
     23  */
     24 package org.opensolaris.opengrok.index;
     25 
     26 import java.io.BufferedReader;
     27 import java.io.IOException;
     28 import java.io.InputStreamReader;
     29 import java.io.PrintWriter;
     30 import java.io.StringWriter;
     31 import java.text.DateFormat;
     32 import java.util.ArrayList;
     33 import java.util.Date;
     34 import java.util.Iterator;
     35 import java.util.List;
     36 
     37 public class CommandLineOptions {
     38 
     39     private final static String ON_OFF = "on/off";
     40 
     41     static class Option {
     42 
     43         char option;
     44         String argument;
     45         String description;
     46 
     47         public Option(char opt, String arg, String descr) {
     48             option = opt;
     49             argument = arg;
     50             description = descr;
     51         }
     52 
     53         public String getUsage() {
     54             StringBuilder sb = new StringBuilder();
     55             sb.append('-');
     56             sb.append(option);
     57             if (argument != null) {
     58                 sb.append(' ');
     59                 sb.append(argument);
     60             }
     61             sb.append("\n\t");
     62             sb.append(description);
     63 
     64             return sb.toString();
     65         }
     66     }
     67     private final List<Option> options;
     68 
     69     public CommandLineOptions() {
     70         options = new ArrayList<Option>();
     71         options.add(new Option('q', null, "Run as quietly as possible"));
     72         options.add(new Option('v', null, "Print progress information as we go along"));
     73         options.add(new Option('e', null, "Economical - consumes less disk space. It does not generate hyper text cross reference files offline, but will do so on demand - which could be sightly slow."));
     74         options.add(new Option('c', "/path/to/ctags", "Path to Exuberant Ctags from http://ctags.sf.net by default takes the Exuberant Ctags in PATH."));
     75         options.add(new Option('R', "/path/to/configuration", "Read configuration from the specified file"));
     76         options.add(new Option('W', "/path/to/configuration", "Write the current configuration to the specified file (so that the web application can use the same configuration"));
     77         options.add(new Option('U', "host:port", "Send the current configuration to the specified address (This is most likely the web-app configured with ConfigAddress)"));
     78         options.add(new Option('P', null, "Generate a project for each of the top-level directories in source root"));
     79         options.add(new Option('p', "/path/to/default/project", "This is the path to the project that should be selected by default in the web application(when no other project set either in cookie or in parameter). You should strip off the source root."));
     80         options.add(new Option('Q', ON_OFF, "Turn on/off quick context scan. By default only the first 32k of a file is scanned, and a '[..all..]' link is inserted if the file is bigger. Activating this may slow the server down (Note: this is setting only affects the web application)"));
     81         options.add(new Option('n', null, "Do not generate indexes, but process all other command line options"));
     82         options.add(new Option('H', null, "Generate history cache for all external repositories"));
     83         options.add(new Option('h', "/path/to/repository", "Generate history cache for the specified repos (absolute path from source root)"));
     84         options.add(new Option('D', null, "Store history cache in a database (needs the JDBC driver in the classpath, typically derbyclient.jar or derby.jar)"));
     85         options.add(new Option('j', "class", "Name of the JDBC driver class used by the history cache. Can use one of the shorthands \"client\" (org.apache.derby.jdbc.ClientDriver) or \"embedded\" (org.apache.derby.jdbc.EmbeddedDriver). Default: \"client\""));
     86         options.add(new Option('u', "url", "URL to the database that contains the history cache. Default: If -j specifies \"embedded\", \"jdbc:derby:$DATA_ROOT/cachedb;create=true\"; otherwise, \"jdbc:derby://localhost/cachedb;create=true\""));
     87         options.add(new Option('r', ON_OFF, "Turn on/off support for remote SCM systems"));
     88         options.add(new Option('L', "path", "Path to the subdirectory in the web-application containing the requested stylesheet. The following factory-defaults exist: \"default\", \"offwhite\" and \"polished\""));
     89         options.add(new Option('l', ON_OFF, "Turn on/off locking of the Lucene database during index generation"));
     90         options.add(new Option('O', ON_OFF, "Turn on/off the optimization of the index database as part of the indexing step"));
     91         options.add(new Option('a', ON_OFF, "Allow or disallow leading wildcards in a search"));
     92         options.add(new Option('w', "webapp-context", "Context of webapp. Default is /source. If you specify a different name, make sure to rename source.war to that name."));
     93         options.add(new Option('i', "pattern", "Ignore the named files or directories"));
     94         options.add(new Option('A', "ext:analyzer", "Files with the named extension should be analyzed with the specified class"));
     95         options.add(new Option('m', "number", "The maximum words to index in a file"));
     96         options.add(new Option('S', null, "Search for \"external\" source repositories and add them"));
     97         options.add(new Option('s', "/path/to/source/root", "The root directory of the source tree"));
     98         options.add(new Option('d', "/path/to/data/root", "The directory where OpenGrok stores the generated data"));
     99         options.add(new Option('T', "number", "The number of threads to use for index generation. By default the number of threads will be set to the number of available CPUs"));
    100         options.add(new Option('?', null, "Help"));
    101         options.add(new Option('V', null, "Print version and quit"));
    102     }
    103 
    104     public String getCommandString() {
    105         StringBuilder sb = new StringBuilder();
    106         for (Option o : options) {
    107             sb.append(o.option);
    108             if (o.argument != null) {
    109                 sb.append(':');
    110             }
    111         }
    112         return sb.toString();
    113     }
    114 
    115     public String getCommandUsage(char c) {
    116         for (Option o : options) {
    117             if (o.option == c) {
    118                 return o.getUsage();
    119             }
    120         }
    121 
    122         return null;
    123     }
    124 
    125     private void spool(BufferedReader reader, PrintWriter out, String tag) throws IOException {
    126         String line;
    127         while ((line = reader.readLine()) != null) {
    128             if (line.equals(tag)) {
    129                 return;
    130             }
    131             out.println(line);
    132         }
    133     }
    134 
    135     public String getUsage() {
    136         StringWriter wrt = new StringWriter();
    137         PrintWriter out = new PrintWriter(wrt);
    138 
    139         out.println("Usage: opengrok.jar [options]");
    140         for (Option o : options) {
    141             out.println(o.getUsage());
    142         }
    143 
    144         out.flush();
    145         out.close();
    146 
    147         return wrt.toString();
    148     }
    149 
    150     public String getManPage() throws IOException {
    151         StringWriter wrt = new StringWriter();
    152         PrintWriter out = new PrintWriter(wrt);
    153 
    154         BufferedReader reader = new BufferedReader(new InputStreamReader(
    155                 CommandLineOptions.class.getResourceAsStream("opengrok.xml"), "US-ASCII"));
    156 
    157         spool(reader, out, "___INSERT_DATE___");
    158         out.print("<refmiscinfo class=\"date\">");
    159         out.print(DateFormat.getDateInstance(DateFormat.MEDIUM).format(new Date()));
    160         out.println("</refmiscinfo>");
    161 
    162         spool(reader, out, "___INSERT_USAGE___");
    163         for (Option o : options) {
    164             out.println("<optional><option>");
    165             out.print(o.option);
    166             if (o.argument != null) {
    167                 out.print(" <replaceable>");
    168                 out.print(o.argument);
    169                 out.print("</replaceable>");
    170             }
    171             out.println("</option></optional>");
    172         }
    173 
    174         spool(reader, out, "___INSERT_OPTIONS___");
    175         for (Option o : options) {
    176             out.print("<varlistentry><term><option>");
    177             out.print(o.option);
    178             out.print("</option></term><listitem><para>");
    179             out.print(o.description);
    180             out.println("</para></listitem></varlistentry>");
    181         }
    182 
    183         spool(reader, out, "___END_OF_FILE___");
    184         out.flush();
    185         reader.close();
    186 
    187         return wrt.toString();
    188     }
    189 
    190     /**
    191      * Not intended for normal use, but for the JUnit test suite to validate
    192      * that all options contains a description :-)
    193      *
    194      * @return an iterator to iterate through all of the command line options
    195      */
    196     Iterator<Option> getOptionsIterator() {
    197         return options.iterator();
    198     }
    199 
    200    /**
    201     * Print out a manual page on standard out. Used for building manual page.
    202     *
    203     * @param argv argument vector. not used.
    204     */
    205    @SuppressWarnings("PMD.SystemPrintln")
    206    public static void main(String[] argv) {
    207        CommandLineOptions co = new CommandLineOptions();
    208        try {
    209            System.out.println(co.getManPage());
    210        } catch (IOException exp) {
    211            exp.printStackTrace(System.err);
    212            System.exit(1);
    213        }
    214    }
    215 }
    216