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