LeoColman
09/14/2021, 11:52 AMclass Restore : CliktCommand() {
val file by argument().file(mustExist = true)
val temporary = createTempFile().toFile()
val password by argument().convert { it.toCharArray() }
val destinationDir by option().file().defaultLazy { file.parentFile }
override fun run() {
decryptFile()
unzipFile()
}
private fun decryptFile() {
Alice(AliceContextBuilder().build()).decrypt(file, temporary, password)
}
private fun unzipFile() {
val zipFile = ZipFile(temporary)
zipFile.entries.asIterator().forEach {
val destination = File(destinationDir, it.name)
destinationDir.mkdirs()
IOUtils.copy(zipFile.getInputStream(it), destination.outputStream())
}
file.delete()
}
}
At the first line I'm getting the exception Exception in thread "main" java.lang.IllegalStateException: Cannot read from argument delegate before parsing command line
. I don't know what's going on, as my command seems very similar to the current example at the documentation https://ajalt.github.io/clikt/arguments/LeoColman
09/14/2021, 11:53 AMLeoColman
09/14/2021, 12:06 PMLeoColman
09/14/2021, 12:09 PMAJ Alt
09/14/2021, 4:42 PMLeoColman
09/14/2021, 4:44 PMLeoColman
09/14/2021, 4:45 PMby lazy
AJ Alt
09/14/2021, 5:28 PMdestinationDir
entirely and do this instead:
val destination = File(destinationDir ?: file.parentFile, it.name)
That's simple and doesn't rely on finalization order.
I agree that the documentation should be improved.AJ Alt
10/01/2021, 6:59 PMdefaultLazy
is still called during parsing so that values can be validated, but now you can reference another parameter.