io.proscript.jlight.JLight.java Source code

Java tutorial

Introduction

Here is the source code for io.proscript.jlight.JLight.java

Source

/*
 * Copyright 2015 Matiss Treinis <mrtreinis@gmail.com>
 *
 * This file is part of JLight project.
 * Project home: http://proscript.io/jlight/
 *
 * 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 io.proscript.jlight;

import org.apache.commons.cli.*;

import java.io.IOException;

public class JLight {

    public static void main(String[] args) throws IOException {

        CommandLineParser parser = new DefaultParser();
        Options options = new Options();

        Option host = new Option("h", "host", true, "Host of the HTTP server (default 127.0.0.1)");
        Option port = new Option("p", "port", true, "Port of the HTTP server (default 9000)");

        Option main = new Option("c", "class", true, "Application to run, e.g. org.vendor.class");
        Option help = new Option("h", "help", false, "Display help and exit");
        Option version = new Option("v", "version", false, "Display JLight version");

        options.addOption(host);
        options.addOption(port);

        options.addOption(main);
        options.addOption(help);
        options.addOption(version);

        CommandLine line;

        try {
            line = parser.parse(options, args);

            if (line.hasOption("h")) {
                HelpFormatter formatter = new HelpFormatter();
                Version.print();
                formatter.printHelp("jlight", options);
                return;
            }

            if (line.hasOption("v")) {
                Version.print();
                return;
            }

            if (!line.hasOption("c")) {
                throw new ParseException("Option 'class' is required");
            }

            Runtime.run(line);
        } catch (ParseException exp) {
            System.err.println(exp.getMessage());
        }
    }
}