Hi. I have this code: ```fun main(args: Array<S...
# clikt
d
Hi. I have this code:
Copy code
fun main(args: Array<String>) = Launcher.subcommands(InitKeystore).main(args)

object Launcher : CliktCommand(help = "", invokeWithoutSubcommand = true) {
...
    override fun run() {
        ...
        log("I have done something")
    }
}

object InitKeystore : CliktCommand(name = "keystore_init", help = "") {
...
    override fun run() {
        ...
        log("I have done init")
    }
}
When I run it without the
keystore_init
subcommand, it launches correctly only the
Launcher
, but when I specify the
keystore_init
subcommand, I see in my output both the log strings, ie. it runs the main command and then the subcommand ?
p
https://ajalt.github.io/clikt/commands/#running-parent-command-without-children This is correct. When you run a sub-command, the parent command is always run. There is no option to prevent this occurring. The
invokeWithoutSubcommand
option only controls what should happen if you Do Not invoke a sub-command, i.e. only when you do not run
keystore_init
in your case. By default this is
false
, meaning without a sub-command the parent is not run. When
true
, the parent is run with or without a sub-command.
d
Ah, thanks. Is wrapping everything in the main commands run inside of
Copy code
if (currentContext.invokedSubcommand == null)
appropriate solution to this?