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

Arguments in parent command are not accepted when subcommand is present #543

Closed juraj-hrivnak closed 2 months ago

juraj-hrivnak commented 2 months ago

When running the parentcommand with any argument: $ ./tool parent arg, the command is not run, but the help message is printed instead.

If we comment out the this.subcommands(Subcommand()) this starts to work properly.

Using $ ./tool parent arg subcommand for some reason accepts the argument. That is weird because allowInterspersedArgs is set to falseby default.

Source code:

class Subcommand : CliktCommand()
{
    override fun run() = Unit
}

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

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

Note that I tried both the latest & latest snapshot versions.

ajalt commented 2 months ago

Thanks for the clear reproduction.

This is by design; you can't specify parent arguments after a subcommand because it would lead to ambiguity if the subcommand also had any arguments. arg subcommand is fine because the argument occurs before the subcommand. allowInterspersedArgs only affects arguments and options, not subcommands.

juraj-hrivnak commented 2 months ago

I see, that makes sense. The issue is that the argument doesn’t work when the subcommand is not used but registered under the parent command.

ajalt commented 2 months ago

You need to enable running the parent without children

juraj-hrivnak commented 2 months ago

So that was the trick! Thank you,