Skip to content

Commit

Permalink
Remove Invocation -> OptionInvocation
Browse files Browse the repository at this point in the history
  • Loading branch information
ajalt committed Apr 14, 2024
1 parent d525a6c commit ca87fa7
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,36 @@ import com.github.ajalt.clikt.parameters.groups.ParameterGroup
import com.github.ajalt.clikt.parameters.internal.LateinitException
import com.github.ajalt.clikt.parameters.options.Option
import com.github.ajalt.clikt.parsers.ArgumentInvocation
import com.github.ajalt.clikt.parsers.Invocation
import com.github.ajalt.clikt.parsers.OptionInvocation

internal fun finalizeOptions(
context: Context,
options: List<Option>,
invocationsByOption: Map<Option, List<Invocation>>,
invocationsByOption: Map<Option, List<OptionInvocation>>,
) {
iterateFinalization(
context, getAllOptions(invocationsByOption, options).map { Opt(it.key, it.value) }
).throwErrors()
}

private sealed class Param
private data class Opt(val option: Option, val invs: List<Invocation>) : Param()
private data class Opt(val option: Option, val invs: List<OptionInvocation>) : Param()
private data class Arg(val argument: Argument, val invs: List<String>) : Param()
private data class Group(val group: ParameterGroup, val invs: Map<Option, List<Invocation>>) :
private data class Group(val group: ParameterGroup, val invs: Map<Option, List<OptionInvocation>>) :
Param()

