annotate src/main/kotlin/name/blackcap/passman/Arguments.kt @ 23:af86b8e0b88c

Clean up exit output.
author David Barts <n5jrn@me.com>
date Tue, 02 Jul 2024 18:18:29 -0700
parents ea65ab890f66
children 3a3067ba673b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
16
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
1 package name.blackcap.passman
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
2
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
3 import org.apache.commons.cli.*
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
4 import kotlin.reflect.KCallable
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
5 import kotlin.reflect.KMutableProperty
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
6 import kotlin.reflect.full.findAnnotation
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
7 import kotlin.reflect.full.hasAnnotation
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
8 import kotlin.reflect.full.isSubtypeOf
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
9 import kotlin.reflect.typeOf
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
10 import kotlin.system.exitProcess
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
11
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
12 @Target(AnnotationTarget.FIELD, AnnotationTarget.PROPERTY)
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
13 annotation class Argument(
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
14 val shortName: Char = AnnotationArgumentInfo.UNSPEC_SHORT,
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
15 val longName: String = AnnotationArgumentInfo.UNSPEC_LONG,
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
16 val description: String);
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
17
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
18 private class AnnotationArgumentInfo(val property: KMutableProperty<*>) {
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
19 companion object {
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
20 const val UNSPEC_SHORT: Char = '\u0000'
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
21 const val UNSPEC_LONG: String = ""
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
22 }
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
23 val type = property.returnType
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
24 val annotation = property.findAnnotation<Argument>()!!
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
25 val longName = if (annotation.longName == UNSPEC_LONG) { property.name } else { annotation.longName }
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
26 val shortName = if (annotation.shortName == UNSPEC_SHORT) { longName.first() } else { annotation.shortName }
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
27 }
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
28
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
29 private class AnnotationArgumentParser(val name: String, val args: Array<String>, val into: Any) {
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
30 private companion object {
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
31 val BOOLEAN_TYPE = typeOf<Boolean>()
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
32 val STRING_TYPE = typeOf<String>()
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
33 val CHAR_TYPE = typeOf<Char>()
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
34 }
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
35
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
36 val annotated = into::class.members.filter { it is KMutableProperty<*> && it.hasAnnotation<Argument>() }
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
37 val options = Options()
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
38 lateinit var commandLine: CommandLine
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
39
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
40 fun parse(): Array<String> {
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
41 build()
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
42 doParse()
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
43 return extract()
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
44 }
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
45
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
46 private fun build() {
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
47 annotated.iterate { _, info ->
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
48 when {
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
49 info.type.isSubtypeOf(BOOLEAN_TYPE) ->
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
50 options.addOption(info.shortName.toString(), info.longName, false, info.annotation.description)
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
51
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
52 info.type.isSubtypeOf(STRING_TYPE) || info.type.isSubtypeOf(CHAR_TYPE) ->
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
53 options.addOption(info.shortName.toString(), info.longName, true, info.annotation.description)
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
54 }
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
55 }
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
56 }
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
57
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
58 private fun doParse() {
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
59 commandLine = try {
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
60 DefaultParser().parse(options, args)
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
61 } catch (e: ParseException) {
21
ea65ab890f66 More work to support interactive feature.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
62 throw SubcommandException(message = e.message ?: "syntax error", status = 2)
16
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
63 }
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
64 if (commandLine.hasOption("help")) {
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
65 HelpFormatter().printHelp("$SHORTNAME $name [options] csv_file", options)
21
ea65ab890f66 More work to support interactive feature.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
66 throw SubcommandException(status = 0)
16
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
67 }
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
68 }
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
69
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
70 private fun extract(): Array<String> {
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
71 annotated.iterate { annotated, info ->
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
72 if (commandLine.hasOption(info.longName)) {
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
73 when {
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
74 info.type.isSubtypeOf(BOOLEAN_TYPE) ->
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
75 annotated.setter.call(true)
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
76 info.type.isSubtypeOf(STRING_TYPE) ->
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
77 annotated.setter.call(commandLine.getOptionValue(info.longName))
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
78 info.type.isSubtypeOf(CHAR_TYPE) ->
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
79 annotated.setter.call(commandLine.getCharOptionValue(info.longName))
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
80 }
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
81 }
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
82 }
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
83 return commandLine.args
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
84 }
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
85
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
86 private fun Collection<KCallable<*>>.iterate(block: (KMutableProperty<*>, AnnotationArgumentInfo) -> Unit) =
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
87 forEach {
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
88 block(it as KMutableProperty<*>, AnnotationArgumentInfo(it))
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
89 }
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
90
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
91 private fun CommandLine.getCharOptionValue(name: String): Char {
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
92 val optionValue = getOptionValue(name)
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
93 when (optionValue.length) {
21
ea65ab890f66 More work to support interactive feature.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
94 0 -> throw SubcommandException(message = "--$name value must not be empty")
16
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
95 1 -> return optionValue[0]
21
ea65ab890f66 More work to support interactive feature.
David Barts <n5jrn@me.com>
parents: 16
diff changeset
96 else -> throw SubcommandException(message = "--$name value must be a single character")
16
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
97 }
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
98 }
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
99 }
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
100
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
101 fun parseInto(name: String, args: Array<String>, into: Any): Array<String> =
7a74ae668665 Add export subcommand.
David Barts <n5jrn@me.com>
parents:
diff changeset
102 AnnotationArgumentParser(name, args, into).parse()