Skip to content

Commit

Permalink
Move completion check to finalize
Browse files Browse the repository at this point in the history
  • Loading branch information
ajalt committed Apr 28, 2024
1 parent 9a3f6c0 commit f00bd97
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.github.ajalt.clikt.parsers

import com.github.ajalt.clikt.core.BaseCliktCommand
import com.github.ajalt.clikt.core.CliktError
import com.github.ajalt.clikt.core.PrintHelpMessage
import com.github.ajalt.clikt.core.UsageError
import com.github.ajalt.clikt.completion.CompletionGenerator
import com.github.ajalt.clikt.core.*
import com.github.ajalt.clikt.internal.*
import com.github.ajalt.clikt.output.Localization
import com.github.ajalt.clikt.output.defaultLocalization
Expand Down Expand Up @@ -58,6 +56,8 @@ object CommandLineParser {
val groups = command.registeredParameterGroups()
val arguments = command.registeredArguments()

throwCompletionMessageIfRequsted(context, command)

val (eagerOpts, nonEagerOpts) = command.registeredOptions()
.partition { it.eager }

Expand Down Expand Up @@ -105,3 +105,19 @@ private fun CommandInvocation<*>.throwErrors() {
is CliktError -> throw first
}
}

private fun throwCompletionMessageIfRequsted(
context: Context,
command: BaseCliktCommand<*>,
) {
if (command.autoCompleteEnvvar == null) return
val envvar = when {
command.autoCompleteEnvvar.isBlank() -> "_${
command.commandName.replace("-", "_").uppercase()
}_COMPLETE"

else -> command.autoCompleteEnvvar
}
val envval = context.readEnvvar(envvar) ?: return
throw CompletionGenerator.getCompletionMessage(command, envval)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.github.ajalt.clikt.parsers

import com.github.ajalt.clikt.completion.CompletionGenerator
import com.github.ajalt.clikt.core.*
import com.github.ajalt.clikt.parameters.options.Option
import com.github.ajalt.clikt.parameters.options.splitOptionPrefix
Expand All @@ -9,14 +8,6 @@ internal fun <RunnerT> parseArgv(
rootCommand: BaseCliktCommand<RunnerT>,
originalArgv: List<String>,
): CommandLineParseResult<RunnerT> {
val rootContext = rootCommand.resetContext(null)

generateCompletion(rootContext, rootCommand)?.let {
return@parseArgv CommandLineParseResult(
emptyList(), originalArgv, originalArgv, it
)
}

val results = mutableListOf<CommandInvocation<RunnerT>>()
var expandedArgv = originalArgv
var command: BaseCliktCommand<RunnerT>? = rootCommand
Expand Down Expand Up @@ -54,30 +45,14 @@ internal fun <RunnerT> parseArgv(
return CommandLineParseResult(results, originalArgv, expandedArgv, null)
}

private fun generateCompletion(
context: Context,
command: BaseCliktCommand<*>,
): PrintCompletionMessage? {
if (command.autoCompleteEnvvar == null) return null
val envvar = when {
command.autoCompleteEnvvar.isBlank() -> "_${
command.commandName.replace("-", "_").uppercase()
}_COMPLETE"

else -> command.autoCompleteEnvvar
}
val envval = context.readEnvvar(envvar) ?: return null
return CompletionGenerator.getCompletionMessage(command, envval)
}

private class CommandParser<RunnerT>(
private val command: BaseCliktCommand<RunnerT>,
parentContext: Context?,
argv: List<String>,
startingIndex: Int,
) {
private var tokens = argv
private val context = command.currentContext
private val context = command.resetContext(parentContext)
private val aliases = command.aliases()
private val extraSubcommands = parentContext?.selfAndAncestors()
?.firstOrNull { it.command.allowMultipleSubcommands }?.command
Expand Down

0 comments on commit f00bd97

Please sign in to comment.