internal fun finalizeParameters(
context: Context,
options: List<Option>,
groups: List<ParameterGroup>,
arguments: List<Argument>,
optionInvocations: Map<Option, List<Invocation>>,
optionInvocations: Map<Option, List<OptionInvocation>>,
argumentInvocations: List<ArgumentInvocation>,
): List<UsageError> {
// Add uninvoked params last so that e.g. we can skip prompting if there's an error in an
// invoked option

val allGroups = buildMap<ParameterGroup?, Map<Option, List<Invocation>>> {
val allGroups = buildMap<ParameterGroup?, Map<Option, List<OptionInvocation>>> {
optionInvocations.entries
.groupBy({ it.key.group }, { it.key to it.value })
.mapValuesTo(this) { it.value.toMap() }
Expand Down Expand Up @@ -65,9 +65,9 @@ internal fun finalizeParameters(
}

private fun getAllOptions(
invocations: Map<Option, List<Invocation>>,
invocations: Map<Option, List<OptionInvocation>>,
options: List<Option>,
): Map<Option, List<Invocation>> {
): Map<Option, List<OptionInvocation>> {
return buildMap {
putAll(invocations)
for (opt in options) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import com.github.ajalt.clikt.parameters.options.OptionDelegate
import com.github.ajalt.clikt.parameters.options.RawOption
import com.github.ajalt.clikt.parameters.options.switch
import com.github.ajalt.clikt.parameters.types.choice
import com.github.ajalt.clikt.parsers.Invocation
import com.github.ajalt.clikt.parsers.OptionInvocation
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty

Expand Down Expand Up @@ -50,7 +50,7 @@ class ChoiceGroup<GroupT : OptionGroup, OutT> internal constructor(

override fun finalize(
context: Context,
invocationsByOption: Map<Option, List<Invocation>>,
invocationsByOption: Map<Option, List<OptionInvocation>>,
) {
val key = option.value
if (key == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.github.ajalt.clikt.parameters.internal.NullableLateinit
import com.github.ajalt.clikt.parameters.options.Option
import com.github.ajalt.clikt.parameters.options.hasEnvvarOrSourcedValue
import com.github.ajalt.clikt.parameters.options.required
import com.github.ajalt.clikt.parsers.Invocation
import com.github.ajalt.clikt.parsers.OptionInvocation
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty

Expand Down Expand Up @@ -47,7 +47,7 @@ class CoOccurringOptionGroup<GroupT : OptionGroup, OutT> internal constructor(

override fun getValue(thisRef: BaseCliktCommand<*>, property: KProperty<*>): OutT = value

override fun finalize(context: Context, invocationsByOption: Map<Option, List<Invocation>>) {
override fun finalize(context: Context, invocationsByOption: Map<Option, List<OptionInvocation>>) {
occurred = invocationsByOption.isNotEmpty() || group.options.any {
it.hasEnvvarOrSourcedValue(context, invocationsByOption[it] ?: emptyList())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.github.ajalt.clikt.core.*
import com.github.ajalt.clikt.internal.finalizeOptions
import com.github.ajalt.clikt.parameters.internal.NullableLateinit
import com.github.ajalt.clikt.parameters.options.*
import com.github.ajalt.clikt.parsers.Invocation
import com.github.ajalt.clikt.parsers.OptionInvocation
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty

Expand Down Expand Up @@ -44,7 +44,7 @@ class MutuallyExclusiveOptions<OptT : Any, OutT> internal constructor(

override fun finalize(
context: Context,
invocationsByOption: Map<Option, List<Invocation>>,
invocationsByOption: Map<Option, List<OptionInvocation>>,
) {
finalizeOptions(context, options, invocationsByOption)
val values = options.filter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.github.ajalt.clikt.core.ParameterHolder
import com.github.ajalt.clikt.internal.finalizeOptions
import com.github.ajalt.clikt.output.HelpFormatter
import com.github.ajalt.clikt.parameters.options.Option
import com.github.ajalt.clikt.parsers.Invocation
import com.github.ajalt.clikt.parsers.OptionInvocation
import kotlin.properties.PropertyDelegateProvider
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty
Expand Down Expand Up @@ -38,7 +38,7 @@ interface ParameterGroup {
* @param context The context for this parse
* @param invocationsByOption The invocations of options in this group.
*/
fun finalize(context: Context, invocationsByOption: Map<Option, List<Invocation>>)
fun finalize(context: Context, invocationsByOption: Map<Option, List<OptionInvocation>>)

/**
* Called after all of a command's parameters have been [finalize]d to perform validation of the final values.
Expand Down Expand Up @@ -90,7 +90,7 @@ open class OptionGroup(
options += option
}

override fun finalize(context: Context, invocationsByOption: Map<Option, List<Invocation>>) {
override fun finalize(context: Context, invocationsByOption: Map<Option, List<OptionInvocation>>) {
finalizeOptions(context, options, invocationsByOption)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import com.github.ajalt.clikt.core.GroupableOption
import com.github.ajalt.clikt.core.ParameterHolder
import com.github.ajalt.clikt.core.StaticallyGroupedOption
import com.github.ajalt.clikt.output.HelpFormatter
import com.github.ajalt.clikt.parsers.Invocation
import com.github.ajalt.clikt.parsers.OptionInvocation
import com.github.ajalt.clikt.sources.ValueSource
import kotlin.properties.PropertyDelegateProvider
import kotlin.properties.ReadOnlyProperty
Expand Down Expand Up @@ -80,7 +80,7 @@ interface Option {
* @param context The context for this parse
* @param invocations A possibly empty list of invocations of this option.
*/
fun finalize(context: Context, invocations: List<Invocation>)
fun finalize(context: Context, invocations: List<OptionInvocation>)

/**
* Called after all of a command's parameters have been [finalize]d to perform validation of the final value.
Expand Down Expand Up @@ -141,14 +141,14 @@ internal fun splitOptionPrefix(name: String): Pair<String, String> =
internal fun Option.longestName(): String? = names.maxByOrNull { it.length }

internal sealed class FinalValue {
data class Parsed(val values: List<Invocation>) : FinalValue()
data class Parsed(val values: List<OptionInvocation>) : FinalValue()
data class Sourced(val values: List<ValueSource.Invocation>) : FinalValue()
data class Envvar(val key: String, val value: String) : FinalValue()
}

internal fun Option.getFinalValue(
context: Context,
invocations: List<Invocation>,
invocations: List<OptionInvocation>,
envvar: String?,
): FinalValue {
return when {
Expand All @@ -170,7 +170,7 @@ internal fun Option.getFinalValue(
// final value again to check for values from other sources.
internal fun Option.hasEnvvarOrSourcedValue(
context: Context,
invocations: List<Invocation>,
invocations: List<OptionInvocation>,
): Boolean {
val envvar = (this as? OptionWithValues<*, *, *>)?.envvar
val final = this.getFinalValue(context, invocations, envvar)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import com.github.ajalt.clikt.parameters.transform.HelpTransformContext
import com.github.ajalt.clikt.parameters.transform.TransformContext
import com.github.ajalt.clikt.parameters.transform.message
import com.github.ajalt.clikt.parameters.types.int
import com.github.ajalt.clikt.parsers.Invocation
import com.github.ajalt.clikt.parsers.OptionInvocation
import com.github.ajalt.clikt.sources.ValueSource
import com.github.ajalt.mordant.terminal.Terminal
import kotlin.jvm.JvmMultifileClass
Expand Down Expand Up @@ -191,7 +191,7 @@ private class OptionWithValuesImpl<AllT, EachT, ValueT>(
return helpGetter?.invoke(HelpTransformContext(context)) ?: ""
}

override fun finalize(context: Context, invocations: List<Invocation>) {
override fun finalize(context: Context, invocations: List<OptionInvocation>) {
val invs = when (val v = getFinalValue(context, invocations, envvar)) {
is FinalValue.Parsed -> {
when (valueSplit) {
Expand All @@ -208,13 +208,13 @@ private class OptionWithValuesImpl<AllT, EachT, ValueT>(
}

is FinalValue.Sourced -> {
v.values.map { Invocation("", it.values) }
v.values.map { OptionInvocation("", it.values) }
}

is FinalValue.Envvar -> {
when (valueSplit) {
null -> listOf(Invocation(v.key, listOf(v.value)))
else -> listOf(Invocation(v.key, v.value.split(valueSplit)))
null -> listOf(OptionInvocation(v.key, listOf(v.value)))
else -> listOf(OptionInvocation(v.key, v.value.split(valueSplit)))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import com.github.ajalt.clikt.parameters.options.Option
/**
* The output of parsing a single option and its values.
*/
data class Invocation(
data class OptionInvocation(
/**
* The name that was used to invoke the option. May be empty if the value was not retrieved
* from the command line (e.g. values from environment variables).
Expand Down Expand Up @@ -42,7 +42,7 @@ class ArgumentInvocation(
*/
class CommandInvocation<T : BaseCliktCommand<T>>(
val command: T,
val optionInvocations: Map<Option, List<Invocation>>,
val optionInvocations: Map<Option, List<OptionInvocation>>,
val argumentInvocations: List<ArgumentInvocation>,
/**
* The subcommands of this command that were invoked.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ private data class CommandParseResult<T : BaseCliktCommand<T>>(
val i: Int,
val errors: List<CliktError>,
val expandedArgv: List<String>,
val optInvocations: Map<Option, List<Invocation>>,
val optInvocations: Map<Option, List<OptionInvocation>>,
val argInvocations: List<ArgumentInvocation>,
) {
fun toInvocation(subcommands: List<CommandInvocation<T>>) = CommandInvocation(
Expand All @@ -373,7 +373,7 @@ private data class CommandParseResult<T : BaseCliktCommand<T>>(
}

private data class OptInvocation(val opt: Option, val name: String, val values: List<String>) {
val inv get() = Invocation(name, values)
val inv get() = OptionInvocation(name, values)
}

private data class OptParseResult(
Expand Down

0 comments on commit ca87fa7

Please sign in to comment.