view src/name/blackcap/imageprep/Main.kt @ 4:5234e4500d45

Command-line arguments (only partially tested).
author David Barts <n5jrn@me.com>
date Fri, 17 Jul 2020 14:51:41 -0700
parents e0efe7848130
children 884f1415a330
line wrap: on
line source

/*
 * Entry point, etc.
 */
package name.blackcap.imageprep

import java.io.File
import java.util.logging.Level
import java.util.logging.Logger
import javax.swing.UIManager
import name.blackcap.kcli.CommandLine
import name.blackcap.kcli.InvalidArgumentException
import name.blackcap.kcli.Option
import name.blackcap.kcli.PromptingParser
import org.apache.commons.cli.HelpFormatter
import org.apache.commons.cli.Options
import org.apache.commons.cli.ParseException

object Application {
    /* name we call ourselves */
    val MYNAME = "ImagePrep"

    /* global UI objects */
    var mainFrame: MainFrame by setOnce()
    var helpDialog: HelpDialog by setOnce()

    fun initialize() {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        mainFrame = MainFrame() /* must be created first */
        mainFrame.jMenuBar = MyMenuBar()
        setMacMenus() /* always safe to call; no-op if not a Mac */
        helpDialog = HelpDialog()
        mainFrame.setVisible(true)
    }
}

fun main(args: Array<String>) {
    LOGGER.log(Level.INFO, "beginning execution")
    if (OS.type == OS.MAC) {
        System.setProperty("apple.laf.useScreenMenuBar", "true")
    }

    if (Settings.outputToInputDir)
        Settings.outputTo = System.getProperty("user.dir")
    val options = Options().apply {
        addOption(Option<Unit>("?", "help", false, "Print this help message."))
        addOption(Option<Int>("m", "maxdim", true, "Maximum dimension (default: ${Settings.maxDimension})").apply {
            default = Settings.maxDimension
            interpolater = {
                val ret = try { it.toInt() } catch (e: NumberFormatException) { -1 }
                if (ret < 1)
                    throw InvalidArgumentException("Dimension must be a positive integer.")
                Settings.maxDimension = ret
                ret
            }
        })
        addOption(Option<File>("o", "output", true, "Output directory (default: ${Settings.outputTo})").apply {
            default = File(Settings.outputTo)
            interpolater = {
                val ret = File(it)
                if (!ret.exists())
                    throw InvalidArgumentException("'${it}' does not exist")
                if (!ret.isDirectory())
                    throw InvalidArgumentException("'${it}' is not a directory")
                Settings.outputTo = ret.canonicalPath
                ret
            }
        })
        addOption(Option<Int>("q", "quality", true, "JPEG quality for output (0-100, default ${Settings.outputQuality})").apply {
            default = Settings.outputQuality
            interpolater = {
                val ret = try { it.toInt() } catch (e: NumberFormatException) { -1 }
                if (ret < 0 || ret > 100)
                    throw InvalidArgumentException("Quality must be an integer from 0 to 100, inclusive.")
                Settings.outputQuality = ret
                ret
            }
        })
    }
    val cmdLine: CommandLine? = try {
        PromptingParser().parse(options, args)
    } catch (e: ParseException) {
        System.err.println("${Application.MYNAME}: syntax error - ${e.message}")
        System.exit(2)
        null
    }
    if (cmdLine!!.hasOption("help")) {
        val usage = Application.MYNAME + " [--maxdim=integer] [--output=directory] [--quality=integer] file [...]"
        HelpFormatter().printHelp(usage, options, false);
        System.exit(0);
    }

    inSwingThread {
        Application.initialize()
        for (fileName in cmdLine!!.args)
            RotateDialog.makeDialog(File(fileName))
    }
}