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.List;
     35 
     36 public class CommandLineOptions {
     37 
     38     class Option {
     39 
     40         char option;
     41         String argument;
     42         String description;
     43 
     44         public Option(char opt, String arg, String descr) {
     45             option = opt;
     46             argument = arg;
     47             description = descr;
     48         }
     49 
     50         public String getUsage() {
     51             StringBuilder sb = new StringBuilder();
     52             sb.append('-');
     53             sb.append(option);
     54             if (argument != null) {
     55                 sb.append(' ');
     56                 sb.append(argument);
     57             }
     58             sb.append("\n\t");
     59             sb.append(description);
     60 
     61             return sb.toString();
     62         }
     63     }
     64     private List<Option> options;
     65 
     66     public CommandLineOptions() {
     67         options = new ArrayList<Option>();
     68         options.add(new Option('q', null, "Run as quietly as possible"));
     69         options.add(new Option('v', null, "Print progress information as we go along"));
     70         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."));
     71         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."));
     72         options.add(new Option('R', "/path/to/configuration", "Read configuration from the specified file"));
     73         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"));
     74         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)"));
     75         options.add(new Option('P', null, "Generate a project for each of the top-level directories in source root"));
     76         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. You should strip off the source root."));
     77         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)"));
     78         options.add(new Option('n', null, "Do not generate indexes, but process all other command line options"));
     79         options.add(new Option('H', null, "Generate history cache for all external repositories"));
     80         options.add(new Option('h', "/path/to/repository", "Generate history cache for the specified repos (absolute path from source root)"));
     81         options.add(new Option('r', "on/off", "Turn on/off support for remote SCM systems"));
     82         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\""));
     83         options.add(new Option('O', "on/off", "Turn on/off the optimization of the index database as part of the indexing step"));
     84         options.add(new Option('a', "on/off", "Allow or disallow leading wildcards in a search"));
     85         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."));
     86         options.add(new Option('i', "pattern", "Ignore the named files or directories"));
     87         options.add(new Option('A', "ext:analyzer", "Files with the named extension should be analyzed with the specified class"));
     88         options.add(new Option('m', "number", "The maximum words to index in a file"));
     89         options.add(new Option('S', null, "Search for \"external\" source repositories and add them"));
     90         options.add(new Option('s', "/path/to/source/root", "The root directory of the source tree"));
     91         options.add(new Option('d', "/path/to/data/root", "The directory where OpenGrok stores the generated data"));
     92         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"));
     93         options.add(new Option('?', null, "Help"));
     94     }
     95 
     96     public String getCommandString() {
     97         StringBuilder sb = new StringBuilder();
     98         for (Option o : options) {
     99             sb.append(o.option);
    100             if (o.argument != null) {
    101                 sb.append(':');
    102             }
    103         }
    104         return sb.toString();
    105     }
    106 
    107     public String getCommandUsage(char c) {
    108         for (Option o : options) {
    109             if (o.option == c) {
    110                 return o.getUsage();
    111             }
    112         }
    113 
    114         return null;
    115     }
    116 
    117     private void spool(BufferedReader reader, PrintWriter out, String tag) throws IOException {
    118         String line;
    119         while ((line = reader.readLine()) != null) {
    120             if (line.equals(tag)) {
    121                 return;
    122             }
    123             out.println(line);
    124         }
    125     }
    126 
    127     public String getUsage() {
    128         StringWriter wrt = new StringWriter();
    129         PrintWriter out = new PrintWriter(wrt);
    130 
    131         out.println("Usage: opengrok.jar [options]");
    132         for (Option o : options) {
    133             out.println(o.getUsage());
    134         }
    135 
    136         out.flush();
    137         out.close();
    138 
    139         return wrt.toString();
    140     }
    141 
    142     public String getManPage() throws IOException {
    143         StringWriter wrt = new StringWriter();
    144         PrintWriter out = new PrintWriter(wrt);
    145 
    146         BufferedReader reader = new BufferedReader(new InputStreamReader(
    147                 this.getClass().getResourceAsStream("opengrok.xml"), "US-ASCII"));
    148 
    149         String line;
    150         spool(reader, out, "___INSERT_DATE___");
    151         out.print("<refmiscinfo class=\"date\">");
    152         out.print(DateFormat.getDateInstance(DateFormat.MEDIUM).format(new Date()));
    153         out.println("</refmiscinfo>");
    154 
    155         spool(reader, out, "___INSERT_USAGE___");
    156         for (Option o : options) {
    157             out.println("<optional><option>");
    158             out.print(o.option);
    159             if (o.argument != null) {
    160                 out.print(" <replaceable>");
    161                 out.print(o.argument);
    162                 out.print("</replaceable>");
    163             }
    164             out.println("</option></optional>");
    165         }
    166 
    167         spool(reader, out, "___INSERT_OPTIONS___");
    168         for (Option o : options) {
    169             out.print("<varlistentry><term><option>");
    170             out.print(o.option);
    171             out.print("</option></term><listitem><para>");
    172             out.print(o.description);
    173             out.println("</para></listitem></varlistentry>");
    174         }
    175 
    176         spool(reader, out, "___END_OF_FILE___");
    177         out.flush();
    178         reader.close();
    179 
    180         return wrt.toString();
    181     }
    182 
    183     /**
    184      * Print out a manual page on standard out
    185      *
    186      * @param argv argument vector. not used.
    187      */
    188     public static void main(String[] argv) {
    189         CommandLineOptions co = new CommandLineOptions();
    190         try {
    191             System.out.println(co.getManPage());
    192         } catch (IOException exp) {
    193             exp.printStackTrace();
    194             System.exit(1);
    195         }
    196         System.exit(0);
    197     }
    198 }
    199