View Javadoc

1   package org.kit.furia;
2   
3   import java.io.InputStream;
4   import java.util.Properties;
5   
6   import org.ajmm.obsearch.example.HelpException;
7   import org.apache.commons.cli.CommandLine;
8   import org.apache.commons.cli.CommandLineParser;
9   import org.apache.commons.cli.GnuParser;
10  import org.apache.commons.cli.HelpFormatter;
11  import org.apache.commons.cli.Option;
12  import org.apache.commons.cli.Options;
13  import org.apache.commons.cli.ParseException;
14  import org.apache.log4j.PropertyConfigurator;
15  
16  public class AbstractFuriaChanCommandLine {
17  
18      public static void initLogger() {
19          try {
20              
21              InputStream is = AbstractFuriaChanCommandLine.class
22                      .getResourceAsStream("/furiaLog4j.config");
23              Properties props = new Properties();
24              props.load(is);
25              PropertyConfigurator.configure(props);
26          } catch (final Exception e) {
27              System.err.print("Make sure log4j is configured properly"
28                      + e.getMessage());
29              e.printStackTrace();
30              System.exit(48);
31          }
32      }
33  
34      /**
35       * Parses the array of options as received in main() and returns a
36       * CommandLine object that makes it easier to analyze the commands.
37       * @param options
38       *                The options object (generated from initCommandLine(...))
39       * @param c
40       *                The class that will be used for the name of the program.
41       * @param args
42       *                Arguments of the command line (as received in main(...))
43       * @return A CommandLine object ready to parse commands
44       * @throws ParseException
45       *                 If the given arguments have syntax errors
46       * @throws HelpException
47       *                 If the user wants "help" we generate an exception
48       */
49      public static CommandLine getCommandLine(final Options options,
50              final Class c, final String[] args) throws ParseException,
51              HelpException {
52          final CommandLineParser parser = new GnuParser();
53          final Option help = new Option("help", "print this message");
54          options.addOption(help);
55          final CommandLine line = parser.parse(options, args);
56          // add the "help option to the help" :)
57          if (line.hasOption("help")) {
58              final HelpFormatter formatter = new HelpFormatter();
59              formatter.printHelp(c.getName(), options, true);
60              throw new HelpException();
61          }
62          return line;
63      }
64  
65  }