ajalt / clikt

Multiplatform command line interface parsing for Kotlin
https://ajalt.github.io/clikt/
Apache License 2.0
2.54k stars 121 forks source link

Allow `CliktCommand.test` to take a `vararg` #451

Closed sschuberth closed 1 year ago

sschuberth commented 1 year ago

For convenience and to better visualize separate options, allow to write

test("--foo", "--bar")

in addition to

test("--foo --bar")

This requires to remove the test(argv: Array<String>, ...) overload as that would have the same JVM function signature.

sschuberth commented 1 year ago

This is just a proposal @ajalt, not sure whether you like it as it seems to be a breaking change for explicit users of test(arrayOf("-foo", "--bar"), ...).

ajalt commented 1 year ago

Thanks for the PR.

We can add the vararg version as a separate overload to keep backwards compatibility:

@JvmName("varargTest")
fun CliktCommand.test(
    vararg argv: String,
    stdin: String = "",
    envvars: Map<String, String> = emptyMap(),
    includeSystemEnvvars: Boolean = false,
    ansiLevel: AnsiLevel = AnsiLevel.NONE,
    width: Int = 79,
    height: Int = 24,
): CliktCommandTestResult {
    return test(argv.asList(), stdin, envvars, includeSystemEnvvars, ansiLevel, width, height)
}

A vararg version has the potential to be confusing when called with a single argument (test("-x") calls the single String version which tokenizes the input, but test("-x", "-y") calls the vararg version which doesn't), but I can't think of any situation where the single String version would break fail on any input that would be valid to the varargs, so it should be fine.

sschuberth commented 1 year ago

We can add the vararg version as a separate overload to keep backwards compatibility:

Done.

ajalt commented 1 year ago

Thanks!