ajalt / clikt

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

Error with multiple subcommands & arguments #539

Closed juraj-hrivnak closed 2 months ago

juraj-hrivnak commented 2 months ago

The issue

When running the parent command with this setup:

class Subcommand : CliktCommand()
{
    private val arg by argument().optional()
    private val opt by option()

    override fun run() = Unit
}

class Parent : CliktCommand(allowMultipleSubcommands = true)
{
    private val args: List<String> by argument("arguments").multiple()
    override fun run() = Unit

    init
    {
        this.subcommands(Subcommand())
    }
}

in the command line: $ .\tool parent subcommand --opt foo subcommand bar I get this error:

Error: got unexpected extra argument (bar)

This is, I think, incorrect and it should instead pass bar as the argument.

Note that this only happens when an option is used in the subcommand before using the argument in the next subcommand. Running: $ .\tool parent subcommand foo subcommand bar works fine and: $ .\tool parent subcommand --opt foo subcommand --opt bar works fine too.