All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sourceforge.tink.app.TinkAppHelper Maven / Gradle / Ivy

The newest version!
/**
 * Copyright 2008,2009 Ivan SZKIBA
 *
 * 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.
 * under the License.
 */
package net.sourceforge.tink.app;

import net.sourceforge.tink.model.FileObject;
import net.sourceforge.tink.model.TinkException;

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

import java.io.IOException;

import java.util.logging.LogManager;
import java.util.logging.Logger;

public class TinkAppHelper
{
    private static final Logger logger = Logger.getLogger(TinkAppHelper.class.getName());
    public static final String DEFAULT_CONFIG = System.getProperty("user.home") + "/.tinkrc";

    static
    {
        try
        {
            LogManager.getLogManager().readConfiguration(TinkAppHelper.class.getResourceAsStream("logging.properties"));
        }
        catch (IOException x)
        {
            x.printStackTrace();
            System.exit(2);
        }
    }

    private CommandLine commandLine;
    private FileObject configFile;
    private final String name;
    private Option optConfig;
    private Option optHelp;
    private Options options;

    public TinkAppHelper(String name)
    {
        this.name = name;
    }

    public FileObject getConfigFile()
    {
        return configFile;
    }

    public boolean init(String[] args) throws TinkException
    {
        options = buildOptions();
        CommandLineParser parser = new GnuParser();

        try
        {
            commandLine = parser.parse(options, args, true);
        }
        catch (ParseException x)
        {
            throw new TinkException("Usage error: " + x.getMessage(), x);
        }

        if (hasOption(optHelp))
        {
            help();

            return false;
        }

        processOptions();

        return true;
    }

    protected String getName()
    {
        return name;
    }

    protected String getOption(Option opt)
    {
        return getOption(opt, null);
    }

    protected String getOption(Option opt, String defaultValue)
    {
        return commandLine.hasOption(opt.getOpt()) ? opt.getValue() : defaultValue;
    }

    @SuppressWarnings("static-access")
    protected Options buildOptions()
    {
        Options opts = new Options();

        optHelp = OptionBuilder.withDescription("Print this message").withLongOpt("help").create('?');
        optConfig = OptionBuilder.withDescription("Config file").withLongOpt("config").hasArg().withArgName("file").create('c');

        opts.addOption(optHelp);
        opts.addOption(optConfig);

        return opts;
    }

    protected boolean hasOption(Option opt)
    {
        return commandLine.hasOption(opt.getOpt());
    }

    protected void help()
    {
        HelpFormatter formatter = new HelpFormatter();

        formatter.printHelp(getName() + " [options]", "\nOptions:", options, null);
    }

    protected void processOptions() throws TinkException
    {
        configFile = new FileObject(getOption(optConfig, DEFAULT_CONFIG));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy