Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parsing multi subcommands with optional args #540

Merged
merged 1 commit into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
- Added support for aliasing options to other options. ([#535](https://github.com/ajalt/clikt/pull/535))

### Changed
- In a subcommand with `argument().multiple()`, the behavior is now the same regardless of the value of `allowMultipleSubcommands`: if a token matches a subcommand name, it's now treated as a subcommand rather than a positional argument.
- In a subcommand with and an `argument()` with `multiple()` or `optional()`, the behavior is now the same regardless of the value of `allowMultipleSubcommands`: if a token matches a subcommand name, it's now treated as a subcommand rather than a positional argument.
- Due to changes to the internal parsing algorithm, the exact details of error messages when multiple usage errors occur have changed in some cases.
- **Breaking Change:** Moved the following parameters from `CliktCommand`'s constructor; override the corresponding properties instead:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.github.ajalt.clikt.core

import com.github.ajalt.clikt.parameters.arguments.argument
import com.github.ajalt.clikt.parameters.arguments.multiple
import com.github.ajalt.clikt.parameters.arguments.optional
import com.github.ajalt.clikt.parameters.groups.OptionGroup
import com.github.ajalt.clikt.parameters.groups.cooccurring
import com.github.ajalt.clikt.parameters.groups.mutuallyExclusiveOptions
Expand All @@ -10,9 +11,7 @@ import com.github.ajalt.clikt.parameters.options.flag
import com.github.ajalt.clikt.parameters.options.option
import com.github.ajalt.clikt.parameters.options.required
import com.github.ajalt.clikt.parameters.types.int
import com.github.ajalt.clikt.testing.TestCommand
import com.github.ajalt.clikt.testing.parse
import com.github.ajalt.clikt.testing.test
import com.github.ajalt.clikt.testing.*
import com.github.ajalt.clikt.testing.withEnv
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.data.blocking.forAll
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.github.ajalt.clikt.parameters
import com.github.ajalt.clikt.core.*
import com.github.ajalt.clikt.parameters.arguments.argument
import com.github.ajalt.clikt.parameters.arguments.multiple
import com.github.ajalt.clikt.parameters.arguments.optional
import com.github.ajalt.clikt.parameters.arguments.pair
import com.github.ajalt.clikt.parameters.options.default
import com.github.ajalt.clikt.parameters.options.option
Expand Down Expand Up @@ -338,6 +339,18 @@ class SubcommandTest {
}
}

@Test
@JsName("multiple_subcommands_optional_sub_arg")
fun `multiple subcommands optional sub arg`() {
class Sub: TestCommand(count = 2) {
val a by argument().optional()
}
class C: TestCommand(allowMultipleSubcommands = true)
val sub = Sub()
C().subcommands(sub).parse("sub sub b")
sub.a shouldBe "b"
}

@Test
@JsName("multiple_subcommands_with_nesting")
fun `multiple subcommands with nesting`() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ private class CommandParser<T : BaseCliktCommand<T>>(
private val errors = mutableListOf<CliktError>()
private var i = startingIndex
private var minAliasI = i
private val minArgCount = command._arguments.sumOf { it.nvalues.coerceAtLeast(0) }
private val minArgCount = command._arguments.sumOf {
if (it.required) it.nvalues.coerceAtLeast(0) else 0
}
private val canParseSubcommands get() = argumentTokens.size >= minArgCount
private var canParseOptions = true
private var canExpandAtFiles = context.expandArgumentFiles
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
Loading