comparison src/main/kotlin/name/blackcap/passman/ReadSubcommand.kt @ 6:711cc42e96d7

Got the list subcommand working, but needs efficiency improvements.
author David Barts <n5jrn@me.com>
date Tue, 20 Sep 2022 20:52:21 -0700
parents eafa3779aef8
children 698c4a3d758d
comparison
equal deleted inserted replaced
5:ad997df1f560 6:711cc42e96d7
1 package name.blackcap.passman 1 package name.blackcap.passman
2 2
3 import org.apache.commons.cli.CommandLine 3 import org.apache.commons.cli.*
4 import org.apache.commons.cli.DefaultParser 4 import kotlin.system.exitProcess
5 import org.apache.commons.cli.Options
6 import org.apache.commons.cli.ParseException
7 5
8 class ReadSubcommand(): Subcommand() { 6 class ReadSubcommand(): Subcommand() {
9 private lateinit var commandLine: CommandLine 7 private lateinit var commandLine: CommandLine
10 8
11 override fun run(args: Array<String>) { 9 override fun run(args: Array<String>) {
12 val options = Options().apply { 10 val options = Options().apply {
13 addOption("c", "clipboard", false, "Copy username and password into clipboard.") 11 addOption("c", "clipboard", false, "Copy username and password into clipboard.")
12 addOption("h", "help", false, "Print this help message.")
14 addOption("l", "long", false, "Long format listing.") 13 addOption("l", "long", false, "Long format listing.")
15 } 14 }
16 try { 15 try {
17 commandLine = DefaultParser().parse(options, args) 16 commandLine = DefaultParser().parse(options, args)
18 } catch (e: ParseException) { 17 } catch (e: ParseException) {
19 die(e.message ?: "syntax error", 2) 18 die(e.message ?: "syntax error", 2)
20 } 19 }
20 if (commandLine.hasOption("help")) {
21 HelpFormatter().printHelp("$SHORTNAME read", options)
22 exitProcess(0)
23 }
21 val clipboard = commandLine.hasOption("clipboard") 24 val clipboard = commandLine.hasOption("clipboard")
22 val long = commandLine.hasOption("long") 25 val long = commandLine.hasOption("long")
23 if (commandLine.args.size != 1) { 26 if (commandLine.args.isEmpty()) {
24 die("expecting site name", 2) 27 error("expecting site name")
28 }
29 if (commandLine.args.size > 1) {
30 error("unexpected trailing arguments")
25 } 31 }
26 val nameIn = commandLine.args[0]; 32 val nameIn = commandLine.args[0];
27 val db = Database.open() 33 val db = Database.open()
28 val id = db.makeKey(nameIn) 34 val id = db.makeKey(nameIn)
29 35