package net.nullschool.grib2json;

import ch.qos.logback.classic.LoggerContext;
import com.lexicalscope.jewel.JewelRuntimeException;
import com.lexicalscope.jewel.cli.CliFactory;
import org.slf4j.LoggerFactory;

/**
 * 2013-10-24<p/>
 *
 * Execution shim for the grib2json utility. Parses command line options and invokes the {@link Grib2Json} converter.
 *
 * @author Cameron Beccario
 */
class Launcher {

    private static void printUsage() {
        System.out.println(CliFactory.createCli(Options.class).getHelpMessage());
    }

    public static void main(String args[]) {
        try {
            Options options;
            try {
                options = CliFactory.parseArguments(Options.class, args);
            }
            catch (JewelRuntimeException t) {
                printUsage();
                System.out.println();
                System.err.println(t.getMessage());
                System.exit(-1);
                return;
            }

            if (options.getShowHelp() || options.getFile() == null) {
                printUsage();
                return;
            }

            LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
            if (!options.getEnableLogging()) {
                lc.stop();
            }

            new Grib2Json(options).write();
        }
        catch (IllegalArgumentException e) {
            System.err.println(e.getMessage());
            System.exit(-1);
        }
        catch (Throwable t) {
            t.printStackTrace(System.err);
            System.exit(-2);
        }
    }
}

@@ -909,33 +909,32 @@
     System.exit(
--
 1);%0A            
@@ -1052,32 +1052,92 @@
   printUsage();%0A
+                System.exit(options.getShowHelp() ? 0 : 1);%0A
                 
@@ -1501,17 +1501,16 @@
 em.exit(
--
 1);%0A    
@@ -1612,17 +1612,16 @@
 em.exit(
--
 2);%0A    

tweak exit codes