https://kotlinlang.org logo
#clikt
Title
# clikt
r

Rob Elliot

07/02/2021, 11:42 AM
Hi all - I’d like to have a parameter which could either be a
URI
or a
Path
, which I’ll then turn into a
URI
. So accept any of:
file:///tmp/whatever
/tmp/whatever
- will resolve to
file:///tmp/whatever
foo
- will resolve to
file:///currentdir/foo
<http://example.com>
Obviously I can do it in a call to
convert
but I’d quite like to reuse
com.github.ajalt.clikt.parameters.types.path
- but it can only be called on a
RawArgument
, and it delegates to an internal function so I can’t call that.
I was hoping there might be a way to recover from a previous conversion failure in the DSL, but I haven’t spotted it yet - may just be failing to read carefully enough though.
a

AJ Alt

07/02/2021, 5:59 PM
No, there's no way to convert failed conversion into another type. I'm not sure that'd even be possible to express in the type system. You can reuse conversions e.g.
option().path().convert {it.toURI()}
, but that's only for successful cases. The path validation is pretty simple, I'd suggest just copying the parts you care about into your own
convert
(https://github.com/ajalt/clikt/blob/a220c2c9173df9dace0c2a87f9bff86b211b6428/clikt/src/jvmMain/kotlin/com/github/ajalt/clikt/parameters/types/path.kt#L37).
r

Rob Elliot

07/02/2021, 6:46 PM
Got you, thanks.
4 Views