Mercurial > cgi-bin > hgweb.cgi > PassMan
annotate src/main/kotlin/name/blackcap/passman/Arguments.kt @ 27:3a3067ba673b
Add idle-time detection to interactive mode, clean up imports.
author | David Barts <n5jrn@me.com> |
---|---|
date | Sat, 27 Jul 2024 09:50:54 -0700 |
parents | ea65ab890f66 |
children |
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 | |
11 @Target(AnnotationTarget.FIELD, AnnotationTarget.PROPERTY) | |
12 annotation class Argument( | |
13 val shortName: Char = AnnotationArgumentInfo.UNSPEC_SHORT, | |
14 val longName: String = AnnotationArgumentInfo.UNSPEC_LONG, | |
15 val description: String); | |
16 | |
17 private class AnnotationArgumentInfo(val property: KMutableProperty<*>) { | |
18 companion object { | |
19 const val UNSPEC_SHORT: Char = '\u0000' | |
20 const val UNSPEC_LONG: String = "" | |
21 } | |
22 val type = property.returnType | |
23 val annotation = property.findAnnotation<Argument>()!! | |
24 val longName = if (annotation.longName == UNSPEC_LONG) { property.name } else { annotation.longName } | |
25 val shortName = if (annotation.shortName == UNSPEC_SHORT) { longName.first() } else { annotation.shortName } | |
26 } | |
27 | |
28 private class AnnotationArgumentParser(val name: String, val args: Array<String>, val into: Any) { | |
29 private companion object { | |
30 val BOOLEAN_TYPE = typeOf<Boolean>() | |
31 val STRING_TYPE = typeOf<String>() | |
32 val CHAR_TYPE = typeOf<Char>() | |
33 } | |
34 | |
35 val annotated = into::class.members.filter { it is KMutableProperty<*> && it.hasAnnotation<Argument>() } | |
36 val options = Options() | |
37 lateinit var commandLine: CommandLine | |
38 | |
39 fun parse(): Array<String> { | |
40 build() | |
41 doParse() | |
42 return extract() | |
43 } | |
44 | |
45 private fun build() { | |
46 annotated.iterate { _, info -> | |
47 when { | |
48 info.type.isSubtypeOf(BOOLEAN_TYPE) -> | |
49 options.addOption(info.shortName.toString(), info.longName, false, info.annotation.description) | |
50 | |
51 info.type.isSubtypeOf(STRING_TYPE) || info.type.isSubtypeOf(CHAR_TYPE) -> | |
52 options.addOption(info.shortName.toString(), info.longName, true, info.annotation.description) | |
53 } | |
54 } | |
55 } | |
56 | |
57 private fun doParse() { | |
58 commandLine = try { | |
59 DefaultParser().parse(options, args) | |
60 } catch (e: ParseException) { | |
21
ea65ab890f66
More work to support interactive feature.
David Barts <n5jrn@me.com>
parents:
16
diff
changeset
|
61 throw SubcommandException(message = e.message ?: "syntax error", status = 2) |
16 | 62 } |
63 if (commandLine.hasOption("help")) { | |
64 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
|
65 throw SubcommandException(status = 0) |
16 | 66 } |
67 } | |
68 | |
69 private fun extract(): Array<String> { | |
70 annotated.iterate { annotated, info -> | |
71 if (commandLine.hasOption(info.longName)) { | |
72 when { | |
73 info.type.isSubtypeOf(BOOLEAN_TYPE) -> | |
74 annotated.setter.call(true) | |
75 info.type.isSubtypeOf(STRING_TYPE) -> | |
76 annotated.setter.call(commandLine.getOptionValue(info.longName)) | |
77 info.type.isSubtypeOf(CHAR_TYPE) -> | |
78 annotated.setter.call(commandLine.getCharOptionValue(info.longName)) | |
79 } | |
80 } | |
81 } | |
82 return commandLine.args | |
83 } | |
84 | |
85 private fun Collection<KCallable<*>>.iterate(block: (KMutableProperty<*>, AnnotationArgumentInfo) -> Unit) = | |
86 forEach { | |
87 block(it as KMutableProperty<*>, AnnotationArgumentInfo(it)) | |
88 } | |
89 | |
90 private fun CommandLine.getCharOptionValue(name: String): Char { | |
91 val optionValue = getOptionValue(name) | |
92 when (optionValue.length) { | |
21
ea65ab890f66
More work to support interactive feature.
David Barts <n5jrn@me.com>
parents:
16
diff
changeset
|
93 0 -> throw SubcommandException(message = "--$name value must not be empty") |
16 | 94 1 -> return optionValue[0] |
21
ea65ab890f66
More work to support interactive feature.
David Barts <n5jrn@me.com>
parents:
16
diff
changeset
|
95 else -> throw SubcommandException(message = "--$name value must be a single character") |
16 | 96 } |
97 } | |
98 } | |
99 | |
100 fun parseInto(name: String, args: Array<String>, into: Any): Array<String> = | |
101 AnnotationArgumentParser(name, args, into).parse() |