clikt / com.github.ajalt.clikt.core / Context

Context

class Context

A object used to control command line parsing and pass data between commands.

A new Context instance is created for each command each time the command line is parsed.

Types

Builder

class Builder

Constructors

<init>

Context(parent: Context?, command: CliktCommand, allowInterspersedArgs: Boolean, autoEnvvarPrefix: String?, helpOptionNames: Set<String>, helpOptionMessage: String, helpFormatter: HelpFormatter, tokenTransformer: Context.(String) -> String)

A object used to control command line parsing and pass data between commands.

Properties

allowInterspersedArgs

val allowInterspersedArgs: Boolean

If false, options and arguments cannot be mixed; the first time an argument is encountered, all remaining tokens are parsed as arguments.

autoEnvvarPrefix

val autoEnvvarPrefix: String?

The prefix to add to inferred envvar names. If null, the prefix is based on the parent's prefix, if there is one. If no command specifies, a prefix, envvar lookup is disabled.

command

val command: CliktCommand

The command that this context associated with.

helpFormatter

val helpFormatter: HelpFormatter

The help formatter for this command

helpOptionMessage

val helpOptionMessage: String

The description of the help option.

helpOptionNames

val helpOptionNames: Set<String>

The names to use for the help option. If any names in the set conflict with other options, the conflicting name will not be used for the help option. If the set is empty, or contains no unique names, no help option will be added.

invokedSubcommand

var invokedSubcommand: CliktCommand?

obj

var obj: Any?

parent

val parent: Context?

If this context is the child of another command, parent is the parent command's context.

tokenTransformer

val tokenTransformer: Context.(String) -> String

An optional transformation function that is called to transform command line tokens (options and commands) before parsing. This can be used to implement e.g. case insensitive behavior.

Functions

fail

fun fail(message: String = ""): Nothing

Throw a UsageError with the given message

findObject

fun <T> findObject(): T?

Find the closest object of type T

fun <T> findObject(defaultValue: () -> T): T

Find the closest object of type T, setting this.obj if one is not found.

findRoot

fun findRoot(): Context

Find the outermost context

Companion Object Functions

build

fun build(command: CliktCommand, parent: Context? = null, block: Builder.() -> Unit): Context