comparison src/main/kotlin/name/blackcap/passman/Arguments.kt @ 16:7a74ae668665

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