001: /* CommandLineParser
002: *
003: * Created on Feb 2, 2004
004: *
005: * Copyright (C) 2004 Internet Archive.
006: *
007: * This file is part of the Heritrix web crawler (crawler.archive.org).
008: *
009: * Heritrix is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU Lesser Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * any later version.
013: *
014: * Heritrix is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
017: * GNU Lesser Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser Public License
020: * along with Heritrix; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
022: */
023: package org.archive.crawler;
024:
025: import java.io.PrintWriter;
026: import java.util.List;
027:
028: import org.apache.commons.cli.CommandLine;
029: import org.apache.commons.cli.HelpFormatter;
030: import org.apache.commons.cli.Option;
031: import org.apache.commons.cli.Options;
032: import org.apache.commons.cli.ParseException;
033: import org.apache.commons.cli.PosixParser;
034: import org.apache.commons.cli.UnrecognizedOptionException;
035:
036: /**
037: * Print Heritrix command-line usage message.
038: *
039: * @author stack
040: * @version $Id: CommandLineParser.java 4551 2006-08-29 00:47:56Z stack-sf $
041: */
042: public class CommandLineParser {
043: private static final String USAGE = "Usage: ";
044: private static final String NAME = "heritrix";
045: private Options options = null;
046: private CommandLine commandLine = null;
047: private PrintWriter out = null;
048: private String version = null;
049:
050: /**
051: * Block default construction.
052: *
053: */
054: private CommandLineParser() {
055: super ();
056: }
057:
058: /**
059: * Constructor.
060: *
061: * @param args Command-line arguments to process.
062: * @param out PrintStream to write on.
063: * @param version Heritrix version
064: *
065: * @throws ParseException Failied parse of command line.
066: */
067: public CommandLineParser(String[] args, PrintWriter out,
068: String version) throws ParseException {
069: super ();
070:
071: this .out = out;
072: this .version = version;
073:
074: this .options = new Options();
075: this .options.addOption(new Option("h", "help", false,
076: "Prints this message and exits."));
077: this .options
078: .addOption(new Option(
079: "b",
080: "bind",
081: true,
082: "Comma-separated list of IP addresses or hostnames for web server "
083: + "to listen on. Set to / to listen on all available\nnetwork "
084: + "interfaces. Default is 127.0.0.1."));
085: this .options.addOption(new Option("p", "port", true,
086: "Port to run web user interface on. Default: 8080."));
087: this .options
088: .addOption(new Option(
089: "a",
090: "admin",
091: true,
092: "Login and password for web user interface administration. "
093: + "Required (unless passed via the 'heritrix.cmdline.admin'\n"
094: + "system property). Pass value of the form 'LOGIN:PASSWORD'."));
095: this .options
096: .addOption(new Option("r", "run", false,
097: "Put heritrix into run mode. If ORDER.XML begin crawl."));
098: this .options.addOption(new Option("n", "nowui", false,
099: "Put heritrix into run mode and begin crawl using ORDER.XML."
100: + " Do not put up web user interface."));
101: Option option = new Option(
102: "s",
103: "selftest",
104: true,
105: "Run the integrated selftests. Pass test name to test it only"
106: + " (Case sensitive: E.g. pass 'Charset' to run charset selftest).");
107: option.setOptionalArg(true);
108: this .options.addOption(option);
109:
110: PosixParser parser = new PosixParser();
111: try {
112: this .commandLine = parser.parse(this .options, args, false);
113: } catch (UnrecognizedOptionException e) {
114: usage(e.getMessage(), 1);
115: }
116: }
117:
118: /**
119: * Print usage then exit.
120: */
121: public void usage() {
122: usage(0);
123: }
124:
125: /**
126: * Print usage then exit.
127: *
128: * @param exitCode
129: */
130: public void usage(int exitCode) {
131: usage(null, exitCode);
132: }
133:
134: /**
135: * Print message then usage then exit.
136: *
137: * The JVM exits inside in this method.
138: *
139: * @param message Message to print before we do usage.
140: * @param exitCode Exit code to use in call to System.exit.
141: */
142: public void usage(String message, int exitCode) {
143: outputAndExit(message, true, exitCode);
144: }
145:
146: /**
147: * Print message and then exit.
148: *
149: * The JVM exits inside in this method.
150: *
151: * @param message Message to print before we do usage.
152: * @param exitCode Exit code to use in call to System.exit.
153: */
154: public void message(String message, int exitCode) {
155: outputAndExit(message, false, exitCode);
156: }
157:
158: /**
159: * Print out optional message an optional usage and then exit.
160: *
161: * Private utility method. JVM exits from inside in this method.
162: *
163: * @param message Message to print before we do usage.
164: * @param doUsage True if we are to print out the usage message.
165: * @param exitCode Exit code to use in call to System.exit.
166: */
167: private void outputAndExit(String message, boolean doUsage,
168: int exitCode) {
169: if (message != null) {
170: this .out.println(message);
171: }
172:
173: if (doUsage) {
174: HeritrixHelpFormatter formatter = new HeritrixHelpFormatter();
175: formatter.printHelp(this .out, 80, NAME, "Options:",
176: this .options, 1, 2, "Arguments:", false);
177: this .out.println(" ORDER.XML Crawl order to run.\n");
178: }
179:
180: // Close printwriter so stream gets flushed.
181: this .out.close();
182: System.exit(exitCode);
183: }
184:
185: /**
186: * @return Options passed on the command line.
187: */
188: public Option[] getCommandLineOptions() {
189: return this .commandLine.getOptions();
190: }
191:
192: /**
193: * @return Arguments passed on the command line.
194: */
195: public List getCommandLineArguments() {
196: return this .commandLine.getArgList();
197: }
198:
199: /**
200: * @return Command line.
201: */
202: public CommandLine getCommandLine() {
203: return this .commandLine;
204: }
205:
206: /**
207: * @return Returns the version.
208: */
209: public String getVersion() {
210: return this .version;
211: }
212:
213: /**
214: * Override so can customize usage output.
215: *
216: * @author stack
217: * @version $Id: CommandLineParser.java 4551 2006-08-29 00:47:56Z stack-sf $
218: */
219: public class HeritrixHelpFormatter extends HelpFormatter {
220: public HeritrixHelpFormatter() {
221: super ();
222: }
223:
224: public void printUsage(PrintWriter pw, int width,
225: String cmdLineSyntax) {
226: out.println(USAGE + NAME + " --help");
227: out.println(USAGE + NAME + " --nowui ORDER.XML");
228: out.println(USAGE + NAME + " [--port=#]"
229: + " [--run] [--bind=IP,IP...] "
230: + "--admin=LOGIN:PASSWORD \\\n\t[ORDER.XML]");
231: out.println(USAGE + NAME
232: + " [--port=#] --selftest[=TESTNAME]");
233: out.println("Version: " + getVersion());
234: }
235:
236: public void printUsage(PrintWriter pw, int width, String app,
237: Options options) {
238: this.printUsage(pw, width, app);
239: }
240: }
241: }
|