comparison 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
comparison
equal deleted inserted replaced
3:09dcd475d1bf 4:5234e4500d45
1 /* 1 /*
2 * Entry point, etc. 2 * Entry point, etc.
3 */ 3 */
4 package name.blackcap.imageprep 4 package name.blackcap.imageprep
5 5
6 import javax.swing.UIManager 6 import java.io.File
7 import java.util.logging.Level 7 import java.util.logging.Level
8 import java.util.logging.Logger 8 import java.util.logging.Logger
9 import javax.swing.UIManager
10 import name.blackcap.kcli.CommandLine
11 import name.blackcap.kcli.InvalidArgumentException
12 import name.blackcap.kcli.Option
13 import name.blackcap.kcli.PromptingParser
14 import org.apache.commons.cli.HelpFormatter
15 import org.apache.commons.cli.Options
16 import org.apache.commons.cli.ParseException
9 17
10 object Application { 18 object Application {
11 /* name we call ourselves */ 19 /* name we call ourselves */
12 val MYNAME = "ImagePrep" 20 val MYNAME = "ImagePrep"
13 21
28 fun main(args: Array<String>) { 36 fun main(args: Array<String>) {
29 LOGGER.log(Level.INFO, "beginning execution") 37 LOGGER.log(Level.INFO, "beginning execution")
30 if (OS.type == OS.MAC) { 38 if (OS.type == OS.MAC) {
31 System.setProperty("apple.laf.useScreenMenuBar", "true") 39 System.setProperty("apple.laf.useScreenMenuBar", "true")
32 } 40 }
41
42 if (Settings.outputToInputDir)
43 Settings.outputTo = System.getProperty("user.dir")
44 val options = Options().apply {
45 addOption(Option<Unit>("?", "help", false, "Print this help message."))
46 addOption(Option<Int>("m", "maxdim", true, "Maximum dimension (default: ${Settings.maxDimension})").apply {
47 default = Settings.maxDimension
48 interpolater = {
49 val ret = try { it.toInt() } catch (e: NumberFormatException) { -1 }
50 if (ret < 1)
51 throw InvalidArgumentException("Dimension must be a positive integer.")
52 Settings.maxDimension = ret
53 ret
54 }
55 })
56 addOption(Option<File>("o", "output", true, "Output directory (default: ${Settings.outputTo})").apply {
57 default = File(Settings.outputTo)
58 interpolater = {
59 val ret = File(it)
60 if (!ret.exists())
61 throw InvalidArgumentException("'${it}' does not exist")
62 if (!ret.isDirectory())
63 throw InvalidArgumentException("'${it}' is not a directory")
64 Settings.outputTo = ret.canonicalPath
65 ret
66 }
67 })
68 addOption(Option<Int>("q", "quality", true, "JPEG quality for output (0-100, default ${Settings.outputQuality})").apply {
69 default = Settings.outputQuality
70 interpolater = {
71 val ret = try { it.toInt() } catch (e: NumberFormatException) { -1 }
72 if (ret < 0 || ret > 100)
73 throw InvalidArgumentException("Quality must be an integer from 0 to 100, inclusive.")
74 Settings.outputQuality = ret
75 ret
76 }
77 })
78 }
79 val cmdLine: CommandLine? = try {
80 PromptingParser().parse(options, args)
81 } catch (e: ParseException) {
82 System.err.println("${Application.MYNAME}: syntax error - ${e.message}")
83 System.exit(2)
84 null
85 }
86 if (cmdLine!!.hasOption("help")) {
87 val usage = Application.MYNAME + " [--maxdim=integer] [--output=directory] [--quality=integer] file [...]"
88 HelpFormatter().printHelp(usage, options, false);
89 System.exit(0);
90 }
91
33 inSwingThread { 92 inSwingThread {
34 Application.initialize() 93 Application.initialize()
94 for (fileName in cmdLine!!.args)
95 RotateDialog.makeDialog(File(fileName))
35 } 96 }
36 } 97 }