comparison src/main/kotlin/name/blackcap/passman/MergeSubcommand.kt @ 21:ea65ab890f66

More work to support interactive feature.
author David Barts <n5jrn@me.com>
date Tue, 02 Jul 2024 11:27:39 -0700
parents 8f3ddebb4295
children
comparison
equal deleted inserted replaced
20:4391afcf6bd0 21:ea65ab890f66
1 package name.blackcap.passman 1 package name.blackcap.passman
2 2
3 import org.apache.commons.cli.* 3 import org.apache.commons.cli.*
4 import java.sql.ResultSet 4 import java.sql.ResultSet
5 import kotlin.system.exitProcess
6 5
7 class MergeSubcommand(): Subcommand() { 6 class MergeSubcommand(): Subcommand() {
8 private companion object { 7 private companion object {
9 const val FORCE = "force" 8 const val FORCE = "force"
10 const val HELP = "help" 9 const val HELP = "help"
13 private lateinit var commandLine: CommandLine 12 private lateinit var commandLine: CommandLine
14 private lateinit var db: Database 13 private lateinit var db: Database
15 14
16 override fun run(args: Array<String>) { 15 override fun run(args: Array<String>) {
17 parseArguments(args) 16 parseArguments(args)
18 db = Database.open() 17 if (commandLine.hasOption(MergeSubcommand.HELP)) {
18 return
19 }
20 db = Database.default
19 doMerge() 21 doMerge()
20 } 22 }
21 23
22 private fun parseArguments(args: Array<String>) { 24 private fun parseArguments(args: Array<String>) {
23 val options = Options().apply { 25 val options = Options().apply {
26 addOption("h", MergeSubcommand.HELP, false, "Print this help message.") 28 addOption("h", MergeSubcommand.HELP, false, "Print this help message.")
27 } 29 }
28 try { 30 try {
29 commandLine = DefaultParser().parse(options, args) 31 commandLine = DefaultParser().parse(options, args)
30 } catch (e: ParseException) { 32 } catch (e: ParseException) {
31 die(e.message ?: "syntax error", 2) 33 throw SubcommandException(message = e.message ?: "syntax error", status = 2, cause = e)
32 } 34 }
33 if (commandLine.hasOption(MergeSubcommand.HELP)) { 35 if (commandLine.hasOption(MergeSubcommand.HELP)) {
34 HelpFormatter().printHelp("$SHORTNAME merge [options] other_database", options) 36 HelpFormatter().printHelp("$SHORTNAME merge [options] other_database", options)
35 exitProcess(0) 37 return
36 } 38 }
37 if (commandLine.args.isEmpty()) { 39 if (commandLine.args.isEmpty()) {
38 die("expecting other database name", 2) 40 throw SubcommandException(message = "expecting other database name", status = 2)
39 } 41 }
40 if (commandLine.args.size > 1) { 42 if (commandLine.args.size > 1) {
41 die("unexpected trailing arguments", 2) 43 throw SubcommandException(message = "unexpected trailing arguments", status = 2)
42 } 44 }
43 } 45 }
44 46
45 private fun doMerge() { 47 private fun doMerge() {
46 val otherFile = commandLine.args[0] 48 val otherFile = commandLine.args[0]