BooruGrabberTelegramBot/src/main/kotlin/EnableArgsParser.kt

60 lines
2.4 KiB
Kotlin
Raw Normal View History

2022-09-07 09:01:03 +00:00
import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.parameters.arguments.*
import com.github.ajalt.clikt.parameters.options.*
2022-09-09 10:42:59 +00:00
import com.github.ajalt.clikt.parameters.types.enum
2022-09-07 09:01:03 +00:00
import com.github.ajalt.clikt.parameters.types.int
2022-10-07 16:15:06 +00:00
import net.kodehawa.lib.imageboards.boards.DefaultBoards
2022-09-09 10:42:59 +00:00
import net.kodehawa.lib.imageboards.entities.Rating
2022-09-07 09:01:03 +00:00
2022-10-07 16:15:06 +00:00
class EnableArgsParser(
private val base: ChatSettings = ChatSettings.DEFAULT
2022-10-07 16:15:06 +00:00
) : CliktCommand(name = "enable") {
private fun <EachT : Any, ValueT> NullableOption<EachT, ValueT>.default(
value: EachT?,
defaultForHelp: String = value.toString(),
): NullableOption<EachT, ValueT> {
return if (value != null) {
transformAll(defaultForHelp) { it.lastOrNull() ?: value }
} else {
this
}
2022-09-07 09:01:03 +00:00
}
2022-10-07 16:15:06 +00:00
val count by option("-n")
.int()
.help("Amount of pictures to grab each trigger time")
.check("Count should be in range 1-10") { it in 1 .. 10 }
val query by argument()
.multiple(required = true)
.optional()
2022-10-07 16:15:06 +00:00
.help("Your query to booru. Use syntax \"-- -sometag\" to add excluding of some tag in query")
val krontab by option("-k", "--krontab")
.transformValues(5) { it.joinToString(" ") }
.help("Krontab in format * * * * *. See https://bookstack.inmo.dev/books/krontab/page/string-format")
val board by option("-b", "--board")
2022-12-11 06:17:15 +00:00
.default("safebooru")
2022-10-07 16:15:06 +00:00
.convert { ChatSettings.BoardSerializer.types.getValue(it) }
.help("Board type. Possible values: ${ChatSettings.BoardSerializer.types.keys.joinToString { it }}")
val gallery by option("-g", "--gallery")
.flag(default = base.gallery)
.help("Effective only when count passed > 1. Will send chosen images as gallery instead of separated images")
val rating by option("-r", "--rating")
.enum<Rating> { it.name.lowercase() }
val attachUrls by option("-a", "--attach_urls")
.flag(default = base.attachUrls)
2022-09-07 09:01:03 +00:00
2022-09-09 10:19:14 +00:00
var resultSettings: ChatSettings? = null
private set
2022-09-07 09:01:03 +00:00
override fun run() {
2022-09-09 10:19:14 +00:00
resultSettings = ChatSettings(
query ?.filterNot { it.isEmpty() } ?.joinToString(" ") ?.trim() ?: base.query,
2022-09-07 09:01:03 +00:00
krontab,
2022-10-07 16:15:06 +00:00
board ?: base.board.boardType as DefaultBoards,
count ?: base.count,
2022-09-09 10:42:59 +00:00
gallery,
rating,
attachUrls
2022-09-07 09:01:03 +00:00
)
}
}