OptionBuilder

Builder for creating command-line options with various configurations and constraints.

This class provides a fluent API for defining options, including type refinements, constraints, environment variable support, and help text.

Functions

Link copied to clipboard

Sets the arity (number of required values) for this option.

Link copied to clipboard
fun atLeastOneWith(vararg refs: KProperty<*>): OptionBuilder<T>

Ensures at least one of this option and the specified properties is provided.

Link copied to clipboard
fun atMostOneWith(vararg refs: KProperty<*>): OptionBuilder<T>

Ensures at most one of this option and the specified properties is provided.

Link copied to clipboard

Converts string option to boolean option.

Link copied to clipboard
fun conflictsWith(vararg refs: KProperty<*>): OptionBuilder<T>

Specifies that this option conflicts with the specified properties.

Link copied to clipboard

Converts boolean option to count collector.

Link copied to clipboard

Sets a default value for nullable option, making it non-nullable.

Link copied to clipboard

Converts string option to double option.

Link copied to clipboard

Makes boolean option eager (evaluated immediately, causing early exit).

Link copied to clipboard
inline fun <E : Enum<E>> OptionBuilder<String?>.enum(aliases: Map<String, E> = emptyMap(), ignoreCase: Boolean = true): OptionBuilder<E?>

Converts string option to enum option.

Link copied to clipboard
fun exactlyOneWith(vararg refs: KProperty<*>): OptionBuilder<T>

Ensures exactly one of this option and the specified properties is provided.

Link copied to clipboard

Converts string option to float option.

Link copied to clipboard

Configures this option to read its value from the specified environment variable.

Link copied to clipboard
fun help(text: String): OptionBuilder<T>

Sets help text for this option.

Link copied to clipboard

Marks this option as hidden from help output.

Link copied to clipboard

Converts string option to integer option.

Link copied to clipboard

Converts option to list collector.

Link copied to clipboard

Converts string option to long option.

Link copied to clipboard
fun <R> map(desc: String? = expectedDesc, f: (T) -> R?): OptionBuilder<R?>

Maps the parsed value using the specified transformation function. For non-nullable builders (from .default() or .required()), preserves non-nullable type when transformation returns non-null. For nullable builders, always returns nullable type.

Link copied to clipboard

Makes this nullable boolean option negatable with a custom prefix. Creates an automatic negation option (e.g., --disable-colors for --colors with prefix "disable-").

@JvmName(name = "negatableNonNull")
fun OptionBuilder<Boolean>.negatable(prefix: String? = null): OptionBuilder<Boolean>

Makes this non-nullable boolean option negatable with a custom prefix. Creates an automatic negation option (e.g., --disable-colors for --colors with prefix "disable-").

Link copied to clipboard
fun OptionBuilder<String?>.oneOf(vararg allowed: String, ignoreCase: Boolean = true): OptionBuilder<String?>

Restricts string option to one of the specified allowed values.

Link copied to clipboard
fun onlyInDomains(vararg refs: KProperty<*>): OptionBuilder<T>

Restricts this option to be available only in the specified domains.

Link copied to clipboard
fun onValue(callback: (T) -> Unit): OptionBuilder<T>

Registers a callback to be executed when this option's value is found during parsing. The callback receives the converted value and is executed after all parsing and validation completes. Callbacks are executed in the order their values appear on the command line.

Link copied to clipboard
fun OptionBuilder<String?>.password(prompt: String = "Password: ", confirmPrompt: String = "Confirm password: ", requireConfirmation: Boolean = false, mismatchPrompt: String = "Passwords do not match. Please try again.", maxRetries: Int = 3): OptionBuilder<CharArray?>

Converts string option to password option with prompting capabilities.

Link copied to clipboard
open operator override fun provideDelegate(thisRef: Arguments, property: KProperty<*>): ReadOnlyProperty<Arguments, T>

Provides the property delegate implementation for command-line options. This method is called automatically by Kotlin's property delegation system. It registers the option with the Arguments instance and returns a delegate that provides access to the parsed value.

Link copied to clipboard

Marks this option as required.

@JvmName(name = "requiredGeneric")
fun <T : Any> OptionBuilder<T?>.required(): NonNullableOptionBuilder<T>

Marks this nullable option as required, converting it to a non-nullable type.

Link copied to clipboard

Requires that this option be present when all of the specified properties are present.

Link copied to clipboard

Requires that this option be present along with any of the specified properties.

Link copied to clipboard
fun <U> requireIfValue(ref: KProperty<U>, predicate: (U?) -> Boolean): OptionBuilder<T>

Requires that this option be present when the specified property matches the given predicate.

Link copied to clipboard

Controls whether this option requires a value.

Link copied to clipboard

Converts option to set collector.

Link copied to clipboard
fun validate(vararg validations: Pair<String, (T) -> Boolean>): OptionBuilder<T>

Adds multiple validators at once.

fun validate(message: String = "Invalid value for @name: @value", predicate: (T) -> Boolean): OptionBuilder<T>

Adds a validator that checks the parsed value against the given predicate. The validator only runs if the value is not null.