1 23 package org.archive.crawler; 24 25 import java.io.PrintWriter ; 26 import java.util.List ; 27 28 import org.apache.commons.cli.CommandLine; 29 import org.apache.commons.cli.HelpFormatter; 30 import org.apache.commons.cli.Option; 31 import org.apache.commons.cli.Options; 32 import org.apache.commons.cli.ParseException; 33 import org.apache.commons.cli.PosixParser; 34 import org.apache.commons.cli.UnrecognizedOptionException; 35 36 37 43 public class CommandLineParser { 44 private static final String USAGE = "Usage: "; 45 private static final String NAME = "heritrix"; 46 private Options options = null; 47 private CommandLine commandLine = null; 48 private PrintWriter out = null; 49 private String version = null; 50 51 55 private CommandLineParser() { 56 super(); 57 } 58 59 68 public CommandLineParser(String [] args, PrintWriter out, String version) 69 throws ParseException { 70 super(); 71 72 this.out = out; 73 this.version = version; 74 75 this.options = new Options(); 76 this.options.addOption(new Option("h","help", false, 77 "Prints this message and exits.")); 78 this.options.addOption(new Option("b", "bind", true, 79 "Comma-separated list of IP addresses or hostnames for web server " 80 + "to listen on. Set to / to listen on all available\nnetwork " 81 + "interfaces. Default is 127.0.0.1.")); 82 this.options.addOption(new Option("p","port", true, 83 "Port to run web user interface on. Default: 8080.")); 84 this.options.addOption(new Option("a", "admin", true, 85 "Login and password for web user interface administration. " + 86 "Required (unless passed via the 'heritrix.cmdline.admin'\n" + 87 "system property). Pass value of the form 'LOGIN:PASSWORD'.")); 88 this.options.addOption(new Option("r", "run", false, 89 "Put heritrix into run mode. If ORDER.XML begin crawl.")); 90 this.options.addOption(new Option("n", "nowui", false, 91 "Put heritrix into run mode and begin crawl using ORDER.XML." + 92 " Do not put up web user interface.")); 93 Option option = new Option("s", "selftest", true, 94 "Run the integrated selftests. Pass test name to test it only" + 95 " (Case sensitive: E.g. pass 'Charset' to run charset selftest)."); 96 option.setOptionalArg(true); 97 this.options.addOption(option); 98 99 PosixParser parser = new PosixParser(); 100 try { 101 this.commandLine = parser.parse(this.options, args, false); 102 } catch (UnrecognizedOptionException e) { 103 usage(e.getMessage(), 1); 104 } 105 } 106 107 110 public void usage() { 111 usage(0); 112 } 113 114 119 public void usage(int exitCode) { 120 usage(null, exitCode); 121 } 122 123 131 public void usage(String message, int exitCode) { 132 outputAndExit(message, true, exitCode); 133 } 134 135 143 public void message(String message, int exitCode) { 144 outputAndExit(message, false, exitCode); 145 } 146 147 156 private void outputAndExit(String message, boolean doUsage, int exitCode) { 157 if (message != null) { 158 this.out.println(message); 159 } 160 161 if (doUsage) { 162 HeritrixHelpFormatter formatter = 163 new HeritrixHelpFormatter(); 164 formatter.printHelp(this.out, 80, NAME, "Options:", this.options, 165 1, 2, "Arguments:", false); 166 this.out.println(" ORDER.XML Crawl order to run.\n"); 167 } 168 169 this.out.close(); 171 System.exit(exitCode); 172 } 173 174 177 public Option [] getCommandLineOptions() { 178 return this.commandLine.getOptions(); 179 } 180 181 184 public List getCommandLineArguments() { 185 return this.commandLine.getArgList(); 186 } 187 188 191 public CommandLine getCommandLine() { 192 return this.commandLine; 193 } 194 195 198 public String getVersion() { 199 return this.version; 200 } 201 202 208 public class HeritrixHelpFormatter 209 extends HelpFormatter { 210 public HeritrixHelpFormatter() { 211 super(); 212 } 213 214 public void printUsage(PrintWriter pw, int width, 215 String cmdLineSyntax) { 216 out.println(USAGE + NAME + " --help"); 217 out.println(USAGE + NAME + " --nowui ORDER.XML"); 218 out.println(USAGE + NAME + " [--port=#]" + 219 " [--run] [--bind=IP,IP...] " + 220 "--admin=LOGIN:PASSWORD \\\n\t[ORDER.XML]"); 221 out.println(USAGE + NAME + " [--port=#] --selftest[=TESTNAME]"); 222 out.println("Version: " + getVersion()); 223 } 224 225 public void printUsage(PrintWriter pw, int width, 226 String app, Options options) { 227 this.printUsage(pw, width, app); 228 } 229 } 230 } | Popular Tags |