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

Multiple subcommands: required option on main command reported missing #190

Closed
arm-xxl opened this issue Jun 4, 2020 · 1 comment · Fixed by #191
Closed

Multiple subcommands: required option on main command reported missing #190

arm-xxl opened this issue Jun 4, 2020 · 1 comment · Fixed by #191

Comments

@arm-xxl
Copy link

arm-xxl commented Jun 4, 2020

I have the following scenario: A CLI with multiple subcommands with the main CLI having required options. Here is what I came up with:

fun main(args: Array<String>) {
    MainCLI().subcommands(Sub1(), Sub2()).main(args)
}

class MainCLI : CliktCommand(allowMultipleSubcommands = true) {
    private val optA: String by option("-a", "--opt-a", help = "My option 'A'").required()

    override fun run() {
        echo("Running main command, optA = $optA")
    }
}

open class SubCLI : CliktCommand() {
    private val optB: String by option("-b", "--opt-b", help = "My option 'B'").default("B")

    override fun run() {
        echo("Running subcommand ${this::class.simpleName}, optB = $optB")
    }
}

class Sub1 : SubCLI()

class Sub2 : SubCLI()

When I run it like this:

main-cli --opt-a=X sub1 --opt-b=Y sub2 --opt-b=Z

I get the following output:

Running main command, optA = X
Running subcommand Sub1, optB = Y
Usage: main-cli [OPTIONS] COMMAND [ARGS]...

Error: Missing option "--opt-a".

I dug a bit with a debugger in hand and got to Parser.parse() It appears it is called several times:

//breakpoint on Line 122
//if (o !is EagerOption && o !in invocationsByOption && (o as? GroupableOption)?.parameterGroup == null) ...

*** Call 1 ***
startingArgI = 0
command -> <MainCLI name=main-cli options=[--opt-a=X --help] subcommands=[<Sub1 name=sub1 options=[--opt-b=null --help]> <Sub2 name=sub2 options=[--opt-b=null --help]>]>
subcommand -> <Sub1 name=sub1 options=[--opt-b=null --help]>

*** Call 2 ***
startingArgI = 2
command -> <Sub1 name=sub1 options=[--opt-b=Y --help]>
subcommand -> null

*** Call 3 ***
startingArgI = 3
command -> <MainCLI name=main-cli options=[--opt-a=X --help] subcommands=[<Sub1 name=sub1 options=[--opt-b=Y --help]> <Sub2 name=sub2 options=[--opt-b=null --help]>]>
subcommand -> <Sub2 name=sub2 options=[--opt-b=null --help]>

Due to my very limited knowledge in kotlin, I got stuck there.
What am I doing wrong?

@ajalt
Copy link
Owner

ajalt commented Jun 5, 2020

Thanks for the clear reproduction! This should be fixed in #191.

@ajalt ajalt closed this as completed in #191 Jun 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants