Mercurial > cgi-bin > hgweb.cgi > PassMan
annotate src/main/kotlin/name/blackcap/passman/Arguments.kt @ 22:07406c4af4a5
More interactive mode stuff.
author | David Barts <n5jrn@me.com> |
---|---|
date | Tue, 02 Jul 2024 17:34:52 -0700 |
parents | ea65ab890f66 |
children | 3a3067ba673b |
rev | line source |
---|---|
16 | 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) { | |
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 | 63 } |
64 if (commandLine.hasOption("help")) { | |
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 | 67 } |
68 } | |
69 | |
70 private fun extract(): Array<String> { | |
71 annotated.iterate { annotated, info -> | |
72 if (commandLine.hasOption(info.longName)) { | |
73 when { | |
74 info.type.isSubtypeOf(BOOLEAN_TYPE) -> | |
75 annotated.setter.call(true) | |
76 info.type.isSubtypeOf(STRING_TYPE) -> | |
77 annotated.setter.call(commandLine.getOptionValue(info.longName)) | |
78 info.type.isSubtypeOf(CHAR_TYPE) -> | |
79 annotated.setter.call(commandLine.getCharOptionValue(info.longName)) | |
80 } | |
81 } | |
82 } | |
83 return commandLine.args | |
84 } | |
85 | |
86 private fun Collection<KCallable<*>>.iterate(block: (KMutableProperty<*>, AnnotationArgumentInfo) -> Unit) = | |
87 forEach { | |
88 block(it as KMutableProperty<*>, AnnotationArgumentInfo(it)) | |
89 } | |
90 | |
91 private fun CommandLine.getCharOptionValue(name: String): Char { | |
92 val optionValue = getOptionValue(name) | |
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 | 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 | 97 } |
98 } | |
99 } | |
100 | |
101 fun parseInto(name: String, args: Array<String>, into: Any): Array<String> = | |
102 AnnotationArgumentParser(name, args, into).parse() |