/*
 * Main.java
 * By: Denver Coneybeare
 * Sept 05, 2011
 *
 * Copyright 2011 Denver Coneybeare
 * This file is released under the Apache License Version 2.0
 * http://www.apache.org/licenses/LICENSE-2.0.txt
 */
package vpp;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

/**
 * The main entry point for the Velocity Preprocessor.
 */
public class Main implements Runnable {

    private final String[] args;

    /**
     * Creates a new instance of <code>Main</code>.
     * <p>
     * A reference to the given String array will be stored internally.
     * Therefore, no modifications to this array should be done; otherwise,
     * undefined behaviour may occur.
     * 
     * @param args the command-line arguments to use; may be null
     */
    public Main(String[] args) {
        this.args = args;
    }

    /**
     * Exits the Java Virtual Machine with the given status. This method never
     * returns.
     * <p>
     * The implementation of this method in this class simply invokes
     * {@link System#exit(int)} with the given status. It is invoked by
     * {@link #run()} to terminate the Java Virtual Machine. It may be
     * overridden to terminate the JVM in some non-standard way, which will
     * especially be useful if unit testing this class. Normally, there is no
     * reason to override this method.
     * 
     * @param status the exit status; conventionally, 0 (zero) indicates
     * success, 1 (one) indicates abnormal program termination, and 2 (two)
     * indicates an error processing the command-line arguments occurred.
     */
    public void exit(int status) {
        System.exit(status);
    }

    /**
     * Returns the command-line arguments array used by this class. A reference
     * to the exact array that was specified to the constructor is returned;
     * therefore, it is strongly recommended to <em>not</em> modify the array in
     * any way, as it may lead to undefined behaviour.
     * 
     * @return the command-line arguments array that was specified to the
     * constructor; may be null
     */
    public String[] getArgs() {
        return this.args;
    }

    private void parseArgs() throws ParseException {
        final Option outputPathOption =
            new Option("o", "output-path", true,
                "The output file or directory. If not specified then the "
                    + "output is written to standard output.");

        final Options options = new Options();
        options.addOption(outputPathOption);

        final GnuParser parser = new GnuParser();
        final CommandLine parsedArgs = parser.parse(options, this.args);
        final String[] leftoverArgs = parsedArgs.getArgs();

        System.out.println("Leftover Args (" + leftoverArgs.length + ")");
        for (int i = 0; i < leftoverArgs.length; i++) {
            System.out.println("  " + i + ": " + leftoverArgs[i]);
        }

        final Option[] parsedOptions = parsedArgs.getOptions();
        for (final Option option : parsedOptions) {
            final String value = option.getValue();
            System.out.println(option.getOpt() + "=" + value);
        }
    }

    /**
     * Runs the Velocity Preprocessor using the command-line arguments that were
     * given to the constructor. Upon completion, {@link #exit(int)} is invoked
     * to terminate the Java Virtual Machine; therefore, under normal
     * circumstances this method never returns.
     */
    public void run() {
        int exitCode = 0;
        try {
            this.parseArgs();
        } catch (final ParseException e) {
            System.err.println("ERROR: " + e.getMessage());
            exitCode = 2;
        }

        this.exit(exitCode);
    }

    /**
     * The main entry point for the Velocity Preprocessor application. This
     * method simply creates a new instance of this class with the given String
     * array and then invokes {@link #run()}.
     * 
     * @param args the command-line arguments; may be null.
     */
    public static void main(String[] args) {
        final Main main = new Main(args);
        main.run();
    }

}

@@ -304,16 +304,61 @@
 Parser;%0A
+import org.apache.commons.cli.HelpFormatter;%0A
 import o
@@ -2612,24 +2612,163 @@
 output.%22);%0A%0A
+        final Option helpOption =%0A            new Option(%22h%22, %22help%22, false,%0A                %22Print help for this application and exit%22);%0A%0A
         fina
@@ -2846,16 +2846,55 @@
 Option);
+%0A        options.addOption(helpOption);
 %0A%0A      
@@ -3083,437 +3083,738 @@
     
-System.out.println(%22Leftover Args (%22 + leftoverArgs.length + %22)%22);%0A        for (int i = 0; i %3C leftoverArgs.length; i++) %7B%0A            System.out.println(%22  %22 + i + %22: %22 + leftoverArgs%5Bi%5D);%0A        %7D%0A%0A        final Option%5B%5D parsedOptions = parsedArgs.getOptions();%0A        for (final Option option : parsedOptions) %7B%0A            final String value = option.getValue();%0A            System.out.println(option.getOpt() + %22=%22 + value
+boolean printHelp = false;%0A        final Option%5B%5D parsedOptions = parsedArgs.getOptions();%0A        for (final Option option : parsedOptions) %7B%0A            final String value = option.getValue();%0A            System.out.println(option.getOpt() + %22=%22 + value);%0A%0A            if (option.getOpt().equals(%22h%22)) %7B%0A                printHelp = true;%0A            %7D%0A        %7D%0A%0A        System.out.println(%22Leftover Args (%22 + leftoverArgs.length + %22)%22);%0A        for (int i = 0; i %3C leftoverArgs.length; i++) %7B%0A            System.out.println(%22  %22 + i + %22: %22 + leftoverArgs%5Bi%5D);%0A        %7D%0A%0A        if (printHelp) %7B%0A            final HelpFormatter formatter = new HelpFormatter();%0A            formatter.printHelp(%22%5Boptions%5D %5Binput paths%5D%22, options
 );%0A 

Add a help option