Skip to content

Refer to other option during validation #467

Answered by ajalt
lppedd asked this question in Q&A
Discussion options

You must be logged in to vote

validate is explicitly run after everything has been parsed, so your approach is fine. The only thing I would change is to use the built-in path conversion, which includes some validation already.

private val basePath by option()
    .path(mustExist = true, canBeFile = false)
    .default(Path("."))

private val configFile by option()
    .path()
    .default(Path("config.properties"))
    .validate {
        val tmpPath = basePath.resolve(it)
        if (!tmpPath.isRegularFile()) {
            fail("The configuration file '$tmpPath' does not exist or is not a valid file")
        }
    }

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@lppedd
Comment options

Answer selected by lppedd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants