comparison src/main/kotlin/name/blackcap/passman/ReadSubcommand.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 a38a2a1036c3
children 131e39d96862
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 kotlin.system.exitProcess
5 4
6 class ReadSubcommand(): Subcommand() { 5 class ReadSubcommand(): Subcommand() {
7 private companion object { 6 private companion object {
8 const val CLIPBOARD = "clipboard" 7 const val CLIPBOARD = "clipboard"
9 const val HELP = "help" 8 const val HELP = "help"
21 addOption("l", LONG, false, "Long format listing.") 20 addOption("l", LONG, false, "Long format listing.")
22 } 21 }
23 try { 22 try {
24 commandLine = DefaultParser().parse(options, args) 23 commandLine = DefaultParser().parse(options, args)
25 } catch (e: ParseException) { 24 } catch (e: ParseException) {
26 die(e.message ?: "syntax error", 2) 25 throw SubcommandException(message = e.message ?: "syntax error", status = 2, cause = e)
27 } 26 }
28 if (commandLine.hasOption(HELP)) { 27 if (commandLine.hasOption(HELP)) {
29 HelpFormatter().printHelp("$SHORTNAME read [options] name", options) 28 HelpFormatter().printHelp("$SHORTNAME read [options] name", options)
30 exitProcess(0) 29 return
31 } 30 }
32 if (commandLine.args.isEmpty()) { 31 if (commandLine.args.isEmpty()) {
33 die("expecting site name", 2) 32 throw SubcommandException(message = "expecting site name", status = 2)
34 } 33 }
35 if (commandLine.args.size > 1) { 34 if (commandLine.args.size > 1) {
36 die("unexpected trailing arguments", 2) 35 throw SubcommandException(message = "unexpected trailing arguments", status = 2)
37 } 36 }
38 val nameIn = commandLine.args[0]; 37 val nameIn = commandLine.args[0];
39 val db = Database.open() 38 val db = Database.default
40 val entry = Entry.fromDatabase(db, nameIn) 39 val entry = Entry.fromDatabase(db, nameIn)
41 if (entry == null) { 40 if (entry == null) {
42 die("no record matches ${see(nameIn)}") 41 throw SubcommandException(message = "no record matches ${see(nameIn)}")
43 return // Kotlin is too stupid to realize we never get here
44 } 42 }
45 try { 43 try {
46 print(ALT_SB + CLEAR) 44 print(ALT_SB + CLEAR)
47 val redaction = if (commandLine.hasOption(CLIPBOARD)) { "(in clipboard)" } else { null } 45 val redaction = if (commandLine.hasOption(CLIPBOARD)) { "(in clipboard)" } else { null }
48 if (commandLine.hasOption(LONG)) { 46 if (commandLine.hasOption(LONG)) {