comparison src/main/kotlin/name/blackcap/passman/ListSubcommand.kt @ 26:69526ae8c8de

Allow list --name to be abbreviated -n, since it is so commonly used.
author David Barts <n5jrn@me.com>
date Fri, 05 Jul 2024 10:11:31 -0700
parents 07406c4af4a5
children
comparison
equal deleted inserted replaced
25:131e39d96862 26:69526ae8c8de
19 OptionDescriptor(CASE, "Treat upper and lower case as distinct."), 19 OptionDescriptor(CASE, "Treat upper and lower case as distinct."),
20 OptionDescriptor(FIXED, "Match fixed substrings instead of regular expressions."), 20 OptionDescriptor(FIXED, "Match fixed substrings instead of regular expressions."),
21 OptionDescriptor(HELP, "Print this help message."), 21 OptionDescriptor(HELP, "Print this help message."),
22 OptionDescriptor(LONG, "Long format listing.") 22 OptionDescriptor(LONG, "Long format listing.")
23 ) 23 )
24 val ABBREV_STRING_OPTIONS = listOf<OptionDescriptor>(
25 OptionDescriptor(NAME, "Match site name.")
26 )
24 val STRING_OPTIONS = listOf<OptionDescriptor>( 27 val STRING_OPTIONS = listOf<OptionDescriptor>(
25 OptionDescriptor(NAME, "Match site name."),
26 OptionDescriptor(NOTES, "Match notes."), 28 OptionDescriptor(NOTES, "Match notes."),
27 OptionDescriptor(USERNAME, "Match username.") 29 OptionDescriptor(USERNAME, "Match username.")
28 ) 30 )
29 val TIME_OPTIONS = listOf<OptionDescriptor>( 31 val TIME_OPTIONS = listOf<OptionDescriptor>(
30 OptionDescriptor(ACCESSED, "Match time password last read."), 32 OptionDescriptor(ACCESSED, "Match time password last read."),
46 private fun parseArgs(args: Array<String>): Unit { 48 private fun parseArgs(args: Array<String>): Unit {
47 val options = Options().apply { 49 val options = Options().apply {
48 FLAG_OPTIONS.forEach { 50 FLAG_OPTIONS.forEach {
49 addOption(it.name.first().toString(), it.name, false, it.help) 51 addOption(it.name.first().toString(), it.name, false, it.help)
50 } 52 }
53 ABBREV_STRING_OPTIONS.forEach() {
54 addOption(Option(it.name.first().toString(), it.name, true, it.help).apply
55 { setArgs(Option.UNLIMITED_VALUES) })
56 }
51 (STRING_OPTIONS + TIME_OPTIONS).forEach { 57 (STRING_OPTIONS + TIME_OPTIONS).forEach {
52 addOption(Option(null, it.name, true, it.help).apply 58 addOption(Option(null, it.name, true, it.help).apply
53 { setArgs(Option.UNLIMITED_VALUES) }) 59 { setArgs(Option.UNLIMITED_VALUES) })
54 } 60 }
55 } 61 }
64 } 70 }
65 if (commandLine.args.isNotEmpty()) { 71 if (commandLine.args.isNotEmpty()) {
66 throw SubcommandException(message = "unexpected trailing arguments", status = 2) 72 throw SubcommandException(message = "unexpected trailing arguments", status = 2)
67 } 73 }
68 74
69 STRING_OPTIONS.forEach { 75 (ABBREV_STRING_OPTIONS + STRING_OPTIONS).forEach {
70 commandLine.getOptionValues(it.name)?.forEach { value -> 76 commandLine.getOptionValues(it.name)?.forEach { value ->
71 val regexOptions = mutableSetOf<RegexOption>() 77 val regexOptions = mutableSetOf<RegexOption>()
72 if (commandLine.hasOption(FIXED)) { 78 if (commandLine.hasOption(FIXED)) {
73 regexOptions.add(RegexOption.LITERAL) 79 regexOptions.add(RegexOption.LITERAL)
74 } 80 }