/*
 * Copyright 2006 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.geronimo.gshell.commands.standard;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.PosixParser;
import org.apache.geronimo.gshell.command.Command;
import org.apache.geronimo.gshell.command.CommandSupport;
import org.apache.geronimo.gshell.console.IO;

/**
 * Exit the virtual machine.
 *
 * @version $Id$
 */
public class ExitCommand
    extends CommandSupport
{
    public ExitCommand() {
        super("exit");
    }

    protected int doExecute(final String[] args) throws Exception {
        assert args != null;

        //
        // TODO: Optimize, move common code to CommandSupport
        //

        IO io = getIO();

        Options options = new Options();

        options.addOption(OptionBuilder.withLongOpt("help")
            .withDescription("Display this help message")
            .create('h'));

        options.addOption(OptionBuilder.withLongOpt("code")
            .withDescription("Use the given exit code")
            .hasArg()
            .create('c'));

        CommandLineParser parser = new PosixParser();
        CommandLine line = parser.parse(options, args);

        if (line.hasOption('h')) {
            io.out.println(getName() + " -- exit the virtual machine");
            io.out.println();

            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp(
                io.out,
                80, // width (FIXME: Should pull from gshell.columns variable)
                getName() + " [options]",
                "",
                options,
                4, // left pad
                4, // desc pad
                "",
                false); // auto usage

            io.out.println();

            return Command.SUCCESS;
        }

        // Get the status code to exit with
        int exitCode = 0;
        if (line.hasOption('c')) {
            String value = line.getOptionValue('c');
            exitCode = Integer.parseInt(value);
        }

        exit(exitCode);

        // Should never get this far
        assert false;

        return Command.FAILURE;
    }

    private void exit(final int exitCode) {
        log.info("Exiting w/code: " + exitCode);
        System.exit(exitCode);
    }
}

@@ -2925,24 +2925,230 @@
 exitCode) %7B%0A
+        //%0A        // TODO: Need to implement this with an exception notification%0A        //       To allow sub-shell or sever shells to function w/o%0A        //       killing the entire JVM :-(%0A        //%0A%0A
         log.

Comment about fixing exit