convert

inline fun <InT : Any, ValueT : Any> NullableOption<InT, InT>.convert(metavar: String, completionCandidates: CompletionCandidates? = null, crossinline conversion: ValueConverter<InT, ValueT>): NullableOption<ValueT, ValueT>

Convert the option's value type.

The conversion is called once for each value in each invocation of the option. If any errors are thrown, they are caught and a BadParameterValue is thrown with the error message. You can call fail to throw a BadParameterValue manually.

You can call convert more than once to wrap the result of the previous convert, but it cannot be called after transformAll (e.g. multiple) or transformValues (e.g. pair).

Example

val bd: BigDecimal? by option().convert { it.toBigDecimal() }
val fileText: ByteArray? by option().file().convert { it.readBytes() }

Parameters

metavar

The metavar for the type. Overridden by a metavar passed to option.

completionCandidates

candidates to use when completing this option in shell autocomplete, if no candidates are specified in option


inline fun <InT : Any, ValueT : Any> NullableOption<InT, InT>.convert(noinline metavar: Context.() -> String = { localization.defaultMetavar() }, completionCandidates: CompletionCandidates? = null, crossinline conversion: ValueConverter<InT, ValueT>): NullableOption<ValueT, ValueT>

Convert the option's value type.

The conversion is called once for each value in each invocation of the option. If any errors are thrown, they are caught and a BadParameterValue is thrown with the error message. You can call fail to throw a BadParameterValue manually.

You can call convert more than once to wrap the result of the previous convert, but it cannot be called after transformAll (e.g. multiple) or transformValues (e.g. pair).

Example

val bd: BigDecimal? by option().convert { it.toBigDecimal() }
val fileText: ByteArray? by option().file().convert { it.readBytes() }

Parameters

metavar

A lambda returning the metavar for the type. The lambda has a Context receiver for access to localization. Overridden by a metavar passed to option.

completionCandidates

candidates to use when completing this option in shell autocomplete, if no candidates are specified in option


Convert this flag's value type.

The conversion is called once with the final value of the option. If any errors are thrown, they are caught and a BadParameterValue is thrown with the error message. You can call fail to throw a BadParameterValue manually.

Example

val loud by option().flag().convert { if (it) Volume.Loud else Volume.Soft }